addColor
authorClaus Gittinger <cg@exept.de>
Fri, 12 Mar 1999 15:49:12 +0100
changeset 1054 ef98bc11d1fd
parent 1053 31097504a15a
child 1055 c20e03559d8d
addColor
ImageEditor.st
--- a/ImageEditor.st	Fri Mar 12 10:57:35 1999 +0100
+++ b/ImageEditor.st	Fri Mar 12 15:49:12 1999 +0100
@@ -1428,14 +1428,24 @@
 !
 
 hasClassAndSelectorDefined
-    ^ [(Smalltalk at:(imageEditView resourceClass) ifAbsent:nil) notNil
-       and:[imageEditView resourceSelector notNil]]
+    ^ [
+        |clsName|
+
+        (clsName := imageEditView resourceClass) size > 0
+        and:[(Smalltalk at:clsName ifAbsent:nil) notNil
+        and:[imageEditView resourceSelector notNil]]
+      ]
 
     "Created: / 31.7.1998 / 02:02:54 / cg"
 !
 
 hasClassDefined
-    ^ [(Smalltalk at:(imageEditView resourceClass) ifAbsent:nil) notNil]
+    ^ [
+        |clsName|
+
+        (clsName := imageEditView resourceClass) > 0
+        and:[(Smalltalk at:clsName ifAbsent:nil) notNil]
+      ]
 
     "Created: / 31.7.1998 / 02:02:22 / cg"
 !
@@ -1513,31 +1523,40 @@
 findColorMapMode
     "finds the colorMapMode for a new image"
 
-    self image depth > 8 ifTrue: [colorMapMode value: ''. self listOfColors removeAll. ^nil].
-
-    colorMapMode value: self image depth printString, '-plane'.
-
-    self listOfColors isEmpty
-    ifTrue:
-    [   
-         self colorMapMode: colorMapMode value
+    |image listOfColors|
+
+    image := self image.
+    image depth > 8 ifTrue: [
+        colorMapMode value: ''. 
+        self listOfColors removeAll. 
+        ^ nil
+    ].
+
+    colorMapMode value:(image depth printString, '-plane').
+
+    (listOfColors := self listOfColors) isEmpty ifTrue:[   
+        self colorMapMode: colorMapMode value.
+        listOfColors := self listOfColors.
+        image := self image.
     ].                               
-    imageEditView selectColors: (Array with: (self listOfColors at: 1) with: (self listOfColors at: 2 ifAbsent: [self listOfColors at: 1])).
-
-    self image mask notNil
-    ifTrue:
-    [             
-        colorMapMode value: colorMapMode value, ' + mask'.
-
-        (self listOfColors detect: [:clr| clr = (Color basicNew setColorId:0)] ifNone: nil) isNil
-        ifTrue:
-        [
-            self listOfColors addFirst: (Color basicNew setColorId:0).
-            imageEditView selectColors: (Array with: (self listOfColors at: 2 ifAbsent: [self listOfColors at: 1]) with: (self listOfColors at: 1)).
+    imageEditView selectColors: (Array 
+                                    with: (listOfColors at:1) 
+                                    with: (listOfColors at:2 ifAbsent:[listOfColors at:1])).
+
+    image mask notNil ifTrue: [             
+        colorMapMode value:(colorMapMode value, ' + mask').
+
+        (listOfColors detect: [:clr| clr = (Color basicNew setColorId:0)] ifNone: nil) isNil
+        ifTrue:[
+            listOfColors addFirst: (Color basicNew setColorId:0).
+            imageEditView selectColors: (Array 
+                                            with:(listOfColors at:2 ifAbsent:[listOfColors at: 1]) 
+                                            with: (listOfColors at: 1)).
         ]
     ].
-    self selectionOfColor value: 0.
-    self selectionOfColor value: (self listOfColors indexOf: imageEditView selectedColor).
+    self selectionOfColor 
+        value: 0;
+        value: (self listOfColors indexOf: imageEditView selectedColor).
 !
 
 update:something with:aParameter from:changedObject
@@ -1562,7 +1581,7 @@
     "Modified: / 18.8.1998 / 17:18:42 / cg"
 !
 
-updateForNoneImage
+updateForNoImage
     "updates channels and view, if image is loaded"
 
     self imageIsLoaded value: false.
@@ -1722,7 +1741,7 @@
                 self findColorMapMode.     
                 self updateLabelsAndHistory.
             ] ifFalse:[
-                self updateForNoneImage
+                self updateForNoImage
             ]
         ]
     ]
@@ -1745,7 +1764,7 @@
         self findColorMapMode.
         self updateLabelsAndHistory.
     ] ifFalse:[
-        self updateForNoneImage
+        self updateForNoImage
     ]
 
     "Modified: / 18.8.1998 / 17:04:46 / cg"
@@ -1762,7 +1781,7 @@
             self findColorMapMode.
             self updateLabelsAndHistory.
         ] ifFalse:[
-            self updateForNoneImage
+            self updateForNoImage
         ]
     ]
 
@@ -1800,11 +1819,9 @@
 editMode
     "returns editMode"
 
