Color.st
changeset 3903 dde30ac28db3
parent 3898 368c43005b5a
child 3904 52925c65a8cc
--- a/Color.st	Tue Jul 01 20:20:45 2003 +0200
+++ b/Color.st	Tue Jul 01 23:17:35 2003 +0200
@@ -153,6 +153,84 @@
 
 !Color class methodsFor:'initialization'!
 
+allocateColorsIn:aColorVector on:aDevice
+    "{ Pragma: +optSpace }"
+
+    "preallocates a nR x nG x nB colorMap for later use in dithering.
+     Doing so has the advantage that the system will never run out of colors,
+     however, colors may be either inexact or dithered."
+
+    |clr round
+     devClr|
+
+    round := 0.
+    1 to:aColorVector size do:[:dstIndex |
+        clr := aColorVector at:dstIndex.
+        devClr := clr exactOn:aDevice.
+        devClr isNil ifTrue:[
+            round == 0 ifTrue:[
+                'Color [info]: scavenge to reclaim colors' infoPrintCR.
+                ObjectMemory scavenge.
+                round := 1.
+                devClr := clr exactOn:aDevice.
+            ].
+        ].
+        devClr isNil ifTrue:[
+            round == 1 ifTrue:[
+                'Color [info]: collect garbage to reclaim colors' infoPrintCR.
+                ObjectMemory performLowSpaceCleanup.
+                ObjectMemory garbageCollect.
+                round := 2.
+                devClr := clr exactOn:aDevice.
+           ].
+        ].
+        devClr isNil ifTrue:[
+            ColorAllocationFailSignal raiseErrorString:'failed to allocate fix color'.
+            ^ self
+        ].
+        aColorVector at:dstIndex put:devClr.
+    ].
+!
+
+colorCubeWithRed:nRed green:nGreen blue:nBlue
+    "{ Pragma: +optSpace }"
+
+    |nR "{Class: SmallInteger }"
+     nG "{Class: SmallInteger }"
+     nB "{Class: SmallInteger }"
+     dR dG dB red green blue dstIndex clr round
+     colorCube|
+
+    nR := nRed.
+    nG := nGreen.
+    nB := nBlue.
+
+    dR := 100.0 / (nR - 1).
+    dG := 100.0 / (nG - 1).
+    dB := 100.0 / (nB - 1).
+
+    colorCube := Array new:(nR * nG * nB).
+
+    round := 0.
+
+    dstIndex := 1.
+    1 to:nR do:[:sR |
+        red := dR * (sR - 1).
+        1 to:nG do:[:sG |
+            green := dG * (sG - 1).
+            1 to:nB do:[:sB |
+                blue := dB * (sB - 1).
+                clr := self red:red green:green blue:blue.
+                colorCube at:dstIndex put:clr.
+                dstIndex := dstIndex + 1
+            ]
+        ]
+    ].
+
+    "Created: 11.7.1996 / 17:55:32 / cg"
+    "Modified: 10.1.1997 / 15:37:13 / cg"
+!
+
 flushDeviceColors
     "unassign all colors from their device"
 
@@ -269,8 +347,7 @@
     |nR "{Class: SmallInteger }"
      nG "{Class: SmallInteger }"
      nB "{Class: SmallInteger }"
-     dR dG dB red green blue dstIndex clr round
-     fixColors|
+     dR dG dB fixColors|
 
     aDevice visualType == #TrueColor ifTrue:[^ self].
 
@@ -282,46 +359,10 @@
     dG := 100.0 / (nG - 1).
     dB := 100.0 / (nB - 1).
 
