code cleanup
authorClaus Gittinger <cg@exept.de>
Thu, 10 Apr 2003 16:28:48 +0200
changeset 3844 6640f9dd4b9e
parent 3843 57d7f752c63b
child 3845 189f7932a76f
code cleanup
Colormap.st
--- a/Colormap.st	Thu Apr 10 16:25:49 2003 +0200
+++ b/Colormap.st	Thu Apr 10 16:28:48 2003 +0200
@@ -37,14 +37,11 @@
 
 documentation
 "
-    Colormaps are used with images (and Forms) to keep the byte-to-color
-    mapping.
+    Colormaps are used with images (and Forms) to keep the byte-to-color mapping.
     Externally, either colors or pixel values can be accessed.
     Internally, the values are stored as 3 separate byte-arrays
     (i.e. individual components can be 0..255).
-    This was done to avoid overhead due to allocation of many color
-    instances.
-
+    This was done to avoid overhead due to allocation of many color instances.
 
     [author:]
         Claus Gittinger
@@ -57,9 +54,10 @@
 !Colormap class methodsFor:'instance creation'!
 
 fromColors:aColorArray
-    "given a sequenceable collection of colors, return a new instance
-     of myself"
+    "given a sequenceable collection of colors, return a new instance of myself.
+     Obsolete: use #withColors: for VW compatibility."
 
+    self obsoleteMethodWarning:'use #withColors:'.
     ^ self withColors:aColorArray
 
     "
@@ -73,15 +71,33 @@
 !
 
 new:numColors
-    ^ self withColors:(Array new:numColors withAll:Color black)
+    "/ ^ self withColors:(Array new:numColors withAll:Color black)
+
+    ^ self
+        redVector:(ByteArray new:numColors withAll:0)
+        greenVector:(ByteArray new:numColors withAll:0)
+        blueVector:(ByteArray new:numColors withAll:0)
+
+    "
+     self new:100
+    "
 !
 
-redVector:r greenVector:g blueVector:b
+redVector:rV greenVector:gV blueVector:bV
     "given vectors of red/green/and blue pixelValues,
      return a new instance of myself.
      The values must be in the range 0..255."
 
-    ^ self new redVector:r greenVector:g blueVector:b
+"/    rV size == 2 ifTrue:[
+"/        "/ mhmh - actually a monochrome palette.
+"/        "/ should return an instance of MonochromePalette
+"/        self halt.
+"/    ].
+
+    ^ self new 
+        redVector:rV 
+        greenVector:gV 
+        blueVector:bV
 
     "
      Colormap 
@@ -93,6 +109,42 @@
     "Modified: 23.4.1996 / 22:16:00 / cg"
 !
 
+rgbVector:rgbVector
+    "given a single vector containing r-g-b values,
+     return a new instance of myself.
+     The values must be in the range 0..255."
+
+    |rV gV bV srcIndex nColors|
+
+    nColors := rgbVector size // 3.
+    rV := ByteArray new:nColors.
+    gV := ByteArray new:nColors.
+    bV := ByteArray new:nColors.
+
+    srcIndex := 1.
+    1 to:nColors do:[:dstIndex |
+        |r g b|
+
+        r := rgbVector at:srcIndex.
+        g := rgbVector at:srcIndex+1.
+        b := rgbVector at:srcIndex+2.
+
+        rV at:dstIndex put:r.
+        gV at:dstIndex put:g.
+        bV at:dstIndex put:b.
+        srcIndex := srcIndex + 3.
+    ].
+
+    ^ self 
+        redVector:rV 
+        greenVector:gV 
+        blueVector:bV
+
+    "
+     Colormap rgbVector:#[0 0 0 127 127 127 255 255 255]
+    "
+!
+
 withColors:aColorArray
     "given a sequenceable collection of colors, return a new instance
      of myself. Renamed from #fromColors: for ST-80 compatibility."
@@ -114,7 +166,7 @@
 
 at:index 
     "return the color for a index. 
-     Notice that index range is 1..."
+     Notice that index range is 1...nColors"
 
     |r g b idx "{ Class: SmallInteger }" |
 
@@ -148,6 +200,13 @@
     "Modified: 2.5.1996 / 17:29:29 / cg"
 !
 
+atPixelValue:pixelValue 
+    "return the color for a pixelValue. 
+     Notice that pixelValue range is 0...nColors-1"
+
+    ^ self at:(pixelValue + 1)
+!
+
 blueAt:index
     "return the blue component for some index.
      The returned value is scaled from the internal 0.. 255 to
@@ -289,7 +348,10 @@
 !Colormap methodsFor:'copying'!
 
 copyFrom:start to:stop
-    ^ self class withColors:(self asArray copyFrom:start to:stop)
+    ^ self class 
+        redVector:(redVector copyFrom:start to:stop)
+        greenVector:(greenVector copyFrom:start to:stop)
+        blueVector:(blueVector copyFrom:start to:stop)
 !
 
 postCopy
@@ -405,5 +467,5 @@
 !Colormap class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview/Colormap.st,v 1.30 2003-04-04 17:21:46 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview/Colormap.st,v 1.31 2003-04-10 14:28:48 cg Exp $'
 ! !