-    editMode isNil
-    ifTrue:
-    [
+    editMode isNil ifTrue: [
         editMode := #point asValue.
-        editMode onChangeSend: #value to: [imageEditView editMode: editMode value]
+        editMode onChangeSend:#value to:[imageEditView editMode:(editMode value)]
     ].
 
     ^editMode
@@ -1866,8 +1883,23 @@
 selectedColorIndex: anIndex
     "sets the index of the selected color"
 
-    selectedColorIndex := anIndex.    
-    imageEditView selectedColor: (self listOfColors at: anIndex ifAbsent: [^nil])
+    |clr pixel|
+
+    selectedColorIndex := anIndex.
+    clr := self listOfColors at:anIndex ifAbsent:nil.
+    clr isNil ifTrue:[^ self].
+
+    (self listOfColors at:1) = Color noColor ifTrue:[
+        anIndex == 1 ifTrue:[
+            pixel := nil.       "/ mask
+        ] ifFalse:[
+            pixel := anIndex - 2
+        ]
+    ] ifFalse:[
+        pixel := anIndex - 1
+    ].
+    imageEditView selectedColorIndex:pixel.
+    imageEditView selectedColor:clr.
 
 ! !
 
@@ -1908,25 +1940,66 @@
 !ImageEditor methodsFor:'user actions - colormap'!
 
 addColorToColormap
-    |d img cMap newCMap|
+    |depth img cMap newColorMap newImage oldCListSize|
 
     img := self image.
-    d := img depth.
+    depth := img depth.
     cMap := img colorMap.
-    ((d == 1)
-    or:[cMap size == (1 bitShift:d)]) ifTrue:[
-        d >= 8 ifTrue:[
+    cMap isNil ifTrue:[
+        self warn:'Image has no colormap\change colorMap mode first.' withCRs.
+        ^ self
+    ].
+
+    ((depth == 1)
+    or:[cMap size == (1 bitShift:depth)]) ifTrue:[
+        depth >= 8 ifTrue:[
             self warn:'No space for more colors in colormap.'.
             ^ self
         ].
-        (self confirm:'No space for more colors in colormap.\Change depth ?' withCRs)
+        (self confirm:'No space for more colors in colormap.\Change depth first.' withCRs)
         ifFalse:[
             ^ self
         ]
     ].
-    newCMap := cMap asArray copyWith:(Color black).
-    img colorMap:newCMap.
-    self listOfColors contents: newCMap.
+
+    imageEditView makeUndo.
+
+    cMap := cMap asArray.
+    oldCListSize := self listOfColors size.
+
+"/    (colorMapMode value asString endsWith:'mask') ifTrue:[
+"/        cMap last = Color noColor ifTrue:[
+"/            cMap := cMap copyWithoutLast:1
+"/        ] ifFalse:[
+"/            cMap first = Color noColor ifTrue:[
+"/                cMap := cMap copyFrom:2
+"/            ]
+"/        ].
+"/    ].
+"/
+
+    newColorMap := cMap copyWith:(Color black).
+
+    newImage := img species new
+                    width:img width
+                    height:img height
+                    depth:depth
+                    fromArray:img bits.
+
+    newImage colorMap:newColorMap.  
+    newImage fileName:img fileName.
+    newImage mask:(img mask copy).
+
+    (imageEditView image:newImage) notNil ifTrue:[
+        self listOfColors contents: newImage colorMap.
+        self findColorMapMode.
+        "/ mhmh - somehow, we get two colors added ... (sigh findColorMapMode adds another one ...)
+        self listOfColors size > (oldCListSize + 1) ifTrue:[
+            self listOfColors removeLast
+        ].
+        self selectionOfColor value:(self listOfColors size).
+        self updateLabelsAndHistory.
+    ]
 
     "Created: / 12.3.1999 / 00:20:28 / cg"
     "Modified: / 12.3.1999 / 02:09:01 / cg"
@@ -2109,23 +2182,19 @@
             image mask: nil.
         ]. 
         realColorMap := OrderedCollection new.
-        image realColorMap do:
-        [:clr|
+        image realColorMap do:[:clr|
             (realColorMap includes: clr) ifFalse: [realColorMap add: clr]
         ].
         newColors := realColorMap copyFrom: 1 to: (newColorMap size min: realColorMap size).
-        newColorMap do:
-        [:clr|
+        newColorMap do:[:clr|
             ((newColors size < newColorMap size) and: [(newColors includes: clr) not]) 
-            ifTrue:
-            [      
+            ifTrue:[      
                 newColors add: clr
             ]
         ].                  
         image colorMap: newColors.   
 
-        (imageEditView image: image) notNil ifTrue:
-        [
+        (imageEditView image: image) notNil ifTrue:[
             image fileName: oldFileName.
             self listOfColors contents: image colorMap.
             self findColorMapMode.
@@ -2318,8 +2387,7 @@
         newImage fileName:oldImage fileName.
         newImage mask:(oldImage mask copy).
 
-        (imageEditView image:newImage) notNil ifTrue:
-        [
+        (imageEditView image:newImage) notNil ifTrue:[
             self listOfColors contents: newImage colorMap.
             self findColorMapMode.
             self updateLabelsAndHistory.
@@ -2508,19 +2576,13 @@
 doLoadFromClass
     "opens a dialog for loading an image from class and a (resource-) selector"
 
-    (imageEditView loadFromClass) notNil
-    ifTrue:
-    [
-        self image notNil
-        ifTrue:
-        [
+    (imageEditView loadFromClass) notNil ifTrue:[
+        self image notNil ifTrue: [
             self listOfColors contents: self image usedColors asSet asOrderedCollection.
             self findColorMapMode.
             self updateLabelsAndHistory
-        ]
-        ifFalse:
-        [
-            self updateForNoneImage
+        ] ifFalse: [
+            self updateForNoImage
         ]
     ]
 !