#REFACTORING by cg draft
authorClaus Gittinger <cg@exept.de>
Mon, 09 Oct 2017 09:16:44 +0200
changeset 3496 83bb01ebff3b
parent 3495 3f1eab73f7fa
child 3497 632e25674f39
#REFACTORING by cg class: ImageEditor added: #addPastedColorToColormap #clipBoardColor #hasWritableColorSelectedHolder comment/format in: #doNewImageFromClipboard #doNewMaskFromClipboard #pasteColorIntoColormap changed: #addColorToColormap:undoable: #processSelectedColorsWith: class: ImageEditor class changed: #colorMapMenu
ImageEditor.st
--- a/ImageEditor.st	Tue Sep 19 17:07:51 2017 +0200
+++ b/ImageEditor.st	Mon Oct 09 09:16:44 2017 +0200
@@ -2719,6 +2719,12 @@
           )
          (MenuItem
             enabled: canAddColorToColormapHolder
+            label: 'Add & Paste Color'
+            itemValue: addPastedColorToColormap
+            translateLabel: true
+          )
+         (MenuItem
+            enabled: canAddColorToColormapHolder
             label: 'Pick and Add Color...'
             itemValue: pickAndAddColorToColormap
             translateLabel: true
@@ -2806,7 +2812,7 @@
         nil
       )
 
-    "Modified: / 31-08-2017 / 18:46:33 / cg"
+    "Modified: / 08-10-2017 / 15:02:33 / cg"
 !
 
 menu
@@ -4682,6 +4688,12 @@
     "Created: / 04-07-2010 / 10:12:22 / cg"
 !
 
+hasWritableColorSelectedHolder
+    ^ [ self hasSingleColorSelectedHolder value and:[self canChangeColorInColormap ]]
+
+    "Created: / 08-10-2017 / 14:45:11 / cg"
+!
+
 imageHasColormap
     "true if a colormap is shown (might be a drwing map, not the real map)"
     
@@ -6202,18 +6214,22 @@
 
     depth := img depth.
     cMap := img colorMap.
