91
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
118
|
13 |
SequenceableCollection subclass:#Colormap
|
572
|
14 |
instanceVariableNames:'redVector greenVector blueVector'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Graphics-Images support'
|
91
|
18 |
!
|
|
19 |
|
|
20 |
!Colormap class methodsFor:'documentation'!
|
|
21 |
|
|
22 |
copyright
|
|
23 |
"
|
|
24 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
25 |
All Rights Reserved
|
|
26 |
|
|
27 |
This software is furnished under a license and may be used
|
|
28 |
only in accordance with the terms of that license and with the
|
|
29 |
inclusion of the above copyright notice. This software may not
|
|
30 |
be provided or otherwise made available to, or used by, any
|
|
31 |
other person. No title to or ownership of the software is
|
|
32 |
hereby transferred.
|
|
33 |
"
|
|
34 |
!
|
|
35 |
|
|
36 |
documentation
|
|
37 |
"
|
|
38 |
Colormaps are used with images (and Forms) to keep the byte-to-color
|
|
39 |
mapping.
|
602
|
40 |
Externally, either colors or pixel values can be accessed.
|
91
|
41 |
Internally, the values are stored as 3 separate byte-arrays
|
|
42 |
(i.e. individual components can be 0..255).
|
602
|
43 |
This was done to avoid overhead due to allocation of many color
|
|
44 |
instances.
|
|
45 |
|
|
46 |
[see also:]
|
603
|
47 |
Color Image Form
|
91
|
48 |
"
|
|
49 |
! !
|
|
50 |
|
|
51 |
!Colormap class methodsFor:'instance creation'!
|
|
52 |
|
118
|
53 |
fromColors:aColorArray
|
602
|
54 |
"given a collection of real colors, return a new instance
|
|
55 |
of myself"
|
|
56 |
|
118
|
57 |
|n rV gV bV|
|
|
58 |
|
|
59 |
n := aColorArray size.
|
|
60 |
rV := ByteArray new:n.
|
|
61 |
gV := ByteArray new:n.
|
|
62 |
bV := ByteArray new:n.
|
|
63 |
1 to:n do:[:i |
|
602
|
64 |
|clr|
|
118
|
65 |
|
602
|
66 |
clr := aColorArray at:i.
|
|
67 |
rV at:i put:(clr red * 255 / 100) rounded.
|
|
68 |
bV at:i put:(clr green * 255 / 100) rounded.
|
|
69 |
gV at:i put:(clr blue * 255 / 100) rounded.
|
118
|
70 |
].
|
|
71 |
^ self new redVector:rV greenVector:gV blueVector:bV
|
|
72 |
|
|
73 |
"
|
|
74 |
Colormap
|
602
|
75 |
fromColors:(Array with:Color black
|
|
76 |
with:Color red
|
|
77 |
with:Color white)
|
118
|
78 |
"
|
602
|
79 |
|
|
80 |
"Modified: 23.4.1996 / 22:15:22 / cg"
|
91
|
81 |
!
|
|
82 |
|
|
83 |
redVector:r greenVector:g blueVector:b
|
602
|
84 |
"given vectors of red/green/and blue pixelValues,
|
|
85 |
return a new instance of myself.
|
|
86 |
The values must be in the range 0..255."
|
|
87 |
|
285
|
88 |
^ self new redVector:r greenVector:g blueVector:b
|
91
|
89 |
|
285
|
90 |
"
|
|
91 |
Colormap
|
602
|
92 |
redVector:#[0 127 255]
|
|
93 |
greenVector:#[0 127 255]
|
|
94 |
blueVector:#[0 127 255]
|
285
|
95 |
"
|
602
|
96 |
|
|
97 |
"Modified: 23.4.1996 / 22:16:00 / cg"
|
285
|
98 |
! !
|
91
|
99 |
|
285
|
100 |
!Colormap methodsFor:'accessing'!
|
91
|
101 |
|
|
102 |
at:index
|
|
103 |
"return the color at index - notice, that index is a typically derived
|
|
104 |
from an images pixel value which ranges 0..255; while the index here
|
|
105 |
starts at 1 (to make colormap look more like a collection)."
|
|
106 |
|
|
107 |
|r g b|
|
|
108 |
r := redVector at:index.
|
|
109 |
g := greenVector at:index.
|
|
110 |
b := blueVector at:index.
|
|
111 |
^ Color
|
|
112 |
red:(r * 100 / 255)
|
|
113 |
green:(g * 100 / 255)
|
|
114 |
blue:(b * 100 / 255)
|
|
115 |
!
|
|
116 |
|
|
117 |
at:index put:aColor
|
572
|
118 |
"set the color at index to aColor. Return aColor (sigh).
|
|
119 |
- notice, that a color index is a typically derived
|
|
120 |
from an images pixel value which ranges 0..255;
|
|
121 |
while the index here starts at 1
|
|
122 |
(to make colormap look more like a collection).
|
|
123 |
Therefore, to modify a colorMap entry for pixel i, change
|
|
124 |
the colorMaps slot at i+1."
|
91
|
125 |
|
|
126 |
|r g b|
|
572
|
127 |
|
91
|
128 |
r := (aColor red * 255 / 100) rounded.
|
|
129 |
g := (aColor green * 255 / 100) rounded.
|
|
130 |
b := (aColor blue * 255 / 100) rounded.
|
|
131 |
redVector at:index put:r.
|
|
132 |
greenVector at:index put:g.
|
|
133 |
blueVector at:index put:b.
|
572
|
134 |
^ aColor
|
|
135 |
|
|
136 |
"Modified: 19.4.1996 / 11:19:04 / cg"
|
118
|
137 |
!
|
91
|
138 |
|
285
|
139 |
blueAt:index
|
|
140 |
"return the blue component at some index.
|
153
|
141 |
The returned value is scaled from the internal 0.. 255 to
|
|
142 |
a percentage value 0..100"
|
|
143 |
|
285
|
144 |
^ (blueVector at:index) * 100 / 255
|
|
145 |
!
|
|
146 |
|
|
147 |
blueVector
|
|
148 |
"return blueVector"
|
|
149 |
|
|
150 |
^ blueVector
|
|
151 |
!
|
|
152 |
|
|
153 |
blueVector:something
|
|
154 |
"set blueVector"
|
|
155 |
|
|
156 |
blueVector := something.
|
|
157 |
!
|
|
158 |
|
|
159 |
colors
|
|
160 |
"ST-80 compatibility: return a collection containing the colors I hold"
|
|
161 |
|
|
162 |
^ self asArray
|
153
|
163 |
!
|
|
164 |
|
|
165 |
greenAt:index
|
|
166 |
"return the green component at some index.
|
|
167 |
The returned value is scaled from the internal 0.. 255 to
|
|
168 |
a percentage value 0..100"
|
|
169 |
|
|
170 |
^ (greenVector at:index) * 100 / 255
|
|
171 |
!
|
|
172 |
|
285
|
173 |
greenVector
|
|
174 |
"return greenVector"
|
|
175 |
|
|
176 |
^ greenVector
|
|
177 |
!
|
|
178 |
|
|
179 |
greenVector:something
|
|
180 |
"set greenVector"
|
|
181 |
|
|
182 |
greenVector := something.
|
|
183 |
!
|
|
184 |
|
|
185 |
redAt:index
|
|
186 |
"return the red component at some index.
|
153
|
187 |
The returned value is scaled from the internal 0.. 255 to
|
|
188 |
a percentage value 0..100"
|
|
189 |
|
285
|
190 |
^ (redVector at:index) * 100 / 255
|
|
191 |
!
|
|
192 |
|
|
193 |
redVector
|
|
194 |
"return redVector"
|
|
195 |
|
|
196 |
^ redVector
|
153
|
197 |
!
|
|
198 |
|
285
|
199 |
redVector:something
|
|
200 |
"set redVector"
|
153
|
201 |
|
285
|
202 |
redVector := something.
|
|
203 |
!
|
91
|
204 |
|
285
|
205 |
redVector:r greenVector:g blueVector:b
|
602
|
206 |
"set the red, green and blueVectors"
|
|
207 |
|
285
|
208 |
redVector := r.
|
|
209 |
greenVector := g.
|
|
210 |
blueVector := b.
|
602
|
211 |
|
|
212 |
"Modified: 23.4.1996 / 22:13:31 / cg"
|
285
|
213 |
!
|
|
214 |
|
|
215 |
size
|
602
|
216 |
"return the number of colors in the receiver"
|
|
217 |
|
285
|
218 |
^ redVector size
|
602
|
219 |
|
|
220 |
"Modified: 23.4.1996 / 22:13:43 / cg"
|
91
|
221 |
! !
|
|
222 |
|
|
223 |
!Colormap methodsFor:'misc'!
|
|
224 |
|
|
225 |
scaleValuesBy:scaleFactor
|
153
|
226 |
"multiply all values by scaleFactor; finally round to integer.
|
|
227 |
This can be used to brighten/darken an images colormap"
|
91
|
228 |
|
|
229 |
1 to:redVector size do:[:index |
|
|
230 |
redVector at:index put:((redVector at:index) * scaleFactor) rounded.
|
153
|
231 |
greenVector at:index put:((greenVector at:index) * scaleFactor) rounded.
|
|
232 |
blueVector at:index put:((blueVector at:index) * scaleFactor) rounded.
|
91
|
233 |
]
|
|
234 |
! !
|
285
|
235 |
|
|
236 |
!Colormap class methodsFor:'documentation'!
|
|
237 |
|
|
238 |
version
|
603
|
239 |
^ '$Header: /cvs/stx/stx/libview/Colormap.st,v 1.8 1996-04-23 20:16:35 cg Exp $'
|
285
|
240 |
! !
|