ImageEditor.st
changeset 2576 9e8d63aadb11
parent 2575 43e98f6d71d2
child 2585 c6150678bc9f
--- a/ImageEditor.st	Fri Sep 11 16:42:36 2009 +0200
+++ b/ImageEditor.st	Tue Sep 15 14:54:14 2009 +0200
@@ -4639,31 +4639,52 @@
 !
 
 ditherToDepth:depth
-    |ditherColors newImage|
-
-    depth = 1 ifTrue:[
-        ditherColors := Array with:(Color black) with:(Color white).
-    ] ifFalse:[ depth = 2 ifTrue:[
-        ditherColors := Array 
-                            with:(Color black) 
-                            with:(Color darkGrey)
-                            with:(Color lightGrey)
-                            with:(Color white).
-    ] ifFalse:[ depth = 3 ifTrue:[
-        ditherColors := Color colorCubeWithRed:2 green:2 blue:2. 
-    ] ifFalse:[ depth = 4 ifTrue:[
-        ditherColors := Color vgaColors. 
-    ] ifFalse:[ depth = 5 ifTrue:[
-        ditherColors := Color colorCubeWithRed:4 green:4 blue:2. 
-    ] ifFalse:[ depth = 5 ifTrue:[
-        ditherColors := Color colorCubeWithRed:4 green:4 blue:3. 
-    ] ifFalse:[ depth <= 8 ifTrue:[
-        ditherColors := Color colorCubeWithRed:6 green:6 blue:5. 
-    ] ifFalse:[ 
-        self error:'unsupported depth'.
-    ]]]]]]].                           
+    |ditherColors newImage useStandardColors nGrey greyColorsAlready additionalGreyColors moreColors d|
+
+    useStandardColors := true.
+"/    useStandardColors := Dialog confirmWithCancel:'Dither in standard colors or use a new (optimized) colormap ?'.
+"/    useStandardColors isNil ifTrue:[^ self].
+
+    useStandardColors ifTrue:[
+        depth = 1 ifTrue:[
+            ditherColors := Array with:(Color black) with:(Color white).
+        ] ifFalse:[ depth = 2 ifTrue:[
+            ditherColors := Array 
+                                with:(Color black) 
+                                with:(Color darkGrey)
+                                with:(Color lightGrey)
+                                with:(Color white).
+        ] ifFalse:[ depth = 3 ifTrue:[
+            ditherColors := Color colorCubeWithRed:2 green:2 blue:2. 
+        ] ifFalse:[ depth = 4 ifTrue:[
+            ditherColors := Color vgaColors. 
+        ] ifFalse:[ depth = 5 ifTrue:[
+            ditherColors := Color colorCubeWithRed:4 green:4 blue:2. 
+        ] ifFalse:[ depth = 6 ifTrue:[
+            ditherColors := Color colorCubeWithRed:4 green:4 blue:3.
+        ] ifFalse:[ depth <= 8 ifTrue:[
+            ditherColors := Color colorCubeWithRed:6 green:6 blue:5. 
+        ] ifFalse:[ 
+            self error:'unsupported depth'.
+        ]]]]]]].
+
+        nGrey := (2 raisedTo:depth) - ditherColors size.  
+        nGrey > 0 ifTrue:[
+            nGrey := nGrey min:128.
+            greyColorsAlready := ditherColors select:[:clr | clr isGreyColor].
+            d := 1 / nGrey.
+            moreColors := (1 to:nGrey-1) 
+                            collect:[:i | Color brightness:(d * i)] 
+                            thenReject:[:clr | greyColorsAlready includes:clr ].
+
+            ditherColors := ditherColors , moreColors.
+        ].
+    ] ifFalse:[
+self halt.
+    ].
 
     self withExecuteCursorDo:[
+        "/ newImage := self image asDitheredImageUsing:ditherColors depth:depth.
         newImage := self image asDitheredImageUsing:ditherColors depth:8.
 
         imageEditView makeUndo.