-    (cMap isNil or:[cMap isMappedPalette or:[cMap isFixedPalette]]) ifTrue:[
-        drawingColormap isNil ifTrue:[
-            self information:(resources stringWithCRs:'Image has no colormap.\The shown colorMap is for drawing only.').
-            drawingColormap := OrderedCollection new.
+
+    (cMap notNil and:[cMap isMappedPalette or:[cMap isArray]]) ifTrue:[
+    ] ifFalse:[
+        (cMap isNil or:[cMap isFixedPalette]) ifTrue:[
+            drawingColormap isNil ifTrue:[
+                self information:(resources stringWithCRs:'Image has no colormap.\The shown colorMap is for drawing only.').
+                drawingColormap := OrderedCollection new.
+            ].
+            drawingColormap add:newColor.
+            self listOfColors contents:drawingColormap.
+            self selectedColors value:{drawingColormap size}.
+            "/ self warn:'Image has no colormap.\Change colorMap mode first.' withCRs.
+            ^ self
         ].
-        drawingColormap add:newColor.
-        self listOfColors contents:drawingColormap.
-        self selectedColors value:{drawingColormap size}.
-        "/ self warn:'Image has no colormap.\Change colorMap mode first.' withCRs.
-        ^ self
-    ].
-
+    ].
+    
     (cMap size == (1 bitShift:depth)) ifTrue:[
         depth >= 8 ifTrue:[
             self warn:'No space for more colors in colormap.'.
@@ -6261,6 +6277,20 @@
     ]
 
     "Created: / 31-08-2017 / 14:30:01 / cg"
+    "Modified: / 08-10-2017 / 09:15:10 / cg"
+!
+
+addPastedColorToColormap
+    "undoable: add the color in the clipboard to the map"
+
+    |clipBoardColor|
+
+    (clipBoardColor := self clipBoardColor) isNil ifTrue:[                
+        ^ self
+    ].
+    self addColorToColormap:clipBoardColor undoable:true
+
+    "Created: / 08-10-2017 / 09:04:23 / cg"
 !
 
 changeHLS
@@ -6568,6 +6598,24 @@
     ]
 !
 
+clipBoardColor
+    "return the color in the clipboard, or nil, if there is none.
+     If there is none, beep (if user's settings allow it)"
+     
+    |clr|
+
+    clr := imageEditView getClipboardObject.
+    clr isColor ifFalse:[
+        UserPreferences current beepInEditor ifTrue:[                
+            self window beep.
+        ].
+        ^ nil
+    ].
+    ^ clr
+
+    "Created: / 08-10-2017 / 09:02:14 / cg"
+!
+
 colorMapChanged
     |img|
 
@@ -7171,13 +7219,9 @@
 !
 
 pasteColorIntoColormap
-    |copyBufferColor cmap|
-
-    copyBufferColor := imageEditView getClipboardObject.
-    copyBufferColor isColor ifFalse:[
-        UserPreferences current beepInEditor ifTrue:[                
-            self window beep.
-        ].
+    |clipBoardColor cmap|
+
+    (clipBoardColor := self clipBoardColor) isNil ifTrue:[                
         ^ self
     ].
 
@@ -7186,16 +7230,14 @@
         drawingColormap isNil ifTrue:[
             drawingColormap := #()
         ].
-        drawingColormap := drawingColormap copyWith:copyBufferColor.
+        drawingColormap := drawingColormap copyWith:clipBoardColor.
         self selectedColors value:drawingColormap size.
         ^ self.
     ].
     
-    self processSelectedColorWith:[:clr |
-        copyBufferColor
-    ]
-
-    "Modified: / 03-02-2017 / 21:45:45 / cg"
+    self processSelectedColorWith:[:clr | clipBoardColor]
+
+    "Modified (format): / 08-10-2017 / 09:03:59 / cg"
 !
 
 pickAndAddColorToColormap
@@ -7249,10 +7291,16 @@
     imageEditView makeUndo.
 
     modifiedColormap := cMap asNewArray.
-
+    selectedColorIndices max > modifiedColormap size ifTrue:[
+        |t|
+        t := Array new:selectedColorIndices max.
+        t replaceFrom:1 with:modifiedColormap.
+        modifiedColormap := t.
+    ].
+    
     newColors := oldColors collect:aBlock.
     selectedColorIndices do:[:idx |
-        modifiedColormap at:idx-maskOffset put:(newColors at:idx)
+        modifiedColormap at:idx-maskOffset put:(newColors at:idx-maskOffset ifAbsent:[Color black])
     ].
 
     newImage := img species new
@@ -7275,7 +7323,7 @@
     ].
     self selectedColors value:selectedColorIndices.
 
-    "Modified: / 05-09-2017 / 09:03:52 / cg"
+    "Modified: / 08-10-2017 / 09:20:13 / cg"
 !
 
 reduceNumberOfColors
@@ -8790,7 +8838,7 @@
 
     self checkModified ifFalse:[ ^ self ].
 
-    image := imageEditView clipBoard.
+    image := imageEditView clipBoardImage.
     image isImageOrForm ifFalse:[
         image := Image readFrom:(image asString) onError:nil.
         image isNil ifTrue:[
@@ -8798,6 +8846,7 @@
             ^ self.
         ].
     ].
+    
     imageEditView image:image.
 
     image notNil ifTrue:[
@@ -8805,6 +8854,8 @@
         self findColorMapMode.
         self updateLabelsAndHistory.
     ]
+
+    "Modified (format): / 08-10-2017 / 08:58:03 / cg"
 !
 
 doNewMaskFromClipboard
@@ -8812,7 +8863,7 @@
 
     self checkModified ifFalse:[ ^ self ].
 
-    mask := imageEditView clipBoard.
+    mask := imageEditView clipBoardImage.
     mask isImageOrForm ifFalse:[
         mask := Image readFrom:(mask asString) onError:nil.
         mask isNil ifTrue:[
@@ -8820,6 +8871,7 @@
             ^ self.
         ].
     ].
+    
     (image:= imageEditView image) isNil ifTrue:[
         image := mask.
     ].
@@ -8831,7 +8883,7 @@
     imageEditView newImageWithUndo:newImage.
     self updateAfterImageChange.
 
-    "Modified (comment): / 16-02-2017 / 10:22:19 / cg"
+    "Modified (format): / 08-10-2017 / 08:57:58 / cg"
 !
 
 grabFullScreenImage