-    fixColors := Array new:(nR * nG * nB).
-
-    round := 0.
-
-    dstIndex := 1.
-    1 to:nR do:[:sR |
-        red := dR * (sR - 1).
-        1 to:nG do:[:sG |
-            green := dG * (sG - 1).
-            1 to:nB do:[:sB |
-                blue := dB * (sB - 1).
-                clr := (self red:red green:green blue:blue) exactOn:aDevice.
-                clr isNil ifTrue:[
-                    round == 0 ifTrue:[
-                        'Color [info]: scavenge to reclaim colors' infoPrintCR.
-                        ObjectMemory scavenge.
-                        round := 1.
-                        clr := (self red:red green:green blue:blue) exactOn:aDevice.
-                    ].
-                ].
-                clr isNil ifTrue:[
-                    round == 1 ifTrue:[
-                        'Color [info]: collect garbage to reclaim colors' infoPrintCR.
-                        ObjectMemory performLowSpaceCleanup.
-                        ObjectMemory garbageCollect.
-                        round := 2.
-                        clr := (self red:red green:green blue:blue) exactOn:aDevice.
-                   ].
-                ].
-                clr isNil ifTrue:[
-                    ColorAllocationFailSignal raiseErrorString:'failed to allocate fix color'.
-                    ^ self
-                ].
-                fixColors at:dstIndex put:clr.
-                dstIndex := dstIndex + 1
-            ]
-        ]
-    ].
-    aDevice setFixColors:fixColors
-                  numRed:nR numGreen:nG numBlue:nB
+    fixColors := self colorCubeWithRed:nRed green:nGreen blue:nBlue.
+    self allocateColorsIn:fixColors on:aDevice.
+
+    aDevice setFixColors:fixColors numRed:nR numGreen:nG numBlue:nB
 
     "
      Color getColorsRed:2 green:2 blue:2 on:Display
@@ -339,46 +380,16 @@
      however, colors may be either inexact or dithered."
 
     |nG "{Class: SmallInteger }"
-     d gray dstIndex clr round
-     fixGrayColors|
+     d fixGrayColors|
 
     aDevice visualType == #TrueColor ifTrue:[^ self].
 
     nG := nGray.
     d := 100.0 / (nG - 1).
 
-    fixGrayColors := Array new:nG.
-
-    round := 0.
-
-    dstIndex := 1.
-    1 to:nG do:[:sG |
-        gray := d * (sG - 1).
-        clr := (self red:gray green:gray blue:gray) exactOn:aDevice.
-        clr isNil ifTrue:[
-            round == 0 ifTrue:[
-                'Color [info]: scavenge to reclaim colors' infoPrintCR.
-                ObjectMemory scavenge.
-                round := 1.
-                clr := (self red:gray green:gray blue:gray) exactOn:aDevice.
-            ].
-        ].
-        clr isNil ifTrue:[
-            round == 1 ifTrue:[
-                'Color [info]: collect garbage to reclaim colors' infoPrintCR.
-                ObjectMemory performLowSpaceCleanup.
-                ObjectMemory garbageCollect.
-                round := 2.
-                clr := (self red:gray green:gray blue:gray) exactOn:aDevice.
-           ].
-        ].
-        clr isNil ifTrue:[
-            ColorAllocationFailSignal raiseErrorString:'failed to allocate fix gray color'.
-            ^ self
-        ].
-        fixGrayColors at:dstIndex put:clr.
-        dstIndex := dstIndex + 1
-    ].
+    fixGrayColors := self grayColorVector:nGray.
+    self allocateColorsIn:fixGrayColors on:aDevice.
+
     aDevice setFixGrayColors:fixGrayColors
 
     "
@@ -500,6 +511,33 @@
     "Modified: 21.10.1997 / 02:42:28 / cg"
 !
 
+grayColorVector:nGray
+    |nG "{Class: SmallInteger }"
+     d gray dstIndex clr round
+     grayColors|
+
+    nG := nGray.
+    d := 100.0 / (nG - 1).
+
+    grayColors := Array new:nG.
+
+    round := 0.
+
+    dstIndex := 1.
+    1 to:nG do:[:sG |
+        gray := d * (sG - 1).
+        clr := self red:gray green:gray blue:gray.
+        grayColors at:dstIndex put:clr.
+        dstIndex := dstIndex + 1
+    ].
+
+    "
+     Color getGrayColors:16 on:Display
+    "
+
+    "Created: 23.6.1997 / 15:29:50 / cg"
+!
+
 initialize
     "setup tracker of known colors and initialize classvars with
      heavily used colors"
@@ -5194,7 +5232,7 @@
 !Color class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/Color.st,v 1.180 2003-06-13 14:13:59 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/Color.st,v 1.181 2003-07-01 21:17:35 cg Exp $'
 ! !
 
 Color initialize!