author | ca |
Thu, 13 Feb 1997 22:58:54 +0100 | |
changeset 1347 | 4e19b2d586a4 |
parent 1326 | a519593747e8 |
child 1348 | acb4e4874e4d |
permissions | -rw-r--r-- |
1023 | 1 |
" |
2 |
COPYRIGHT (c) 1992 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 |
||
13 |
Object subclass:#Color |
|
1237 | 14 |
instanceVariableNames:'red green blue device colorId ditherForm replacementColor |
15 |
writable' |
|
1023 | 16 |
classVariableNames:'MaxValue Lobby Cells Black White LightGrey Grey DarkGrey Pseudo0 |
17 |
Pseudo1 PseudoAll Red Green Blue RetryAllocation DitherBits |
|
1269
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
18 |
ColorAllocationFailSignal InvalidColorNameSignal' |
1023 | 19 |
poolDictionaries:'' |
20 |
category:'Graphics-Support' |
|
21 |
! |
|
22 |
||
1144 | 23 |
!Color class methodsFor:'documentation'! |
1023 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
Color represents colors in a device independent manner, main info I keep about |
|
42 |
mySelf are the red, green and blue components scaled into 0 .. MaxValue. |
|
43 |
The device specific color can be aquired by sending a color the 'on:aDevice' message, |
|
44 |
which will return a color with the same rgb values as the receiver but specific |
|
45 |
for that device. |
|
46 |
||
47 |
Colors can be pure or dithered, depending on the capabilities of the device. |
|
48 |
For plain colors, the colorId-instvar is a handle (usually lookup-table entry) for that |
|
49 |
device. For dithered colors, the colorId is nil and ditherForm specifies the form |
|
50 |
used to dither that color. The ditherForm can be either a depth-1 bitmap or a pixmap |
|
51 |
with the devices depth. The plain colors needed by the ditherForm are found in its |
|
52 |
colormap (as usual for bitmaps). |
|
53 |
||
54 |
The default algorithm for color allocation is to ask the display for colors as |
|
55 |
new colors are created. When running out of colors, dithered colors will be used, |
|
56 |
using existing nearest colors and a dither pattern to aproximate the color. |
|
57 |
There could be situations, where no good colors are available for the dither, leading |
|
58 |
to ugly looking dither colors. |
|
59 |
This can be avoided by preallocating a set of colors over the complete range, which |
|
60 |
makes certain that appropriate colors are later available for the dither process. |
|
61 |
To do so, add a statement like: 'Color getColors5x5x5' to the startup.rc file. |
|
62 |
(beside 5x5x5, there are various other size combinations available). |
|
63 |
However, doing so may make things worse when displaying bitmap images, since this |
|
64 |
preallocated table may steal colors from the image ... |
|
65 |
||
66 |
[Instance variables:] |
|
67 |
||
68 |
red <Integer> the red component (0..MaxValue) |
|
69 |
green <Integer> the green component (0..MaxValue) |
|
70 |
blue <Integer> the blue component (0..MaxValue) |
|
71 |
||
72 |
device <Device> the device I am on, or nil |
|
73 |
colorId <Object> some device dependent identifier (or nil if dithered) |
|
74 |
ditherForm <Form> the Form to dither this color (if non-nil) |
|
75 |
writable <Boolean> true if this is for a writable color cell |
|
76 |
||
77 |
[Class variables:] |
|
78 |
||
79 |
MaxValue <Integer> r/g/b components are scaled relative to this maximum |
|
80 |
||
81 |
Lobby <Registry> all colors in use - keeps track of already allocated |
|
1229 | 82 |
colors for reuse and finalization. |
83 |
(dont use it: this will be moved to the device) |
|
84 |
||
1023 | 85 |
Cells <Registry> keeps track of allocated writable color cells |
1229 | 86 |
(dont use it: this will be moved to the device) |
1023 | 87 |
|
88 |
FixColors <Array> preallocated colors for dithering on Display |
|
89 |
NumRedFix <Integer> number of distinct red values in FixColors |
|
90 |
NumGreenFix <Integer> number of distinct green values in FixColors |
|
91 |
NumBlueFix <Integer> number of distinct blue values in FixColors |
|
92 |
||
93 |
Black <Color> for fast return of black |
|
94 |
White <Color> for fast return of white |
|
95 |
Grey <Color> for fast return of grey |
|
96 |
LightGrey <Color> for fast return of lightGrey |
|
97 |
DarkGrey <Color> for fast return of darkGrey |
|
98 |
||
99 |
Pseudo0 <Color> a color with 0 as handle (for forms and bitblit) |
|
100 |
Pseudo1 <Color> a color with 1 as handle (for forms) |
|
101 |
PseudoAll <Color> a color with allPlanes as handle (for bitblit) |
|
102 |
||
103 |
Red <Color> red, needed for dithering |
|
104 |
Green <Color> green, for dithering |
|
105 |
Blue <Color> blue, for dithering |
|
106 |
||
107 |
DitherColors <Collection> some preallocated colors for dithering |
|
1229 | 108 |
(kept, so they are available when needed) |
1023 | 109 |
|
110 |
RetryAllocation <Boolean> this flag controls how a request for a |
|
1229 | 111 |
color should be handled which failed previously. |
112 |
I.e. a color is asked for, which was dithered |
|
113 |
the last time. Since it could happen, that in |
|
114 |
the meantime more colors became free, the request |
|
115 |
might succeed this time - however, your screen may |
|
116 |
look a bit funny, due to having both dithered and |
|
117 |
undithered versions around. |
|
118 |
The default is true, which means: do retry |
|
1023 | 119 |
|
120 |
compatibility issues: |
|
121 |
||
1229 | 122 |
ST-80 seems to represent colors internally with scaled smallInteger |
123 |
components (this can be guessed from uses of |
|
124 |
scaledRed:scaledGreen:scaledBlue:). The main instance creation method is |
|
125 |
via 'ColorValue red:green:blue:', passing components in 0..1. |
|
126 |
In ST/X, component are internally represented as percent. |
|
127 |
For more compatibility (when subclassing color), these internals may |
|
128 |
change in the near future. For migration, a compatibility subclass |
|
129 |
called ColorValue is provided. |
|
130 |
After the change, Color will be renamed to ColorValue and Color |
|
131 |
be made a subclass of ColorValue (offering the 0..100 interface for |
|
132 |
backward compatibility). |
|
1023 | 133 |
|
134 |
[see also:] |
|
1229 | 135 |
DeviceWorkstation |
136 |
GraphicsContext DeviceDrawable Form Image Colormap |
|
137 |
Font Cursor |
|
1023 | 138 |
|
139 |
[author:] |
|
1229 | 140 |
Claus Gittinger |
1023 | 141 |
" |
142 |
! ! |
|
143 |
||
1144 | 144 |
!Color class methodsFor:'initialization'! |
1023 | 145 |
|
146 |
flushDeviceColors |
|
147 |
"unassign all colors from their device" |
|
148 |
||
149 |
"if all colors are registered in Lobby, use:" |
|
150 |
" |
|
151 |
Lobby do:[:aColor | |
|
152 |
aColor restored. |
|
153 |
Lobby unregister:aColor |
|
154 |
]. |
|
155 |
" |
|
156 |
||
157 |
"if only device colors are registered, use" |
|
158 |
||
159 |
self allInstances do:[:aColor | |
|
160 |
aColor restored |
|
161 |
]. |
|
162 |
||
163 |
Lobby do:[:aColor | |
|
164 |
Lobby unregister:aColor |
|
165 |
] |
|
166 |
! |
|
167 |
||
168 |
getColors6x6x4 |
|
169 |
"preallocates a 6x6x4 (144) colorMap and later uses those colors only. |
|
170 |
Doing so has the advantage that the system will never run out of colors, |
|
171 |
however, colors may be either inexact or dithered." |
|
172 |
||
173 |
self getColorsRed:6 green:6 blue:4 |
|
174 |
||
175 |
" |
|
176 |
Color getColors6x6x4 |
|
177 |
" |
|
178 |
! |
|
179 |
||
180 |
getColors6x6x5 |
|
181 |
"preallocates a 6x6x5 (180) colorMap and later uses those colors only. |
|
182 |
Doing so has the advantage that the system will never run out of colors, |
|
183 |
however, colors may be either inexact or dithered." |
|
184 |
||
185 |
self getColorsRed:6 green:6 blue:5 |
|
186 |
||
187 |
" |
|
188 |
Color getColors6x6x5 |
|
189 |
" |
|
190 |
! |
|
191 |
||
192 |
getColors6x6x6 |
|
193 |
"preallocates a 6x6x6 (196) colorMap and later uses those colors only. |
|
194 |
Doing so has the advantage that the system will never run out of colors, |
|
195 |
however, colors may be either inexact or dithered." |
|
196 |
||
197 |
self getColorsRed:6 green:6 blue:6 |
|
198 |
||
199 |
" |
|
200 |
Color getColors6x6x6 |
|
201 |
" |
|
202 |
! |
|
203 |
||
204 |
getColors6x7x4 |
|
205 |
"preallocates a 6x7x4 (168) colorMap and later uses those colors only. |
|
206 |
Doing so has the advantage that the system will never run out of colors, |
|
207 |
however, colors may be either inexact or dithered." |
|
208 |
||
209 |
self getColorsRed:6 green:7 blue:4 |
|
210 |
||
211 |
" |
|
212 |
Color getColors6x7x4 |
|
213 |
" |
|
214 |
||
215 |
"Created: 12.6.1996 / 17:41:57 / cg" |
|
216 |
! |
|
217 |
||
218 |
getColors7x8x4 |
|
219 |
"preallocates a 7x8x4 (224) colorMap and later uses those colors only. |
|
220 |
Doing so has the advantage that the system will never run out of colors, |
|
221 |
however, colors may be either inexact or dithered." |
|
222 |
||
223 |
self getColorsRed:7 green:8 blue:4 |
|
224 |
||
225 |
" |
|
226 |
Color getColors7x8x4 |
|
227 |
" |
|
228 |
! |
|
229 |
||
230 |
getColorsRed:nRed green:nGreen blue:nBlue |
|
231 |
"preallocates a nR x nG x nB colorMap for later use in dithering. |
|
232 |
Doing so has the advantage that the system will never run out of colors, |
|
233 |
however, colors may be either inexact or dithered." |
|
234 |
||
235 |
self getColorsRed:nRed green:nGreen blue:nBlue on:Display |
|
236 |
||
237 |
" |
|
238 |
Color getColorsRed:2 green:2 blue:2 |
|
239 |
" |
|
240 |
||
241 |
"Modified: 11.7.1996 / 17:58:09 / cg" |
|
242 |
! |
|
243 |
||
244 |
getColorsRed:nRed green:nGreen blue:nBlue on:aDevice |
|
245 |
"preallocates a nR x nG x nB colorMap for later use in dithering. |
|
246 |
Doing so has the advantage that the system will never run out of colors, |
|
247 |
however, colors may be either inexact or dithered." |
|
248 |
||
249 |
|nR "{Class: SmallInteger }" |
|
250 |
nG "{Class: SmallInteger }" |
|
251 |
nB "{Class: SmallInteger }" |
|
252 |
dR dG dB red green blue dstIndex clr round |
|
253 |
fixColors| |
|
254 |
||
255 |
aDevice visualType == #TrueColor ifTrue:[^ self]. |
|
256 |
||
257 |
nR := nRed. |
|
258 |
nG := nGreen. |
|
259 |
nB := nBlue. |
|
260 |
||
261 |
dR := 100.0 / (nR - 1). |
|
262 |
dG := 100.0 / (nG - 1). |
|
263 |
dB := 100.0 / (nB - 1). |
|
264 |
||
265 |
fixColors := Array new:(nR * nG * nB). |
|
266 |
||
267 |
round := 0. |
|
268 |
||
269 |
dstIndex := 1. |
|
270 |
1 to:nR do:[:sR | |
|
1165 | 271 |
red := dR * (sR - 1). |
272 |
1 to:nG do:[:sG | |
|
273 |
green := dG * (sG - 1). |
|
274 |
1 to:nB do:[:sB | |
|
275 |
blue := dB * (sB - 1). |
|
276 |
clr := (self red:red green:green blue:blue) exactOn:aDevice. |
|
277 |
clr isNil ifTrue:[ |
|
278 |
round == 0 ifTrue:[ |
|
279 |
'Color [info]: scavenge to reclaim colors' infoPrintCR. |
|
280 |
ObjectMemory scavenge. |
|
281 |
round := 1. |
|
282 |
clr := (self red:red green:green blue:blue) exactOn:aDevice. |
|
283 |
]. |
|
284 |
]. |
|
285 |
clr isNil ifTrue:[ |
|
286 |
round == 1 ifTrue:[ |
|
287 |
'Color [info]: collect garbage to reclaim colors' infoPrintCR. |
|
288 |
ObjectMemory performLowSpaceCleanup. |
|
289 |
ObjectMemory garbageCollect. |
|
290 |
round := 2. |
|
291 |
clr := (self red:red green:green blue:blue) exactOn:aDevice. |
|
292 |
]. |
|
293 |
]. |
|
294 |
clr isNil ifTrue:[ |
|
295 |
ColorAllocationFailSignal raiseErrorString:'failed to allocate fix color'. |
|
296 |
^ self |
|
297 |
]. |
|
298 |
fixColors at:dstIndex put:clr. |
|
299 |
dstIndex := dstIndex + 1 |
|
300 |
] |
|
301 |
] |
|
1023 | 302 |
]. |
303 |
aDevice setFixColors:fixColors |
|
1165 | 304 |
numRed:nR numGreen:nG numBlue:nB |
1023 | 305 |
|
306 |
" |
|
307 |
Color getColorsRed:2 green:2 blue:2 on:Display |
|
308 |
" |
|
309 |
||
310 |
"Created: 11.7.1996 / 17:55:32 / cg" |
|
1165 | 311 |
"Modified: 10.1.1997 / 15:37:13 / cg" |
1023 | 312 |
! |
313 |
||
314 |
getPrimaryColors |
|
315 |
"preallocate the primary colors. |
|
316 |
Doing so during early startup prevents us from running out |
|
317 |
of (at least those required) colors later. |
|
318 |
This guarantees, that at least some colors are available |
|
319 |
for dithering." |
|
320 |
||
321 |
self getPrimaryColorsOn:Display |
|
322 |
||
323 |
"Modified: 11.7.1996 / 18:12:17 / cg" |
|
324 |
! |
|
325 |
||
326 |
getPrimaryColorsOn:aDevice |
|
327 |
"preallocate the primary colors. |
|
328 |
Doing so during early startup prevents us from running out |
|
329 |
of (at least those required) colors later. |
|
330 |
This guarantees, that at least some colors are available |
|
331 |
for dithering (although, with only black, white, red, green and blue, |
|
332 |
dithered images look very poor)." |
|
333 |
||
334 |
|colors white black red green blue| |
|
335 |
||
336 |
(aDevice notNil and:[aDevice ditherColors isNil]) ifTrue:[ |
|
337 |
white := (self red:100 green:100 blue:100) exactOn:aDevice. |
|
338 |
black := (self red:0 green:0 blue:0) exactOn:aDevice. |
|
339 |
||
340 |
aDevice hasColors ifTrue:[ |
|
341 |
red := (self red:100 green:0 blue:0) exactOn:aDevice. |
|
342 |
green := (self red:0 green:100 blue:0) exactOn:aDevice. |
|
343 |
blue := (self red:0 green:0 blue:100) exactOn:aDevice. |
|
344 |
]. |
|
345 |
||
346 |
aDevice == Display ifTrue:[ |
|
347 |
"/ keep those around for the main display |
|
348 |
White := white. |
|
349 |
Black := black. |
|
350 |
Red := red. |
|
351 |
Green := green. |
|
352 |
Blue := blue |
|
353 |
]. |
|
354 |
||
355 |
aDevice visualType ~~ #TrueColor ifTrue:[ |
|
356 |
aDevice hasColors ifTrue:[ |
|
357 |
||
358 |
"preallocate some colors for dithering |
|
359 |
- otherwise, they may not be available when we need them ... |
|
360 |
these are: black, white, grey50, |
|
361 |
red, green, blue, yellow, cyan and magenta. |
|
362 |
" |
|
363 |
||
364 |
colors := OrderedCollection new. |
|
365 |
colors add:((self gray:50) exactOn:aDevice). |
|
366 |
||
367 |
colors add:white; add:black; add:red; add:green; add:blue. |
|
368 |
||
369 |
colors add:((self red:100 green:100 blue:0) exactOn:aDevice). |
|
370 |
colors add:((self red:100 green:0 blue:100) exactOn:aDevice). |
|
371 |
colors add:((self red:0 green:100 blue:100) exactOn:aDevice). |
|
372 |
||
373 |
colors := colors select:[:clr | clr notNil]. |
|
374 |
]. |
|
375 |
||
376 |
aDevice hasGreyscales ifTrue:[ |
|
377 |
aDevice hasColors ifFalse:[ |
|
378 |
colors := OrderedCollection new. |
|
379 |
colors add:((self gray:50) exactOn:aDevice). |
|
380 |
colors add:white; add:black. |
|
381 |
||
382 |
]. |
|
383 |
colors add:((self gray:25) exactOn:aDevice). |
|
384 |
colors add:((self gray:33) exactOn:aDevice). |
|
385 |
colors add:((self gray:67) exactOn:aDevice). |
|
386 |
colors add:((self gray:75) exactOn:aDevice). |
|
387 |
||
388 |
colors := colors select:[:clr | clr notNil]. |
|
389 |
]. |
|
390 |
||
391 |
colors notNil ifTrue:[ |
|
392 |
aDevice setDitherColors:(colors asArray). |
|
393 |
] |
|
394 |
] |
|
395 |
] |
|
396 |
||
397 |
"Created: 11.7.1996 / 18:09:28 / cg" |
|
398 |
"Modified: 11.7.1996 / 18:27:39 / cg" |
|
399 |
! |
|
400 |
||
401 |
initialize |
|
402 |
"setup tracker of known colors and initialize classvars with |
|
403 |
heavily used colors" |
|
404 |
||
405 |
ColorAllocationFailSignal isNil ifTrue:[ |
|
1269
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
406 |
ColorAllocationFailSignal := ErrorSignal newSignalMayProceed:true. |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
407 |
ColorAllocationFailSignal nameClass:self message:#colorAllocationFailSignal. |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
408 |
ColorAllocationFailSignal notifierString:'color allocation failed'. |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
409 |
|
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
410 |
InvalidColorNameSignal := ErrorSignal newSignalMayProceed:true. |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
411 |
InvalidColorNameSignal nameClass:self message:#invalidColorNameSignal. |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
412 |
InvalidColorNameSignal notifierString:'invalid color name'. |
1023 | 413 |
]. |
414 |
||
415 |
Lobby isNil ifTrue:[ |
|
1269
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
416 |
MaxValue := 16rFFFF. |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
417 |
|
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
418 |
Lobby := Registry new. |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
419 |
|
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
420 |
self getPrimaryColors. |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
421 |
|
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
422 |
"want to be informed when returning from snapshot" |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
423 |
ObjectMemory addDependent:self. |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
424 |
|
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
425 |
RetryAllocation := true. |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
426 |
|
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
427 |
DitherBits := self ditherBits |
1023 | 428 |
]. |
429 |
||
1269
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
430 |
"Modified: 24.1.1997 / 13:37:30 / cg" |
1023 | 431 |
! |
432 |
||
433 |
update:something with:aParameter from:changedObject |
|
434 |
"handle image restarts and flush any device resource handles" |
|
435 |
||
436 |
(something == #restarted) ifTrue:[ |
|
437 |
self flushDeviceColors |
|
438 |
]. |
|
439 |
(something == #returnFromSnapshot) ifTrue:[ |
|
440 |
self getPrimaryColors. |
|
441 |
||
442 |
Display visualType == #TrueColor ifTrue:[ |
|
443 |
Display releaseFixColors |
|
444 |
] ifFalse:[ |
|
445 |
Display fixColors notNil ifTrue:[ |
|
446 |
ColorAllocationFailSignal handle:[:ex | |
|
447 |
ex return |
|
448 |
] do:[ |
|
449 |
|nR nG nB| |
|
450 |
||
451 |
nR := Display numFixRed. |
|
452 |
nG := Display numFixGreen. |
|
453 |
nB := Display numFixBlue. |
|
454 |
Display releaseFixColors. |
|
455 |
self getColorsRed:nR |
|
456 |
green:nG |
|
457 |
blue:nB |
|
458 |
on:Display |
|
459 |
] |
|
460 |
] |
|
461 |
] |
|
462 |
] |
|
463 |
||
464 |
"Created: 15.6.1996 / 15:14:03 / cg" |
|
465 |
"Modified: 11.7.1996 / 18:03:38 / cg" |
|
466 |
! ! |
|
467 |
||
1144 | 468 |
!Color class methodsFor:'instance creation'! |
1023 | 469 |
|
470 |
allColor |
|
471 |
"return a special color which, when used for bit-blitting will |
|
472 |
behave like a all-1-color (i.e. have a device-pixel value of all-1s)" |
|
473 |
||
474 |
PseudoAll isNil ifTrue:[ |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
475 |
PseudoAll := self basicNew setColorId:-1 |
1023 | 476 |
]. |
477 |
^ PseudoAll |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
478 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
479 |
"Modified: 17.1.1997 / 00:05:36 / cg" |
1023 | 480 |
! |
481 |
||
482 |
brightness:brightness |
|
483 |
"create a gray color with given brightness (0..1). |
|
484 |
ST-80 compatibility." |
|
485 |
||
486 |
^ self scaledGray:(brightness * MaxValue) |
|
487 |
! |
|
488 |
||
489 |
colorId:id |
|
490 |
"return a color for a specific colorid without associating it to a |
|
491 |
specific device. Use this only for bitmaps which want 0- or 1-color, |
|
492 |
or for bitblits if you want to manipulate a specific colorplane." |
|
493 |
||
494 |
id == 0 ifTrue:[ |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
495 |
^ self noColor |
1023 | 496 |
]. |
497 |
id == 1 ifTrue:[ |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
498 |
Pseudo1 isNil ifTrue:[ |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
499 |
Pseudo1 := self basicNew setColorId:1 |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
500 |
]. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
501 |
^ Pseudo1 |
1023 | 502 |
]. |
503 |
id == -1 ifTrue:[ |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
504 |
^ self allColor |
1023 | 505 |
]. |
506 |
"look if already known" |
|
507 |
||
508 |
Lobby do:[:aColor | |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
509 |
aColor scaledRed isNil ifTrue:[ |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
510 |
(aColor colorId == id) ifTrue:[ |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
511 |
^ aColor |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
512 |
] |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
513 |
] |
1023 | 514 |
]. |
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
515 |
^ self basicNew setColorId:id |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
516 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
517 |
"Modified: 17.1.1997 / 00:06:41 / cg" |
1023 | 518 |
! |
519 |
||
520 |
cyan:c magenta:m yellow:y |
|
521 |
"return a color from cyan, magenta and yellow values. |
|
522 |
all values are given in percent (0..100)" |
|
523 |
||
524 |
^ self |
|
525 |
red:(100 - c) |
|
526 |
green:(100 - m) |
|
527 |
blue:(100 - y) |
|
528 |
||
529 |
" |
|
530 |
Color cyan:100 magenta:0 yellow:0 - cyan |
|
531 |
Color cyan:100 magenta:100 yellow:0 - blue |
|
532 |
Color cyan:100 magenta:0 yellow:100 - green |
|
533 |
Color cyan:100 magenta:100 yellow:100 - black |
|
534 |
" |
|
535 |
||
536 |
"Modified: 11.6.1996 / 18:29:15 / cg" |
|
537 |
! |
|
538 |
||
539 |
fromUser |
|
540 |
"let user point on a screen pixel. |
|
541 |
Return an instance for that pixels color" |
|
542 |
||
543 |
|p img| |
|
544 |
||
545 |
p := Screen current pointFromUser. |
|
546 |
img := Image fromScreen:(p corner:p+1). |
|
547 |
^ img at:0@0 |
|
548 |
||
549 |
" |
|
550 |
Color fromUser |
|
551 |
" |
|
552 |
||
553 |
"Modified: 31.8.1995 / 01:34:22 / claus" |
|
554 |
! |
|
555 |
||
556 |
hue:h light:l saturation:s |
|
557 |
"return a color from hue, light and saturation values. |
|
558 |
Hue is in degrees (0..360); light and sturation are |
|
559 |
in percent (0..100)" |
|
560 |
||
561 |
self withRGBFromHue:h light:l saturation:s do:[:r :g :b | |
|
562 |
^ self red:r green:g blue:b |
|
563 |
] |
|
564 |
||
565 |
" |
|
566 |
Color hue:0 light:50 saturation:100 - red |
|
567 |
Color hue:60 light:50 saturation:100 - yellow |
|
568 |
Color hue:120 light:50 saturation:100 - green |
|
569 |
Color hue:120 light:75 saturation:100 - bright green |
|
570 |
Color hue:120 light:25 saturation:100 - dark green |
|
571 |
Color hue:120 light:50 saturation:50 - greyish dark green |
|
572 |
Color hue:120 light:50 saturation:0 - b&w television dark green |
|
573 |
" |
|
574 |
||
575 |
"Modified: 23.4.1996 / 13:22:22 / cg" |
|
576 |
! |
|
577 |
||
1233 | 578 |
name:colorName |
1023 | 579 |
"Return a named color (either exact or dithered). |
580 |
Report an error, if aString is not a valid color name. |
|
581 |
||
582 |
We hereby only guarantee that the 8 basic colors are supported |
|
583 |
on every device (X uses the Xcolor database, so it supports more |
|
584 |
names - other devices use a builtIn name table containing only the |
|
585 |
common names) - use with special names (such as 'mediumGoldenRod' |
|
586 |
is not recommended). Better use: #name:ifIllegal: and provide a fallBack." |
|
587 |
||
1233 | 588 |
^ self |
589 |
name:colorName |
|
1269
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
590 |
ifIllegal:[InvalidColorNameSignal |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
591 |
raiseRequestWith:colorName |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
592 |
errorString:'invalid colorName: '] |
1023 | 593 |
|
594 |
" |
|
595 |
Color name:'brown' |
|
596 |
Color name:'foo' |
|
597 |
Color name:'snow' |
|
598 |
" |
|
599 |
||
1269
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
600 |
"Modified: 24.1.1997 / 13:39:25 / cg" |
1023 | 601 |
! |
602 |
||
1233 | 603 |
name:colorName ifIllegal:errorBlock |
1023 | 604 |
"Return a named color (either exact or dithered). |
1233 | 605 |
If aString is not a valid color name, |
606 |
return the result from evaluating errorBlock." |
|
607 |
||
608 |
|r g b| |
|
609 |
||
610 |
Display |
|
611 |
getScaledRGBFromName:colorName |
|
612 |
into:[:rr :gg :bb | |
|
613 |
r := rr. |
|
614 |
g := gg. |
|
615 |
b := bb |
|
616 |
]. |
|
617 |
||
618 |
r notNil ifTrue:[ |
|
619 |
^ self scaledRed:r scaledGreen:g scaledBlue:b |
|
620 |
]. |
|
621 |
^ errorBlock value |
|
1023 | 622 |
|
623 |
" |
|
624 |
Color name:'brown' ifIllegal:[Color black] |
|
625 |
Color name:'foo' ifIllegal:[Color black] |
|
626 |
" |
|
627 |
||
1233 | 628 |
"Modified: 16.1.1997 / 22:59:24 / cg" |
1023 | 629 |
! |
630 |
||
631 |
noColor |
|
632 |
"return a special color which, when used for bit-blitting will |
|
633 |
behave like a 0-color (i.e. have a device-pixel value of all-0s)" |
|
634 |
||
635 |
Pseudo0 isNil ifTrue:[ |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
636 |
Pseudo0 := self basicNew setColorId:0 |
1023 | 637 |
]. |
638 |
^ Pseudo0 |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
639 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
640 |
"Modified: 17.1.1997 / 00:06:49 / cg" |
1023 | 641 |
! |
642 |
||
643 |
red:r green:g blue:b |
|
644 |
"return a color from red, green and blue values; |
|
645 |
the arguments, r, g and b are interpreted as percent (0..100)" |
|
646 |
||
647 |
^ self scaledRed:(r * MaxValue // 100) |
|
648 |
scaledGreen:(g * MaxValue // 100) |
|
649 |
scaledBlue:(b * MaxValue // 100) |
|
650 |
! |
|
651 |
||
1237 | 652 |
redByte:r greenByte:g blueByte:b |
653 |
"return a color from red, green and blue values; |
|
654 |
the arguments, r, g and b are interpreted as byte values (0..255)" |
|
655 |
||
656 |
^ self scaledRed:(r * MaxValue // 255) |
|
657 |
scaledGreen:(g * MaxValue // 255) |
|
658 |
scaledBlue:(b * MaxValue // 255) |
|
659 |
||
660 |
" |
|
661 |
(Color redByte:255 greenByte:0 blueByte:0) inspect |
|
662 |
(Color redByte:255 greenByte:255 blueByte:255) inspect |
|
663 |
(Color redByte:0 greenByte:0 blueByte:0) inspect |
|
664 |
" |
|
665 |
||
666 |
"Modified: 16.1.1997 / 23:32:43 / cg" |
|
667 |
! |
|
668 |
||
1023 | 669 |
scaledGray:aGrayValue |
670 |
"return a gray color with a scaled gray value (0..MaxValue)" |
|
671 |
||
672 |
^ self scaledRed:aGrayValue scaledGreen:aGrayValue scaledBlue:aGrayValue |
|
673 |
||
674 |
"Modified: 11.6.1996 / 16:31:42 / cg" |
|
675 |
! |
|
676 |
||
677 |
scaledRed:r scaledGreen:g scaledBlue:b |
|
678 |
"return a color from red, green and blue values; |
|
679 |
the arguments, r, g and b are interpreted as (0..MaxValue)" |
|
680 |
||
681 |
|newColor| |
|
682 |
||
683 |
"look if already known" |
|
684 |
||
685 |
Lobby do:[:aColor | |
|
686 |
(r == aColor scaledRed) ifTrue:[ |
|
687 |
(g == aColor scaledGreen) ifTrue:[ |
|
688 |
(b == aColor scaledBlue) ifTrue:[ |
|
689 |
^ aColor |
|
690 |
] |
|
691 |
] |
|
692 |
] |
|
693 |
]. |
|
694 |
newColor := self basicNew setScaledRed:r scaledGreen:g scaledBlue:b device:nil. |
|
695 |
^ newColor |
|
696 |
||
697 |
" |
|
698 |
(Color red:100 green:0 blue:0) inspect |
|
699 |
(Color red:100 green:50 blue:50) inspect |
|
700 |
(Color red:50 green:0 blue:0) inspect |
|
701 |
" |
|
702 |
||
703 |
"Modified: 23.4.1996 / 13:32:36 / cg" |
|
704 |
"Modified: 2.5.1996 / 13:40:51 / stefan" |
|
705 |
! |
|
706 |
||
707 |
variableColorOn:aDevice |
|
708 |
"return a variable color (i.e. allocate a writable colorcell) on |
|
709 |
aDevice. The returned color is not shared and its rgb components |
|
710 |
are initially undefined. The components can be set to any value |
|
711 |
using Color>>red:green:blue. Care should be taken, since this call |
|
712 |
fails on static color or b&w displays (i.e. it depends on the device |
|
713 |
being a pseudocolor device using colormaps). |
|
714 |
Returns nil, if no more colorCells are available, or the display |
|
715 |
uses a fix colormap (i.e. is a directColor or staticColor pr b&w device). |
|
716 |
Because of this, you should not write your application to depend on |
|
717 |
writable colors to be available (i.e. add fallBack code to redraw |
|
718 |
things in another color)" |
|
719 |
||
720 |
|c lutIndex| |
|
721 |
||
722 |
lutIndex := aDevice colorCell. |
|
723 |
lutIndex isNil ifTrue:[^ nil]. |
|
724 |
||
725 |
c := self new. |
|
726 |
c setDevice:aDevice colorId:lutIndex. |
|
727 |
c setWritable:true. |
|
728 |
Cells isNil ifTrue:[ |
|
729 |
Cells := Registry new. |
|
730 |
]. |
|
731 |
Cells register:c. |
|
732 |
^ c |
|
733 |
||
734 |
" |
|
735 |
|l cell| |
|
736 |
||
737 |
l := Label new. |
|
738 |
l label:('hello' asText allBold). |
|
739 |
||
740 |
cell := Color variableColorOn:(Screen current). |
|
741 |
l foregroundColor:cell. |
|
742 |
[ |
|
743 |
1 to:40 do:[:i| |
|
744 |
i odd ifTrue:[ |
|
745 |
cell red:100 green:0 blue:0 |
|
746 |
] ifFalse:[ |
|
747 |
cell red:0 green:0 blue:0 |
|
748 |
]. |
|
749 |
Display flush. |
|
750 |
(Delay forSeconds:0.4) wait |
|
751 |
]. |
|
752 |
l destroy. |
|
753 |
] fork. |
|
754 |
l open |
|
755 |
" |
|
756 |
||
757 |
"Modified: 13.5.1996 / 12:41:53 / cg" |
|
758 |
! ! |
|
759 |
||
1144 | 760 |
!Color class methodsFor:'Signal constants'! |
1023 | 761 |
|
762 |
colorAllocationFailSignal |
|
763 |
"return the signal raised when a color allocation failed." |
|
764 |
||
765 |
^ ColorAllocationFailSignal |
|
766 |
||
767 |
"Created: 12.6.1996 / 17:44:49 / cg" |
|
1269
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
768 |
! |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
769 |
|
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
770 |
invalidColorNameSignal |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
771 |
"return the signal raised when an invalid color name is encountered" |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
772 |
|
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
773 |
^ InvalidColorNameSignal |
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
774 |
|
f0777b2cc08a
use a signal for invalid color names
Claus Gittinger <cg@exept.de>
parents:
1239
diff
changeset
|
775 |
"Created: 24.1.1997 / 13:36:25 / cg" |
1023 | 776 |
! ! |
777 |
||
1144 | 778 |
!Color class methodsFor:'accessing'! |
1023 | 779 |
|
780 |
allocatedColorsOn:aDevice |
|
781 |
"return a collection of colors available on aDevice" |
|
782 |
||
783 |
|colors| |
|
784 |
||
785 |
colors := OrderedCollection new. |
|
786 |
Lobby do:[:clr | |
|
787 |
(clr graphicsDevice == aDevice and:[clr colorId notNil]) ifTrue:[ |
|
788 |
colors add:clr |
|
789 |
] |
|
790 |
]. |
|
791 |
^ colors asArray |
|
792 |
||
793 |
" |
|
794 |
Color allocatedColorsOn:Display |
|
795 |
" |
|
796 |
||
797 |
"Modified: 5.7.1996 / 17:58:23 / cg" |
|
798 |
! ! |
|
799 |
||
1224
2a2270ec4236
added #releaseResourcesOnDevice:, to allow cleanup when
Claus Gittinger <cg@exept.de>
parents:
1205
diff
changeset
|
800 |
!Color class methodsFor:'cleanup'! |
2a2270ec4236
added #releaseResourcesOnDevice:, to allow cleanup when
Claus Gittinger <cg@exept.de>
parents:
1205
diff
changeset
|
801 |
|
2a2270ec4236
added #releaseResourcesOnDevice:, to allow cleanup when
Claus Gittinger <cg@exept.de>
parents:
1205
diff
changeset
|
802 |
releaseResourcesOnDevice:aDevice |
2a2270ec4236
added #releaseResourcesOnDevice:, to allow cleanup when
Claus Gittinger <cg@exept.de>
parents:
1205
diff
changeset
|
803 |
"this is sent when a display connection is closed, |
2a2270ec4236
added #releaseResourcesOnDevice:, to allow cleanup when
Claus Gittinger <cg@exept.de>
parents:
1205
diff
changeset
|
804 |
to release all cached Colors from that device" |
2a2270ec4236
added #releaseResourcesOnDevice:, to allow cleanup when
Claus Gittinger <cg@exept.de>
parents:
1205
diff
changeset
|
805 |
|
2a2270ec4236
added #releaseResourcesOnDevice:, to allow cleanup when
Claus Gittinger <cg@exept.de>
parents:
1205
diff
changeset
|
806 |
Lobby unregisterAllForWhich:[:aColor | aColor graphicsDevice == aDevice] |
2a2270ec4236
added #releaseResourcesOnDevice:, to allow cleanup when
Claus Gittinger <cg@exept.de>
parents:
1205
diff
changeset
|
807 |
|
2a2270ec4236
added #releaseResourcesOnDevice:, to allow cleanup when
Claus Gittinger <cg@exept.de>
parents:
1205
diff
changeset
|
808 |
"Modified: 16.1.1997 / 16:41:12 / cg" |
2a2270ec4236
added #releaseResourcesOnDevice:, to allow cleanup when
Claus Gittinger <cg@exept.de>
parents:
1205
diff
changeset
|
809 |
"Created: 16.1.1997 / 16:44:20 / cg" |
2a2270ec4236
added #releaseResourcesOnDevice:, to allow cleanup when
Claus Gittinger <cg@exept.de>
parents:
1205
diff
changeset
|
810 |
! ! |
2a2270ec4236
added #releaseResourcesOnDevice:, to allow cleanup when
Claus Gittinger <cg@exept.de>
parents:
1205
diff
changeset
|
811 |
|
1144 | 812 |
!Color class methodsFor:'color space conversions'! |
1023 | 813 |
|
814 |
withHLSFromRed:r green:g blue:b do:aBlock |
|
815 |
"compute hls form rgb, evaluate aBlock with h,l and s as arguments" |
|
816 |
||
817 |
|max min r1 g1 b1 delta h l s| |
|
818 |
||
819 |
"compute hls; h in 0..360; l 0..100; s 0..100" |
|
820 |
||
821 |
r1 := r / 100. "scale to 0..1" |
|
822 |
g1 := g / 100. |
|
823 |
b1 := b / 100. |
|
824 |
||
825 |
max := (r1 max:g1) max:b1. |
|
826 |
min := (r1 min:g1) min:b1. |
|
827 |
l := (max + min) / 2. |
|
828 |
||
829 |
max = min ifTrue:[ |
|
830 |
"achromatic, r=g=b" |
|
831 |
||
832 |
s := 0. |
|
833 |
h := nil |
|
834 |
] ifFalse:[ |
|
835 |
l < 0.5 ifTrue:[ |
|
836 |
s := (max - min) / (max + min) |
|
837 |
] ifFalse:[ |
|
838 |
s := (max - min) / (2 - max - min) |
|
839 |
]. |
|
840 |
||
841 |
"calc hue" |
|
842 |
||
843 |
delta := max - min. |
|
844 |
r1 = max ifTrue:[ |
|
845 |
h := (g1 - b1) / delta |
|
846 |
] ifFalse:[ |
|
847 |
g1 = max ifTrue:[ |
|
848 |
h := 2 + ((b1 - r1) / delta) |
|
849 |
] ifFalse:[ |
|
850 |
h := 4 + ((r1 - g1) / delta) |
|
851 |
] |
|
852 |
]. |
|
853 |
h := h * 60. |
|
854 |
h < 0 ifTrue:[ |
|
855 |
h := h + 360 |
|
856 |
]. |
|
857 |
]. |
|
858 |
aBlock value:h value:(l * 100) value:(s * 100) |
|
859 |
! |
|
860 |
||
861 |
withHLSFromScaledRed:r scaledGreen:g scaledBlue:b do:aBlock |
|
862 |
"compute hls form rgb, evaluate aBlock with h,l and s as arguments" |
|
863 |
||
864 |
^ self withHLSFromRed:(r * 100.0 / MaxValue) |
|
865 |
green:(g * 100.0 / MaxValue) |
|
866 |
blue:(b * 100.0 / MaxValue) |
|
867 |
do:aBlock |
|
868 |
||
869 |
"Created: 11.6.1996 / 17:23:47 / cg" |
|
870 |
! |
|
871 |
||
872 |
withRGBFromHue:h light:l saturation:s do:aBlock |
|
873 |
"compute rgb form hls, evaluate aBlock with r,g and b as arguments" |
|
874 |
||
875 |
|valueFunc s1 l1 r g b m1 m2| |
|
876 |
||
877 |
valueFunc := [:n1 :n2 :hIn | |
|
878 |
|hue| |
|
879 |
||
880 |
hue := hIn. |
|
881 |
hue > 360 ifTrue:[ |
|
882 |
hue := hue - 360 |
|
883 |
] ifFalse:[ |
|
884 |
hue < 0 ifTrue:[ |
|
885 |
hue := hue + 360 |
|
886 |
]. |
|
887 |
]. |
|
888 |
hue < 60 ifTrue:[ |
|
889 |
n1 + ((n2 - n1) * hue / 60) |
|
890 |
] ifFalse:[ |
|
891 |
hue < 180 ifTrue:[ |
|
892 |
n2 |
|
893 |
] ifFalse:[ |
|
894 |
hue < 240 ifTrue:[ |
|
895 |
n1 + ((n2 - n1) * (240 - hue) / 60) |
|
896 |
] ifFalse:[ |
|
897 |
n1 |
|
898 |
] |
|
899 |
] |
|
900 |
] |
|
901 |
]. |
|
902 |
||
903 |
"compute hls; h in 0..360; l 0..100; s 0..100" |
|
904 |
||
905 |
s1 := s / 100.0. "scale to 0..1" |
|
906 |
l1 := l / 100.0. |
|
907 |
||
908 |
l1 <= 0.5 ifTrue:[ |
|
909 |
m2 := l1 * (1 + s1) |
|
910 |
] ifFalse:[ |
|
911 |
m2 := l1 + s1 - (l1 * s1) |
|
912 |
]. |
|
913 |
||
914 |
m1 := 2 * l1 - m2. |
|
915 |
||
916 |
s1 = 0 ifTrue:[ |
|
917 |
"achromatic, ignore hue" |
|
918 |
r := g := b := l1 |
|
919 |
] ifFalse:[ |
|
920 |
r := valueFunc value:m1 value:m2 value:h + 120. |
|
921 |
g := valueFunc value:m1 value:m2 value:h. |
|
922 |
b := valueFunc value:m1 value:m2 value:h - 120. |
|
923 |
]. |
|
924 |
aBlock value:r*100 value:g*100 value:b*100 |
|
925 |
! ! |
|
926 |
||
1144 | 927 |
!Color class methodsFor:'constant colors'! |
1023 | 928 |
|
929 |
black |
|
930 |
"return the black color" |
|
931 |
||
932 |
Black isNil ifTrue:[ |
|
933 |
Black := (self red:0 green:0 blue:0) exactOn:Display |
|
934 |
]. |
|
935 |
^ Black |
|
936 |
||
937 |
" |
|
938 |
Color black inspect |
|
939 |
" |
|
940 |
||
941 |
"Modified: 11.6.1996 / 15:55:31 / cg" |
|
942 |
! |
|
943 |
||
944 |
blue |
|
945 |
"return the blue color" |
|
946 |
||
947 |
Blue isNil ifTrue:[ |
|
948 |
Blue := self red:0 green:0 blue:100 |
|
949 |
]. |
|
950 |
^ Blue |
|
951 |
||
952 |
" |
|
953 |
Color blue inspect |
|
954 |
" |
|
955 |
||
956 |
"Modified: 23.4.1996 / 13:15:51 / cg" |
|
957 |
! |
|
958 |
||
959 |
cyan |
|
960 |
"return the cyan color - ST-80 compatibility" |
|
961 |
||
962 |
^ self red:0 green:100 blue:100 |
|
963 |
||
964 |
" |
|
965 |
Color cyan inspect |
|
966 |
" |
|
967 |
||
968 |
"Modified: 23.4.1996 / 13:16:07 / cg" |
|
969 |
! |
|
970 |
||
971 |
darkGray |
|
972 |
"return the dark grey color (English version ;-)" |
|
973 |
||
974 |
DarkGrey isNil ifTrue:[ |
|
975 |
DarkGrey := self gray:33 |
|
976 |
]. |
|
977 |
^ DarkGrey |
|
978 |
||
979 |
" |
|
980 |
Color darkGray inspect |
|
981 |
" |
|
982 |
||
983 |
"Modified: 28.5.1996 / 20:53:57 / cg" |
|
984 |
! |
|
985 |
||
986 |
darkGrey |
|
987 |
"return the darkGrey color (US version ;-)" |
|
988 |
||
989 |
^ self darkGray |
|
990 |
||
991 |
" |
|
992 |
Color darkGrey inspect |
|
993 |
" |
|
994 |
||
995 |
"Modified: 28.5.1996 / 20:47:14 / cg" |
|
996 |
! |
|
997 |
||
998 |
gray |
|
999 |
"return a medium grey color (US version ;-)" |
|
1000 |
||
1001 |
Grey isNil ifTrue:[ |
|
1002 |
Grey := self gray:50 |
|
1003 |
]. |
|
1004 |
^ Grey |
|
1005 |
||
1006 |
" |
|
1007 |
Color gray inspect |
|
1008 |
" |
|
1009 |
||
1010 |
"Modified: 28.5.1996 / 20:48:36 / cg" |
|
1011 |
! |
|
1012 |
||
1013 |
gray:gray |
|
1014 |
"return a gray color (US version). |
|
1015 |
The argument, gray is interpreted as percent (0..100)." |
|
1016 |
||
1017 |
^ self red:gray green:gray blue:gray |
|
1018 |
||
1019 |
" |
|
1020 |
Color gray:25 |
|
1021 |
" |
|
1022 |
||
1023 |
"Modified: 28.5.1996 / 20:49:51 / cg" |
|
1024 |
! |
|
1025 |
||
1026 |
green |
|
1027 |
"return green" |
|
1028 |
||
1029 |
Green isNil ifTrue:[ |
|
1030 |
Green := self red:0 green:100 blue:0 |
|
1031 |
]. |
|
1032 |
^ Green |
|
1033 |
||
1034 |
" |
|
1035 |
Color green inspect |
|
1036 |
" |
|
1037 |
||
1038 |
"Modified: 23.4.1996 / 13:23:08 / cg" |
|
1039 |
! |
|
1040 |
||
1041 |
grey |
|
1042 |
"return the grey color (English version ;-)" |
|
1043 |
||
1044 |
^ self gray |
|
1045 |
||
1046 |
" |
|
1047 |
Color grey inspect |
|
1048 |
" |
|
1049 |
||
1050 |
"Modified: 28.5.1996 / 20:48:26 / cg" |
|
1051 |
! |
|
1052 |
||
1053 |
grey:grey |
|
1054 |
"return a grey color (English version). |
|
1055 |
The argument, grey is interpreted as percent (0..100)." |
|
1056 |
||
1057 |
^ self gray:grey |
|
1058 |
||
1059 |
" |
|
1060 |
Color grey:25 |
|
1061 |
Color grey:12.5 |
|
1062 |
" |
|
1063 |
||
1064 |
"Modified: 28.5.1996 / 20:50:34 / cg" |
|
1065 |
! |
|
1066 |
||
1067 |
lightGray |
|
1068 |
"return the lightGrey color (US version ;-)" |
|
1069 |
||
1070 |
LightGrey isNil ifTrue:[ |
|
1071 |
LightGrey := self gray:67 |
|
1072 |
]. |
|
1073 |
^ LightGrey |
|
1074 |
||
1075 |
" |
|
1076 |
Color lightGray inspect |
|
1077 |
" |
|
1078 |
||
1079 |
"Modified: 28.5.1996 / 20:54:00 / cg" |
|
1080 |
! |
|
1081 |
||
1082 |
lightGrey |
|
1083 |
"return the lightGrey color (English version ;-)" |
|
1084 |
||
1085 |
^ self lightGray |
|
1086 |
||
1087 |
" |
|
1088 |
Color lightGrey inspect |
|
1089 |
" |
|
1090 |
||
1091 |
"Modified: 28.5.1996 / 20:51:11 / cg" |
|
1092 |
! |
|
1093 |
||
1094 |
magenta |
|
1095 |
"return the magenta color - ST-80 compatibility" |
|
1096 |
||
1097 |
^ self red:100 green:0 blue:100 |
|
1098 |
||
1099 |
" |
|
1100 |
Color magenta inspect |
|
1101 |
" |
|
1102 |
||
1103 |
"Modified: 23.4.1996 / 13:23:41 / cg" |
|
1104 |
! |
|
1105 |
||
1106 |
mediumGray |
|
1107 |
"return medium-grey color (US version ;-)" |
|
1108 |
||
1109 |
^ self gray |
|
1110 |
||
1111 |
" |
|
1112 |
Color mediumGray inspect |
|
1113 |
" |
|
1114 |
||
1115 |
"Created: 23.4.1996 / 13:24:17 / cg" |
|
1116 |
"Modified: 28.5.1996 / 20:51:21 / cg" |
|
1117 |
! |
|
1118 |
||
1119 |
mediumGrey |
|
1120 |
"return medium-grey color (English version ;-)" |
|
1121 |
||
1122 |
^ self gray |
|
1123 |
||
1124 |
" |
|
1125 |
Color mediumGrey inspect |
|
1126 |
" |
|
1127 |
||
1128 |
"Modified: 28.5.1996 / 20:51:24 / cg" |
|
1129 |
! |
|
1130 |
||
1131 |
orange |
|
1132 |
"return the orange color - ST-80 compatibility" |
|
1133 |
||
1134 |
^ self red:75 green:50 blue:0 |
|
1135 |
||
1136 |
"Modified: 23.4.1996 / 13:29:32 / cg" |
|
1137 |
! |
|
1138 |
||
1139 |
pink |
|
1140 |
"return the pink color - ST-80 compatibility" |
|
1141 |
||
1142 |
^ self red:100 green:0 blue:100 |
|
1143 |
||
1144 |
"Modified: 23.4.1996 / 13:29:38 / cg" |
|
1145 |
! |
|
1146 |
||
1147 |
red |
|
1148 |
"return the red color" |
|
1149 |
||
1150 |
Red isNil ifTrue:[ |
|
1151 |
Red := self red:100 green:0 blue:0. |
|
1152 |
]. |
|
1153 |
^ Red |
|
1154 |
||
1155 |
"Modified: 23.4.1996 / 13:29:44 / cg" |
|
1156 |
! |
|
1157 |
||
1158 |
veryDarkGray |
|
1159 |
"return a very dark-grey color (US version ;-)" |
|
1160 |
||
1161 |
^ self gray:13 |
|
1162 |
||
1163 |
"Created: 23.4.1996 / 13:33:14 / cg" |
|
1164 |
"Modified: 28.5.1996 / 20:51:41 / cg" |
|
1165 |
! |
|
1166 |
||
1167 |
veryDarkGrey |
|
1168 |
"return a very dark-grey color (English version ;-)" |
|
1169 |
||
1170 |
^ self veryDarkGray |
|
1171 |
||
1172 |
"Modified: 28.5.1996 / 20:52:49 / cg" |
|
1173 |
! |
|
1174 |
||
1175 |
veryLightGray |
|
1176 |
"return a very light-grey color (US version ;-)" |
|
1177 |
||
1178 |
^ self gray:87 |
|
1179 |
||
1180 |
"Created: 23.4.1996 / 13:33:46 / cg" |
|
1181 |
"Modified: 28.5.1996 / 20:51:59 / cg" |
|
1182 |
! |
|
1183 |
||
1184 |
veryLightGrey |
|
1185 |
"return a very light-grey color (English version ;-)" |
|
1186 |
||
1187 |
^ self veryLightGray |
|
1188 |
||
1189 |
"Modified: 28.5.1996 / 20:52:03 / cg" |
|
1190 |
! |
|
1191 |
||
1192 |
white |
|
1193 |
"return the white-color" |
|
1194 |
||
1195 |
White isNil ifTrue:[ |
|
1196 |
White := (self red:100 green:100 blue:100) exactOn:Display |
|
1197 |
]. |
|
1198 |
^ White |
|
1199 |
! |
|
1200 |
||
1201 |
yellow |
|
1202 |
"return the yellow color - ST-80 compatibility" |
|
1203 |
||
1204 |
^ self red:100 green:100 blue:0 |
|
1205 |
||
1206 |
"Modified: 23.4.1996 / 13:33:56 / cg" |
|
1207 |
! ! |
|
1208 |
||
1233 | 1209 |
!Color class methodsFor:'obsolete'! |
1210 |
||
1211 |
nameOrDither:colorName |
|
1212 |
"return a named color - if the exact color is not available, |
|
1213 |
return a dithered color. Report an error, if the colorname is |
|
1214 |
illegal." |
|
1215 |
||
1216 |
self obsoleteMethodWarning:'use #name:'. |
|
1217 |
^ self name:colorName |
|
1218 |
||
1219 |
"Modified: 16.1.1997 / 23:01:03 / cg" |
|
1220 |
! |
|
1221 |
||
1222 |
nameOrDither:colorName ifIllegal:errorBlock |
|
1223 |
"return a named color - if the exact color is not available, |
|
1224 |
return a dithered color. If the colorname is illegal, return |
|
1225 |
the value of evaluating errorBlock." |
|
1226 |
||
1227 |
self obsoleteMethodWarning:'use #name:ifIllegal:'. |
|
1228 |
^ self name:colorName ifIllegal:errorBlock |
|
1229 |
||
1230 |
"Modified: 16.1.1997 / 23:01:32 / cg" |
|
1231 |
! |
|
1232 |
||
1233 |
nameOrNearest:colorName |
|
1234 |
"return a named color - or its nearest match" |
|
1235 |
||
1236 |
self obsoleteMethodWarning:'use #name:'. |
|
1237 |
^ self name:colorName |
|
1238 |
||
1239 |
"Modified: 16.1.1997 / 23:02:18 / cg" |
|
1240 |
! ! |
|
1241 |
||
1144 | 1242 |
!Color class methodsFor:'private'! |
1023 | 1243 |
|
1244 |
colorNearRed:r green:g blue:b on:aDevice |
|
1245 |
"return a device color on aDevice with rgb values |
|
1246 |
almost matching. If there is one, nil otherwise. |
|
1247 |
This is tried as a last chance before dithering. |
|
1248 |
The algorithm needs rework, the color components |
|
1249 |
should be weighted according some theory :-)" |
|
1250 |
||
1251 |
|bestColor minDelta diff rr rg rb dRed| |
|
1252 |
||
1253 |
"/ rr := (r * 3.0) rounded / 3.0. |
|
1254 |
"/ rg := (g * 3.0) rounded / 3.0. |
|
1255 |
"/ rb := (b * 3.0) rounded / 3.0. |
|
1256 |
||
1257 |
rr := r rounded. "round to 1%" |
|
1258 |
rg := (g * 2.0) rounded / 2.0. "round to 0.5%" |
|
1259 |
rb := (b / 2) rounded * 2. "round to 2%" |
|
1260 |
||
1261 |
minDelta := 100*100*100. |
|
1262 |
Lobby do:[:aColor | |
|
1263 |
(aColor graphicsDevice == aDevice) ifTrue:[ |
|
1264 |
"/ (aColor colorId notNil) ifTrue:[ |
|
1265 |
dRed := rr - aColor red. |
|
1266 |
dRed < 10 ifTrue:[ |
|
1267 |
diff := dRed asInteger squared |
|
1268 |
+ (rg - aColor green) asInteger squared |
|
1269 |
+ (rb - aColor blue) asInteger squared. |
|
1270 |
diff < minDelta ifTrue:[ |
|
1271 |
diff = 0 ifTrue:[ |
|
1272 |
"got it" |
|
1273 |
^ aColor |
|
1274 |
]. |
|
1275 |
bestColor := aColor. |
|
1276 |
minDelta := diff |
|
1277 |
] |
|
1278 |
] |
|
1279 |
"/ ] |
|
1280 |
] |
|
1281 |
]. |
|
1282 |
||
1283 |
"allow an error of 10% per component" |
|
1284 |
minDelta < (100+100+100) ifTrue:[ ^ bestColor ]. |
|
1285 |
^ nil |
|
1286 |
||
1287 |
"Modified: 5.7.1996 / 17:58:19 / cg" |
|
1288 |
! |
|
1289 |
||
1290 |
ditherBits |
|
1291 |
"return a dither pattern for x/64; x in 1..63" |
|
1292 |
||
1293 |
^ #( |
|
1294 |
||
1295 |
"/ 1in64 |
|
1296 |
||
1297 |
#[2r10000000 |
|
1298 |
2r00000000 |
|
1299 |
2r00000000 |
|
1300 |
2r00000000 |
|
1301 |
2r00000000 |
|
1302 |
2r00000000 |
|
1303 |
2r00000000 |
|
1304 |
2r00000000] |
|
1305 |
||
1306 |
"/ 2in64 |
|
1307 |
||
1308 |
#[2r10000000 |
|
1309 |
2r00000000 |
|
1310 |
2r00000000 |
|
1311 |
2r00000000 |
|
1312 |
2r00001000 |
|
1313 |
2r00000000 |
|
1314 |
2r00000000 |
|
1315 |
2r00000000] |
|
1316 |
||
1317 |
"/ 3in64 |
|
1318 |
||
1319 |
#[2r10000000 |
|
1320 |
2r00000000 |
|
1321 |
2r00000000 |
|
1322 |
2r00000000 |
|
1323 |
2r10001000 |
|
1324 |
2r00000000 |
|
1325 |
2r00000000 |
|
1326 |
2r00000000] |
|
1327 |
||
1328 |
"/ 4in64 |
|
1329 |
||
1330 |
#[2r10001000 |
|
1331 |
2r00000000 |
|
1332 |
2r00000000 |
|
1333 |
2r00000000 |
|
1334 |
2r10001000 |
|
1335 |
2r00000000 |
|
1336 |
2r00000000 |
|
1337 |
2r00000000] |
|
1338 |
||
1339 |
"/ 5in64 |
|
1340 |
||
1341 |
#[2r10001000 |
|
1342 |
2r00000000 |
|
1343 |
2r00000000 |
|
1344 |
2r00000000 |
|
1345 |
2r10001000 |
|
1346 |
2r00000000 |
|
1347 |
2r00000010 |
|
1348 |
2r00000000] |
|
1349 |
||
1350 |
"/ 6in64 |
|
1351 |
||
1352 |
#[2r10001000 |
|
1353 |
2r00000000 |
|
1354 |
2r00100000 |
|
1355 |
2r00000000 |
|
1356 |
2r10001000 |
|
1357 |
2r00000000 |
|
1358 |
2r00000010 |
|
1359 |
2r00000000] |
|
1360 |
||
1361 |
"/ 7in64 |
|
1362 |
||
1363 |
#[2r10001000 |
|
1364 |
2r00000000 |
|
1365 |
2r00100010 |
|
1366 |
2r00000000 |
|
1367 |
2r10001000 |
|
1368 |
2r00000000 |
|
1369 |
2r00000010 |
|
1370 |
2r00000000] |
|
1371 |
||
1372 |
"/ 8in64 |
|
1373 |
||
1374 |
#[2r10001000 |
|
1375 |
2r00000000 |
|
1376 |
2r00100010 |
|
1377 |
2r00000000 |
|
1378 |
2r10001000 |
|
1379 |
2r00000000 |
|
1380 |
2r00100010 |
|
1381 |
2r00000000] |
|
1382 |
||
1383 |
"/ 9in64 |
|
1384 |
||
1385 |
#[2r10001000 |
|
1386 |
2r00000000 |
|
1387 |
2r00100010 |
|
1388 |
2r00000000 |
|
1389 |
2r10001000 |
|
1390 |
2r00000000 |
|
1391 |
2r10100010 |
|
1392 |
2r00000000] |
|
1393 |
||
1394 |
"/ 10in64 |
|
1395 |
||
1396 |
#[2r10001000 |
|
1397 |
2r00000000 |
|
1398 |
2r00101010 |
|
1399 |
2r00000000 |
|
1400 |
2r10001000 |
|
1401 |
2r00000000 |
|
1402 |
2r10100010 |
|
1403 |
2r00000000] |
|
1404 |
||
1405 |
"/ 11in64 |
|
1406 |
||
1407 |
#[2r10001000 |
|
1408 |
2r00000000 |
|
1409 |
2r00101010 |
|
1410 |
2r00000000 |
|
1411 |
2r10001000 |
|
1412 |
2r00000000 |
|
1413 |
2r10101010 |
|
1414 |
2r00000000] |
|
1415 |
||
1416 |
"/ 12in64 |
|
1417 |
||
1418 |
#[2r10001000 |
|
1419 |
2r00000000 |
|
1420 |
2r10101010 |
|
1421 |
2r00000000 |
|
1422 |
2r10001000 |
|
1423 |
2r00000000 |
|
1424 |
2r10101010 |
|
1425 |
2r00000000] |
|
1426 |
||
1427 |
"/ 13in64 |
|
1428 |
||
1429 |
#[2r10001000 |
|
1430 |
2r00000000 |
|
1431 |
2r10101010 |
|
1432 |
2r00000000 |
|
1433 |
2r10101000 |
|
1434 |
2r00000000 |
|
1435 |
2r10101010 |
|
1436 |
2r00000000] |
|
1437 |
||
1438 |
"/ 14in64 |
|
1439 |
||
1440 |
#[2r10001010 |
|
1441 |
2r00000000 |
|
1442 |
2r10101010 |
|
1443 |
2r00000000 |
|
1444 |
2r10101000 |
|
1445 |
2r00000000 |
|
1446 |
2r10101010 |
|
1447 |
2r00000000] |
|
1448 |
||
1449 |
"/ 15in64 |
|
1450 |
||
1451 |
#[2r10001010 |
|
1452 |
2r00000000 |
|
1453 |
2r10101010 |
|
1454 |
2r00000000 |
|
1455 |
2r10101010 |
|
1456 |
2r00000000 |
|
1457 |
2r10101010 |
|
1458 |
2r00000000] |
|
1459 |
||
1460 |
"/ 16in64 |
|
1461 |
||
1462 |
#[2r10101010 |
|
1463 |
2r00000000 |
|
1464 |
2r10101010 |
|
1465 |
2r00000000 |
|
1466 |
2r10101010 |
|
1467 |
2r00000000 |
|
1468 |
2r10101010 |
|
1469 |
2r00000000] |
|
1470 |
||
1471 |
"/ 17in64 |
|
1472 |
||
1473 |
#[2r10101010 |
|
1474 |
2r01000000 |
|
1475 |
2r10101010 |
|
1476 |
2r00000000 |
|
1477 |
2r10101010 |
|
1478 |
2r00000000 |
|
1479 |
2r10101010 |
|
1480 |
2r00000000] |
|
1481 |
||
1482 |
"/ 18in64 |
|
1483 |
||
1484 |
#[2r10101010 |
|
1485 |
2r01000000 |
|
1486 |
2r10101010 |
|
1487 |
2r00000000 |
|
1488 |
2r10101010 |
|
1489 |
2r00000100 |
|
1490 |
2r10101010 |
|
1491 |
2r00000000] |
|
1492 |
||
1493 |
"/ 19in64 |
|
1494 |
||
1495 |
#[2r10101010 |
|
1496 |
2r01000000 |
|
1497 |
2r10101010 |
|
1498 |
2r00000000 |
|
1499 |
2r10101010 |
|
1500 |
2r01000100 |
|
1501 |
2r10101010 |
|
1502 |
2r00000000] |
|
1503 |
||
1504 |
"/ 20in64 |
|
1505 |
||
1506 |
#[2r10101010 |
|
1507 |
2r01000100 |
|
1508 |
2r10101010 |
|
1509 |
2r00000000 |
|
1510 |
2r10101010 |
|
1511 |
2r01000100 |
|
1512 |
2r10101010 |
|
1513 |
2r00000000] |
|
1514 |
||
1515 |
"/ 21in64 |
|
1516 |
||
1517 |
#[2r10101010 |
|
1518 |
2r01000100 |
|
1519 |
2r10101010 |
|
1520 |
2r00000000 |
|
1521 |
2r10101010 |
|
1522 |
2r01000100 |
|
1523 |
2r10101010 |
|
1524 |
2r00000001] |
|
1525 |
||
1526 |
"/ 22in64 |
|
1527 |
||
1528 |
#[2r10101010 |
|
1529 |
2r01000100 |
|
1530 |
2r10101010 |
|
1531 |
2r00010000 |
|
1532 |
2r10101010 |
|
1533 |
2r01000100 |
|
1534 |
2r10101010 |
|
1535 |
2r00000001] |
|
1536 |
||
1537 |
"/ 23in64 |
|
1538 |
||
1539 |
#[2r10101010 |
|
1540 |
2r01000100 |
|
1541 |
2r10101010 |
|
1542 |
2r00010001 |
|
1543 |
2r10101010 |
|
1544 |
2r01000100 |
|
1545 |
2r10101010 |
|
1546 |
2r00000001] |
|
1547 |
||
1548 |
"/ 24in64 |
|
1549 |
||
1550 |
#[2r10101010 |
|
1551 |
2r01000100 |
|
1552 |
2r10101010 |
|
1553 |
2r00010001 |
|
1554 |
2r10101010 |
|
1555 |
2r01000100 |
|
1556 |
2r10101010 |
|
1557 |
2r00010001] |
|
1558 |
||
1559 |
"/ 25in64 |
|
1560 |
||
1561 |
#[2r10101010 |
|
1562 |
2r01000100 |
|
1563 |
2r10101010 |
|
1564 |
2r00010001 |
|
1565 |
2r10101010 |
|
1566 |
2r01000100 |
|
1567 |
2r10101010 |
|
1568 |
2r01010001] |
|
1569 |
||
1570 |
"/ 26in64 |
|
1571 |
||
1572 |
#[2r10101010 |
|
1573 |
2r01000100 |
|
1574 |
2r10101010 |
|
1575 |
2r00010101 |
|
1576 |
2r10101010 |
|
1577 |
2r01000100 |
|
1578 |
2r10101010 |
|
1579 |
2r01010001] |
|
1580 |
||
1581 |
"/ 27in64 |
|
1582 |
||
1583 |
#[2r10101010 |
|
1584 |
2r01000100 |
|
1585 |
2r10101010 |
|
1586 |
2r00010101 |
|
1587 |
2r10101010 |
|
1588 |
2r01000100 |
|
1589 |
2r10101010 |
|
1590 |
2r01010101] |
|
1591 |
||
1592 |
"/ 28in64 |
|
1593 |
||
1594 |
#[2r10101010 |
|
1595 |
2r01000100 |
|
1596 |
2r10101010 |
|
1597 |
2r01010101 |
|
1598 |
2r10101010 |
|
1599 |
2r01000100 |
|
1600 |
2r10101010 |
|
1601 |
2r01010101] |
|
1602 |
||
1603 |
"/ 29in64 |
|
1604 |
||
1605 |
#[2r10101010 |
|
1606 |
2r01000100 |
|
1607 |
2r10101010 |
|
1608 |
2r01010101 |
|
1609 |
2r10101010 |
|
1610 |
2r01010100 |
|
1611 |
2r10101010 |
|
1612 |
2r01010101] |
|
1613 |
||
1614 |
"/ 30in64 |
|
1615 |
||
1616 |
#[2r10101010 |
|
1617 |
2r01000101 |
|
1618 |
2r10101010 |
|
1619 |
2r01010101 |
|
1620 |
2r10101010 |
|
1621 |
2r01010100 |
|
1622 |
2r10101010 |
|
1623 |
2r01010101] |
|
1624 |
||
1625 |
"/ 31in64 |
|
1626 |
||
1627 |
#[2r10101010 |
|
1628 |
2r01000101 |
|
1629 |
2r10101010 |
|
1630 |
2r01010101 |
|
1631 |
2r10101010 |
|
1632 |
2r01010101 |
|
1633 |
2r10101010 |
|
1634 |
2r01010101] |
|
1635 |
||
1636 |
"/ 32in64 |
|
1637 |
||
1638 |
#[2r10101010 |
|
1639 |
2r01010101 |
|
1640 |
2r10101010 |
|
1641 |
2r01010101 |
|
1642 |
2r10101010 |
|
1643 |
2r01010101 |
|
1644 |
2r10101010 |
|
1645 |
2r01010101] |
|
1646 |
||
1647 |
"/ 33in64 |
|
1648 |
||
1649 |
#[2r11101010 |
|
1650 |
2r01010101 |
|
1651 |
2r10101010 |
|
1652 |
2r01010101 |
|
1653 |
2r10101010 |
|
1654 |
2r01010101 |
|
1655 |
2r10101010 |
|
1656 |
2r01010101] |
|
1657 |
||
1658 |
"/ 34in64 |
|
1659 |
||
1660 |
#[2r11101010 |
|
1661 |
2r01010101 |
|
1662 |
2r10101010 |
|
1663 |
2r01010101 |
|
1664 |
2r10101110 |
|
1665 |
2r01010101 |
|
1666 |
2r10101010 |
|
1667 |
2r01010101] |
|
1668 |
||
1669 |
"/ 35in64 |
|
1670 |
||
1671 |
#[2r11101010 |
|
1672 |
2r01010101 |
|
1673 |
2r10101010 |
|
1674 |
2r01010101 |
|
1675 |
2r11101110 |
|
1676 |
2r01010101 |
|
1677 |
2r10101010 |
|
1678 |
2r01010101] |
|
1679 |
||
1680 |
"/ 36in64 |
|
1681 |
||
1682 |
#[2r11101110 |
|
1683 |
2r01010101 |
|
1684 |
2r10101010 |
|
1685 |
2r01010101 |
|
1686 |
2r11101110 |
|
1687 |
2r01010101 |
|
1688 |
2r10101010 |
|
1689 |
2r01010101] |
|
1690 |
||
1691 |
"/ 37in64 |
|
1692 |
||
1693 |
#[2r11101110 |
|
1694 |
2r01010101 |
|
1695 |
2r10101010 |
|
1696 |
2r01010101 |
|
1697 |
2r11101110 |
|
1698 |
2r01010101 |
|
1699 |
2r10101011 |
|
1700 |
2r01010101] |
|
1701 |
||
1702 |
"/ 38in64 |
|
1703 |
||
1704 |
#[2r11101110 |
|
1705 |
2r01010101 |
|
1706 |
2r10111010 |
|
1707 |
2r01010101 |
|
1708 |
2r11101110 |
|
1709 |
2r01010101 |
|
1710 |
2r10101011 |
|
1711 |
2r01010101] |
|
1712 |
||
1713 |
"/ 39in64 |
|
1714 |
||
1715 |
#[2r11101110 |
|
1716 |
2r01010101 |
|
1717 |
2r10111011 |
|
1718 |
2r01010101 |
|
1719 |
2r11101110 |
|
1720 |
2r01010101 |
|
1721 |
2r10101011 |
|
1722 |
2r01010101] |
|
1723 |
||
1724 |
"/ 40in64 |
|
1725 |
||
1726 |
#[2r11101110 |
|
1727 |
2r01010101 |
|
1728 |
2r10111011 |
|
1729 |
2r01010101 |
|
1730 |
2r11101110 |
|
1731 |
2r01010101 |
|
1732 |
2r10111011 |
|
1733 |
2r01010101] |
|
1734 |
||
1735 |
"/ 41in64 |
|
1736 |
||
1737 |
#[2r11101110 |
|
1738 |
2r01010101 |
|
1739 |
2r10111011 |
|
1740 |
2r01010101 |
|
1741 |
2r11101110 |
|
1742 |
2r01010101 |
|
1743 |
2r11111011 |
|
1744 |
2r01010101] |
|
1745 |
||
1746 |
"/ 42in64 |
|
1747 |
||
1748 |
#[2r11101110 |
|
1749 |
2r01010101 |
|
1750 |
2r10111111 |
|
1751 |
2r01010101 |
|
1752 |
2r11101110 |
|
1753 |
2r01010101 |
|
1754 |
2r11111011 |
|
1755 |
2r01010101] |
|
1756 |
||
1757 |
"/ 43in64 |
|
1758 |
||
1759 |
#[2r11101110 |
|
1760 |
2r01010101 |
|
1761 |
2r10111111 |
|
1762 |
2r01010101 |
|
1763 |
2r11101110 |
|
1764 |
2r01010101 |
|
1765 |
2r11111111 |
|
1766 |
2r01010101] |
|
1767 |
||
1768 |
"/ 44in64 |
|
1769 |
||
1770 |
#[2r11101110 |
|
1771 |
2r01010101 |
|
1772 |
2r11111111 |
|
1773 |
2r01010101 |
|
1774 |
2r11101110 |
|
1775 |
2r01010101 |
|
1776 |
2r11111111 |
|
1777 |
2r01010101] |
|
1778 |
||
1779 |
"/ 45in64 |
|
1780 |
||
1781 |
#[2r11101110 |
|
1782 |
2r01010101 |
|
1783 |
2r11111111 |
|
1784 |
2r01010101 |
|
1785 |
2r11111110 |
|
1786 |
2r01010101 |
|
1787 |
2r11111111 |
|
1788 |
2r01010101] |
|
1789 |
||
1790 |
"/ 46in64 |
|
1791 |
||
1792 |
#[2r11101111 |
|
1793 |
2r01010101 |
|
1794 |
2r11111111 |
|
1795 |
2r01010101 |
|
1796 |
2r11111110 |
|
1797 |
2r01010101 |
|
1798 |
2r11111111 |
|
1799 |
2r01010101] |
|
1800 |
||
1801 |
"/ 47in64 |
|
1802 |
||
1803 |
#[2r11101111 |
|
1804 |
2r01010101 |
|
1805 |
2r11111111 |
|
1806 |
2r01010101 |
|
1807 |
2r11111111 |
|
1808 |
2r01010101 |
|
1809 |
2r11111111 |
|
1810 |
2r01010101] |
|
1811 |
||
1812 |
"/ 48in64 |
|
1813 |
||
1814 |
#[2r11111111 |
|
1815 |
2r01010101 |
|
1816 |
2r11111111 |
|
1817 |
2r01010101 |
|
1818 |
2r11111111 |
|
1819 |
2r01010101 |
|
1820 |
2r11111111 |
|
1821 |
2r01010101] |
|
1822 |
||
1823 |
"/ 49in64 |
|
1824 |
||
1825 |
#[2r11111111 |
|
1826 |
2r01110101 |
|
1827 |
2r11111111 |
|
1828 |
2r01010101 |
|
1829 |
2r11111111 |
|
1830 |
2r01010101 |
|
1831 |
2r11111111 |
|
1832 |
2r01010101] |
|
1833 |
||
1834 |
"/ 50in64 |
|
1835 |
||
1836 |
#[2r11111111 |
|
1837 |
2r01110101 |
|
1838 |
2r11111111 |
|
1839 |
2r01010101 |
|
1840 |
2r11111111 |
|
1841 |
2r01010111 |
|
1842 |
2r11111111 |
|
1843 |
2r01010101] |
|
1844 |
||
1845 |
"/ 51in64 |
|
1846 |
||
1847 |
#[2r11111111 |
|
1848 |
2r01110101 |
|
1849 |
2r11111111 |
|
1850 |
2r01010101 |
|
1851 |
2r11111111 |
|
1852 |
2r01110111 |
|
1853 |
2r11111111 |
|
1854 |
2r01010101] |
|
1855 |
||
1856 |
"/ 52in64 |
|
1857 |
||
1858 |
#[2r11111111 |
|
1859 |
2r01110111 |
|
1860 |
2r11111111 |
|
1861 |
2r01010101 |
|
1862 |
2r11111111 |
|
1863 |
2r01110111 |
|
1864 |
2r11111111 |
|
1865 |
2r01010101] |
|
1866 |
||
1867 |
"/ 53in64 |
|
1868 |
||
1869 |
#[2r11111111 |
|
1870 |
2r01110111 |
|
1871 |
2r11111111 |
|
1872 |
2r01010101 |
|
1873 |
2r11111111 |
|
1874 |
2r01110111 |
|
1875 |
2r11111111 |
|
1876 |
2r11010101] |
|
1877 |
||
1878 |
"/ 54in64 |
|
1879 |
||
1880 |
#[2r11111111 |
|
1881 |
2r01110111 |
|
1882 |
2r11111111 |
|
1883 |
2r01011101 |
|
1884 |
2r11111111 |
|
1885 |
2r01110111 |
|
1886 |
2r11111111 |
|
1887 |
2r11010101] |
|
1888 |
||
1889 |
"/ 55in64 |
|
1890 |
||
1891 |
#[2r11111111 |
|
1892 |
2r01110111 |
|
1893 |
2r11111111 |
|
1894 |
2r11011101 |
|
1895 |
2r11111111 |
|
1896 |
2r01110111 |
|
1897 |
2r11111111 |
|
1898 |
2r11010101] |
|
1899 |
||
1900 |
"/ 56in64 |
|
1901 |
||
1902 |
#[2r11111111 |
|
1903 |
2r01110111 |
|
1904 |
2r11111111 |
|
1905 |
2r11011101 |
|
1906 |
2r11111111 |
|
1907 |
2r01110111 |
|
1908 |
2r11111111 |
|
1909 |
2r11011101] |
|
1910 |
||
1911 |
"/ 57in64 |
|
1912 |
||
1913 |
#[2r11111111 |
|
1914 |
2r01110111 |
|
1915 |
2r11111111 |
|
1916 |
2r11011101 |
|
1917 |
2r11111111 |
|
1918 |
2r01110111 |
|
1919 |
2r11111111 |
|
1920 |
2r11111101] |
|
1921 |
||
1922 |
"/ 58in64 |
|
1923 |
||
1924 |
#[2r11111111 |
|
1925 |
2r01110111 |
|
1926 |
2r11111111 |
|
1927 |
2r11011111 |
|
1928 |
2r11111111 |
|
1929 |
2r01110111 |
|
1930 |
2r11111111 |
|
1931 |
2r11111101] |
|
1932 |
||
1933 |
"/ 59in64 |
|
1934 |
||
1935 |
#[2r11111111 |
|
1936 |
2r01110111 |
|
1937 |
2r11111111 |
|
1938 |
2r11011111 |
|
1939 |
2r11111111 |
|
1940 |
2r01110111 |
|
1941 |
2r11111111 |
|
1942 |
2r11111111] |
|
1943 |
||
1944 |
"/ 60in64 |
|
1945 |
||
1946 |
#[2r11111111 |
|
1947 |
2r01110111 |
|
1948 |
2r11111111 |
|
1949 |
2r11111111 |
|
1950 |
2r11111111 |
|
1951 |
2r01110111 |
|
1952 |
2r11111111 |
|
1953 |
2r11111111] |
|
1954 |
||
1955 |
"/ 61in64 |
|
1956 |
||
1957 |
#[2r11111111 |
|
1958 |
2r01110111 |
|
1959 |
2r11111111 |
|
1960 |
2r11111111 |
|
1961 |
2r11111111 |
|
1962 |
2r01111111 |
|
1963 |
2r11111111 |
|
1964 |
2r11111111] |
|
1965 |
||
1966 |
"/ 62in64 |
|
1967 |
||
1968 |
#[2r11111111 |
|
1969 |
2r11110111 |
|
1970 |
2r11111111 |
|
1971 |
2r11111111 |
|
1972 |
2r11111111 |
|
1973 |
2r01111111 |
|
1974 |
2r11111111 |
|
1975 |
2r11111111] |
|
1976 |
||
1977 |
"/ 63in64 |
|
1978 |
||
1979 |
#[2r11111111 |
|
1980 |
2r11110111 |
|
1981 |
2r11111111 |
|
1982 |
2r11111111 |
|
1983 |
2r11111111 |
|
1984 |
2r11111111 |
|
1985 |
2r11111111 |
|
1986 |
2r11111111] |
|
1987 |
) |
|
1988 |
||
1989 |
"Modified: 23.4.1996 / 13:31:50 / cg" |
|
1990 |
"Created: 11.6.1996 / 15:34:29 / cg" |
|
1991 |
! |
|
1992 |
||
1993 |
existingColorRed:r green:g blue:b on:aDevice |
|
1994 |
"return a device color on aDevice with rgb values |
|
1995 |
if there is one, nil otherwise." |
|
1996 |
||
1997 |
^ self existingColorScaledRed:(r * MaxValue // 100) |
|
1998 |
scaledGreen:(g * MaxValue // 100) |
|
1999 |
scaledBlue:(b * MaxValue // 100) |
|
2000 |
! |
|
2001 |
||
2002 |
existingColorScaledRed:r scaledGreen:g scaledBlue:b on:aDevice |
|
2003 |
"return a device color on aDevice with rgb values |
|
2004 |
if there is one, nil otherwise." |
|
2005 |
||
2006 |
Lobby do:[:aColor | |
|
2007 |
(r == aColor scaledRed) ifTrue:[ |
|
2008 |
(g == aColor scaledGreen) ifTrue:[ |
|
2009 |
(b == aColor scaledBlue) ifTrue:[ |
|
2010 |
(aColor graphicsDevice == aDevice) ifTrue:[ |
|
2011 |
^ aColor |
|
2012 |
] |
|
2013 |
] |
|
2014 |
] |
|
2015 |
] |
|
2016 |
]. |
|
2017 |
^ nil |
|
2018 |
||
2019 |
"Modified: 5.7.1996 / 17:58:15 / cg" |
|
2020 |
! ! |
|
2021 |
||
1144 | 2022 |
!Color class methodsFor:'queries'! |
1023 | 2023 |
|
2024 |
constantNames |
|
2025 |
"return names known as instance creation messages" |
|
2026 |
||
2027 |
^ #(white black |
|
2028 |
grey mediumGrey veryLightGrey lightGrey darkGrey veryDarkGrey |
|
2029 |
red green blue cyan yellow pink orange magenta) |
|
2030 |
||
2031 |
"Modified: 2.5.1996 / 11:34:05 / cg" |
|
2032 |
! |
|
2033 |
||
2034 |
scalingValue |
|
2035 |
"ST-80 compatibility" |
|
2036 |
||
2037 |
^ MaxValue |
|
2038 |
||
2039 |
"Created: 2.5.1996 / 11:30:09 / cg" |
|
2040 |
"Modified: 11.7.1996 / 21:42:26 / cg" |
|
2041 |
! ! |
|
2042 |
||
1144 | 2043 |
!Color class methodsFor:'special instance creation'! |
1023 | 2044 |
|
2045 |
nearestColorRed:r green:g blue:b on:aDevice in:colors |
|
2046 |
"return the nearest color on aDevice with RGB values |
|
2047 |
same or near r/g/b in a collection of colors. |
|
2048 |
If there is one, return it; nil otherwise. |
|
2049 |
Near is defined as having an error less than the argument |
|
2050 |
error (in percent). The error is computed by the color |
|
2051 |
vector distance (which may not be the best possible solution)." |
|
2052 |
||
2053 |
^ self |
|
2054 |
nearestColorScaledRed:(r * MaxValue // 100) |
|
2055 |
scaledGreen:(g * MaxValue // 100) |
|
2056 |
scaledBlue:(b * MaxValue // 100) |
|
2057 |
on:aDevice |
|
2058 |
in:colors |
|
2059 |
||
2060 |
"Modified: 11.6.1996 / 18:04:55 / cg" |
|
2061 |
"Created: 14.6.1996 / 20:05:13 / cg" |
|
2062 |
! |
|
2063 |
||
2064 |
nearestColorScaledRed:r scaledGreen:g scaledBlue:b inCube:aColorCube numRed:nRed numGreen:nGreen numBlue:nBlue |
|
2065 |
"return a color with rgb values same or near r/g/b in a given |
|
2066 |
collection, containing colors from a colorCube. |
|
2067 |
This is used with preallocated fixColors and is quite fast |
|
2068 |
(no need to search)" |
|
2069 |
||
2070 |
|sR "{ Class: SmallInteger }" |
|
2071 |
sG "{ Class: SmallInteger }" |
|
2072 |
sB "{ Class: SmallInteger }" |
|
2073 |
idx "{ Class: SmallInteger }" |
|
2074 |
nR "{ Class: SmallInteger }" |
|
2075 |
nG "{ Class: SmallInteger }" |
|
2076 |
nB "{ Class: SmallInteger }" |
|
2077 |
rI "{ Class: SmallInteger }" |
|
2078 |
gI "{ Class: SmallInteger }" |
|
2079 |
bI "{ Class: SmallInteger }"| |
|
2080 |
||
2081 |
" |
|
2082 |
round to the step given by FixColors |
|
2083 |
" |
|
2084 |
nR := nRed. |
|
2085 |
nG := nGreen. |
|
2086 |
nB := nBlue. |
|
2087 |
||
2088 |
sR := MaxValue // (nR - 1). |
|
2089 |
sG := MaxValue // (nG - 1). |
|
2090 |
sB := MaxValue // (nB - 1). |
|
2091 |
||
2092 |
rI := (r + (sR // 2)) // sR. |
|
2093 |
gI := (g + (sG // 2)) // sG. |
|
2094 |
bI := (b + (sB // 2)) // sB. |
|
2095 |
idx := (((rI * nG) + gI) * nB + bI) + 1. |
|
2096 |
^ aColorCube at:idx |
|
2097 |
||
2098 |
"Modified: 11.7.1996 / 17:52:46 / cg" |
|
2099 |
"Created: 11.7.1996 / 18:20:13 / cg" |
|
2100 |
! |
|
2101 |
||
2102 |
nearestColorScaledRed:r scaledGreen:g scaledBlue:b on:aDevice |
|
2103 |
"return a device color on aDevice with RGB values |
|
2104 |
same or near r/g/b, if there is one, nil otherwise. |
|
2105 |
Near is defined as having an error less than the argument |
|
2106 |
error (in percent). The error is computed by the color |
|
2107 |
vector distance (which may not be the best possible solution)." |
|
2108 |
||
2109 |
|cube| |
|
2110 |
||
2111 |
" |
|
2112 |
if there are preallocated colors, things are much easier ... |
|
2113 |
" |
|
2114 |
(cube := aDevice fixColors) notNil ifTrue:[ |
|
2115 |
^ self |
|
2116 |
nearestColorScaledRed:r |
|
2117 |
scaledGreen:g |
|
2118 |
scaledBlue:b |
|
2119 |
inCube:cube |
|
2120 |
numRed:(aDevice numFixRed) |
|
2121 |
numGreen:(aDevice numFixGreen) |
|
2122 |
numBlue:(aDevice numFixBlue) |
|
2123 |
]. |
|
2124 |
||
2125 |
" |
|
2126 |
search in existing colors ... |
|
2127 |
" |
|
2128 |
^ self |
|
2129 |
nearestColorScaledRed:r |
|
2130 |
scaledGreen:g |
|
2131 |
scaledBlue:b |
|
2132 |
on:aDevice |
|
2133 |
in:Lobby |
|
2134 |
||
2135 |
"Created: 14.6.1996 / 20:11:18 / cg" |
|
2136 |
"Modified: 11.7.1996 / 18:20:50 / cg" |
|
2137 |
! |
|
2138 |
||
2139 |
nearestColorScaledRed:r scaledGreen:g scaledBlue:b on:aDevice in:colors |
|
2140 |
"return the nearest color on aDevice with RGB values |
|
2141 |
same or near r/g/b in a collection of colors. |
|
2142 |
If there is one, return it; nil otherwise." |
|
2143 |
||
2144 |
|delta minDelta bestSoFar| |
|
2145 |
||
2146 |
minDelta := 9999999. |
|
2147 |
||
2148 |
colors do:[:aColor | |
|
2149 |
|cr cg cb| |
|
2150 |
||
2151 |
(aColor graphicsDevice == aDevice) ifTrue:[ |
|
2152 |
aColor colorId notNil ifTrue:[ |
|
2153 |
delta := aColor deltaFromScaledRed:r scaledGreen:g scaledBlue:b. |
|
2154 |
delta < minDelta ifTrue:[ |
|
2155 |
" |
|
2156 |
an exact fit - no need to continue search |
|
2157 |
" |
|
2158 |
delta == 0 ifTrue:[^ aColor]. |
|
2159 |
||
2160 |
bestSoFar := aColor. |
|
2161 |
minDelta := delta |
|
2162 |
] |
|
2163 |
] |
|
2164 |
] |
|
2165 |
]. |
|
2166 |
||
2167 |
^ bestSoFar |
|
2168 |
||
2169 |
"Created: 11.6.1996 / 18:02:12 / cg" |
|
2170 |
"Modified: 5.7.1996 / 17:58:09 / cg" |
|
2171 |
! |
|
2172 |
||
2173 |
quickNearestColorScaledRed:r scaledGreen:g scaledBlue:b on:aDevice |
|
2174 |
"return a device color on aDevice with rgb values |
|
2175 |
same or near r/g/b. |
|
2176 |
Near is defined as having an error less than the argument |
|
2177 |
error (in percent). The error is computed by the color |
|
2178 |
vector distance (which may not be the best possible solution). |
|
2179 |
This looks for primary colors only and is thus faster |
|
2180 |
than the general nearestColor search (slightly uglier though)." |
|
2181 |
||
2182 |
|cube| |
|
2183 |
||
2184 |
" |
|
2185 |
if there are preallocated colors, thungs are much easier ... |
|
2186 |
" |
|
2187 |
(cube := aDevice fixColors) ifTrue:[ |
|
2188 |
^ self |
|
2189 |
nearestColorScaledRed:r |
|
2190 |
scaledGreen:g |
|
2191 |
scaledBlue:b |
|
2192 |
inCube:cube |
|
2193 |
numRed:(aDevice numFixRed) |
|
2194 |
numGreen:(aDevice numFixGreen) |
|
2195 |
numBlue:(aDevice numFixBlue) |
|
2196 |
]. |
|
2197 |
||
2198 |
" |
|
2199 |
search in existing colors ... |
|
2200 |
" |
|
2201 |
^ self nearestColorScaledRed:r |
|
2202 |
scaledGreen:g |
|
2203 |
scaledBlue:b |
|
2204 |
on:aDevice |
|
2205 |
in:aDevice availableDitherColors |
|
2206 |
||
2207 |
"Created: 14.6.1996 / 20:13:22 / cg" |
|
2208 |
"Modified: 11.7.1996 / 18:20:14 / cg" |
|
2209 |
! ! |
|
2210 |
||
1347 | 2211 |
!Color methodsFor:'ST-80 compatibility'! |
2212 |
||
2213 |
asDevicePaintOn:aDevice |
|
2214 |
"ST-80 compatibility: an alias for on:. |
|
2215 |
create a new Color representing the same color as |
|
2216 |
myself on aDevice; if one already exists, return the one" |
|
2217 |
||
2218 |
^ self on:aDevice |
|
2219 |
! ! |
|
2220 |
||
1023 | 2221 |
!Color methodsFor:'accessing'! |
2222 |
||
2223 |
blue |
|
2224 |
"return the blue component in percent [0..100]" |
|
2225 |
||
2226 |
(blue isNil and:[colorId notNil]) ifTrue:[ |
|
2227 |
device getRGBFrom:colorId into:[:r :g :b | ^ b]. |
|
2228 |
]. |
|
2229 |
^ blue * 100.0 / MaxValue |
|
2230 |
! |
|
2231 |
||
2232 |
blueByte |
|
2233 |
"return the blue components value mapped to 0..255" |
|
2234 |
||
2235 |
^ blue * 255 // MaxValue |
|
2236 |
||
2237 |
" |
|
2238 |
Color red blueByte |
|
2239 |
Color blue blueByte |
|
2240 |
Color green blueByte |
|
2241 |
Color black blueByte |
|
2242 |
Color grey blueByte |
|
2243 |
Color white blueByte |
|
2244 |
" |
|
2245 |
||
2246 |
"Created: 7.6.1996 / 18:30:25 / cg" |
|
2247 |
"Modified: 7.6.1996 / 18:32:03 / cg" |
|
2248 |
! |
|
2249 |
||
2250 |
colorId |
|
2251 |
"return the device-dependent color-id" |
|
2252 |
||
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2253 |
colorId notNil ifTrue:[^ colorId]. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2254 |
replacementColor notNil ifTrue:[^ replacementColor colorId]. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2255 |
^ nil |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2256 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2257 |
"Modified: 17.1.1997 / 00:03:18 / cg" |
1023 | 2258 |
! |
2259 |
||
2260 |
cyan |
|
2261 |
"return the cyan component in percent [0..100] in cmy color space" |
|
2262 |
||
2263 |
^ 100 - self red |
|
2264 |
||
2265 |
"Modified: 11.6.1996 / 17:20:07 / cg" |
|
2266 |
"Created: 11.6.1996 / 18:30:00 / cg" |
|
2267 |
! |
|
2268 |
||
2269 |
device |
|
2270 |
"return the device I am associated to" |
|
2271 |
||
2272 |
^ device |
|
2273 |
||
2274 |
"Modified: 23.4.1996 / 13:36:42 / cg" |
|
2275 |
! |
|
2276 |
||
2277 |
deviceBlue |
|
2278 |
"return the actual value of the blue component in percent." |
|
2279 |
||
2280 |
|v| |
|
2281 |
||
2282 |
device getRGBFrom:colorId into:[:r :g :b | v := b]. |
|
2283 |
^ v |
|
2284 |
! |
|
2285 |
||
2286 |
deviceGreen |
|
2287 |
"return the actual value of the green component in percent. |
|
2288 |
(usually 16bit in X; but could be different on other systems)" |
|
2289 |
||
2290 |
|v| |
|
2291 |
||
2292 |
device getRGBFrom:colorId into:[:r :g :b | v := g]. |
|
2293 |
^ v |
|
2294 |
! |
|
2295 |
||
2296 |
deviceRed |
|
2297 |
"return the actual value of the red component in percent." |
|
2298 |
||
2299 |
|v| |
|
2300 |
||
2301 |
device getRGBFrom:colorId into:[:r :g :b | v := r]. |
|
2302 |
^ v |
|
2303 |
||
2304 |
" |
|
2305 |
(Color yellow on:Display) deviceRedValue |
|
2306 |
(Color yellow on:aPrinterPage) deviceRedValue |
|
2307 |
" |
|
2308 |
! |
|
2309 |
||
2310 |
ditherForm |
|
2311 |
"return the form to dither the color" |
|
2312 |
||
2313 |
^ ditherForm |
|
2314 |
! |
|
2315 |
||
2316 |
graphicsDevice |
|
2317 |
"same as #device, for ST-80 compatibility naming. |
|
2318 |
Return the device I am associated with." |
|
2319 |
||
2320 |
^ device |
|
2321 |
||
2322 |
"Created: 28.5.1996 / 18:39:27 / cg" |
|
2323 |
! |
|
2324 |
||
2325 |
green |
|
2326 |
"return the green component in percent [0..100]" |
|
2327 |
||
2328 |
(green isNil and:[colorId notNil]) ifTrue:[ |
|
2329 |
device getRGBFrom:colorId into:[:r :g :b | ^ g]. |
|
2330 |
]. |
|
2331 |
^ green * 100.0 / MaxValue |
|
2332 |
! |
|
2333 |
||
2334 |
greenByte |
|
2335 |
"return the green components value mapped to 0..255" |
|
2336 |
||
2337 |
^ green * 255 // MaxValue |
|
2338 |
||
2339 |
" |
|
2340 |
Color red greenByte |
|
2341 |
Color blue greenByte |
|
2342 |
Color green greenByte |
|
2343 |
Color black greenByte |
|
2344 |
Color grey greenByte |
|
2345 |
Color white greenByte |
|
2346 |
" |
|
2347 |
||
2348 |
"Modified: 7.6.1996 / 18:31:30 / cg" |
|
2349 |
! |
|
2350 |
||
2351 |
hue |
|
2352 |
"return the hue (in hue/light/saturation model) in degrees [0..360]" |
|
2353 |
||
2354 |
|r g b h| |
|
2355 |
||
2356 |
(red isNil and:[colorId notNil]) ifTrue:[ |
|
2357 |
device getRGBFrom:colorId into:[:xr :xg :xb | |
|
2358 |
r := xr. |
|
2359 |
g := xg. |
|
2360 |
b := xb. |
|
2361 |
] |
|
2362 |
] ifFalse:[ |
|
2363 |
r := self red. |
|
2364 |
g := self green. |
|
2365 |
b := self blue. |
|
2366 |
]. |
|
2367 |
||
2368 |
self class withHLSFromRed:r green:g blue:b do:[:xh :xl :xs | |
|
2369 |
h := xh |
|
2370 |
]. |
|
2371 |
^ h |
|
2372 |
||
2373 |
" |
|
2374 |
Color yellow hue |
|
2375 |
" |
|
2376 |
||
2377 |
"Modified: 11.6.1996 / 17:14:51 / cg" |
|
2378 |
! |
|
2379 |
||
2380 |
light |
|
2381 |
"return the light (in hue/light/saturation model) in percent [0..100]. |
|
2382 |
This corresponds to the brightness of the color (if displayed on |
|
2383 |
a b&w television screen)" |
|
2384 |
||
2385 |
|r g b l| |
|
2386 |
||
2387 |
(red isNil and:[colorId notNil]) ifTrue:[ |
|
2388 |
device getRGBFrom:colorId into:[:xr :xg :xb | |
|
2389 |
r := xr. |
|
2390 |
g := xg. |
|
2391 |
b := xb. |
|
2392 |
] |
|
2393 |
] ifFalse:[ |
|
2394 |
r := self red. |
|
2395 |
g := self green. |
|
2396 |
b := self blue. |
|
2397 |
]. |
|
2398 |
||
2399 |
self class withHLSFromRed:r green:g blue:b do:[:xh :xl :xs | |
|
2400 |
l := xl |
|
2401 |
]. |
|
2402 |
^ l |
|
2403 |
||
2404 |
" |
|
2405 |
Color yellow light |
|
2406 |
Color yellow darkened light |
|
2407 |
" |
|
2408 |
||
2409 |
"Modified: 11.6.1996 / 17:15:24 / cg" |
|
2410 |
! |
|
2411 |
||
2412 |
magenta |
|
2413 |
"return the magenta component in percent [0..100] in cmy color space" |
|
2414 |
||
2415 |
^ 100 - self green |
|
2416 |
||
2417 |
"Modified: 11.6.1996 / 17:20:07 / cg" |
|
2418 |
"Created: 11.6.1996 / 18:30:11 / cg" |
|
2419 |
! |
|
2420 |
||
2421 |
red |
|
2422 |
"return the red component in percent [0..100]" |
|
2423 |
||
2424 |
(red isNil and:[colorId notNil]) ifTrue:[ |
|
2425 |
device getRGBFrom:colorId into:[:r :g :b | ^ r]. |
|
2426 |
]. |
|
2427 |
red isNil ifTrue:[^ 0]. |
|
2428 |
^ red * 100.0 / MaxValue |
|
2429 |
||
2430 |
"Modified: 11.6.1996 / 17:20:07 / cg" |
|
2431 |
! |
|
2432 |
||
2433 |
red:r green:g blue:b |
|
2434 |
"set r/g/b components in percent. This method will change the color lookup |
|
2435 |
table in pseudocolor devices. |
|
2436 |
This is only allowed for writable colors (i.e. those allocated with |
|
2437 |
Color>>variableColorOn: on pseudoColor displays). |
|
2438 |
Using this may make your code unportable, since it depends on a display |
|
2439 |
using palettes (i.e. it will not work on greyScale or b&w displays)." |
|
2440 |
||
2441 |
^ self scaledRed:(r * MaxValue // 100) |
|
2442 |
scaledGreen:(g * MaxValue // 100) |
|
2443 |
scaledBlue:(b * MaxValue // 100) |
|
2444 |
||
2445 |
" |
|
2446 |
|c| |
|
2447 |
||
2448 |
c := Color variableColorOn:Display. |
|
2449 |
c inspect. |
|
2450 |
(Delay forSeconds:5) wait. |
|
2451 |
c red:100 green:0 blue:0. |
|
2452 |
(Delay forSeconds:5) wait. |
|
2453 |
c red:0 green:100 blue:0. |
|
2454 |
(Delay forSeconds:5) wait. |
|
2455 |
c red:0 green:0 blue:100. |
|
2456 |
" |
|
2457 |
! |
|
2458 |
||
2459 |
redByte |
|
2460 |
"return the red components value mapped to 0..255" |
|
2461 |
||
2462 |
^ red * 255 // MaxValue |
|
2463 |
||
2464 |
" |
|
2465 |
Color red redByte |
|
2466 |
Color blue redByte |
|
2467 |
Color green redByte |
|
2468 |
Color black redByte |
|
2469 |
Color grey redByte |
|
2470 |
Color white redByte |
|
2471 |
" |
|
2472 |
||
2473 |
"Modified: 7.6.1996 / 18:31:51 / cg" |
|
2474 |
! |
|
2475 |
||
2476 |
saturation |
|
2477 |
"return the saturation (in hue/light/saturation model) in percent [0..100]. |
|
2478 |
This corresponds to the saturation setting of a color TV" |
|
2479 |
||
2480 |
|r g b s| |
|
2481 |
||
2482 |
(red isNil and:[colorId notNil]) ifTrue:[ |
|
2483 |
device getRGBFrom:colorId into:[:xr :xg :xb | |
|
2484 |
r := xr. |
|
2485 |
g := xg. |
|
2486 |
b := xb. |
|
2487 |
] |
|
2488 |
] ifFalse:[ |
|
2489 |
r := self red. |
|
2490 |
g := self green. |
|
2491 |
b := self blue. |
|
2492 |
]. |
|
2493 |
||
2494 |
self class withHLSFromRed:r green:g blue:b do:[:xh :xl :xs | |
|
2495 |
s := xs |
|
2496 |
]. |
|
2497 |
^ s |
|
2498 |
||
2499 |
" |
|
2500 |
Color yellow saturation |
|
2501 |
" |
|
2502 |
||
2503 |
"Modified: 11.6.1996 / 17:15:47 / cg" |
|
2504 |
! |
|
2505 |
||
2506 |
scaledBlue |
|
2507 |
"ST-80 compatibility: |
|
2508 |
return the blue components value mapped to 0..MaxValue" |
|
2509 |
||
2510 |
^ blue |
|
2511 |
||
2512 |
" |
|
2513 |
Color blue scaledBlue |
|
2514 |
Color black scaledBlue |
|
2515 |
Color grey scaledBlue |
|
2516 |
" |
|
2517 |
||
2518 |
"Modified: 7.6.1996 / 18:32:30 / cg" |
|
2519 |
! |
|
2520 |
||
2521 |
scaledGray |
|
2522 |
"return the grey intensity scaled to 0..MaxValue" |
|
2523 |
||
2524 |
^ ((red * 3) + (green * 6) + blue) // 10 |
|
2525 |
||
2526 |
" |
|
2527 |
Color blue scaledGray |
|
2528 |
Color black scaledGray |
|
2529 |
Color white scaledGray |
|
2530 |
Color grey scaledGray |
|
2531 |
" |
|
2532 |
||
2533 |
"Modified: 11.6.1996 / 14:43:51 / cg" |
|
2534 |
! |
|
2535 |
||
2536 |
scaledGreen |
|
2537 |
"ST-80 compatibility: |
|
2538 |
return the green components value mapped to 0..MaxValue" |
|
2539 |
||
2540 |
^ green |
|
2541 |
||
2542 |
" |
|
2543 |
Color green scaledRed |
|
2544 |
Color black scaledRed |
|
2545 |
Color grey scaledRed |
|
2546 |
" |
|
2547 |
||
2548 |
"Modified: 7.6.1996 / 18:32:38 / cg" |
|
2549 |
! |
|
2550 |
||
2551 |
scaledRed |
|
2552 |
"ST-80 compatibility: |
|
2553 |
return the red components value mapped to 0..MaxValue" |
|
2554 |
||
2555 |
^ red |
|
2556 |
||
2557 |
" |
|
2558 |
Color red scaledRed |
|
2559 |
Color black scaledRed |
|
2560 |
Color grey scaledRed |
|
2561 |
" |
|
2562 |
||
2563 |
"Modified: 7.6.1996 / 18:32:43 / cg" |
|
2564 |
! |
|
2565 |
||
2566 |
scaledRed:r scaledGreen:g scaledBlue:b |
|
2567 |
"set r/g/b components in 0..MaxValue. |
|
2568 |
This method will change the color lookup table in pseudocolor devices. |
|
2569 |
This is only allowed for writable colors (i.e. those allocated with |
|
2570 |
Color>>variableColorOn: on pseudoColor displays). |
|
2571 |
Using this may make your code unportable, since it depends on a display |
|
2572 |
using palettes (i.e. it will not work on greyScale or b&w displays)." |
|
2573 |
||
2574 |
(colorId isNil or:[red notNil]) ifTrue:[ |
|
2575 |
^ self error:'operation is not allowed for shared colors' |
|
2576 |
]. |
|
2577 |
device setColor:colorId scaledRed:r scaledGreen:g scaledBlue:b |
|
2578 |
! |
|
2579 |
||
2580 |
writable |
|
2581 |
"return true, if this is a writable colorcell" |
|
2582 |
||
2583 |
^ writable == true |
|
2584 |
! |
|
2585 |
||
2586 |
yellow |
|
2587 |
"return the yellow component in percent [0..100] in cmy color space" |
|
2588 |
||
2589 |
^ 100 - self blue |
|
2590 |
||
2591 |
"Modified: 11.6.1996 / 17:20:07 / cg" |
|
2592 |
"Created: 11.6.1996 / 18:30:20 / cg" |
|
2593 |
! ! |
|
2594 |
||
2595 |
!Color methodsFor:'binary storage'! |
|
2596 |
||
2597 |
readBinaryContentsFrom: stream manager: manager |
|
2598 |
"tell the newly restored Color about restoration" |
|
2599 |
||
2600 |
super readBinaryContentsFrom:stream manager:manager. |
|
2601 |
self postCopy |
|
2602 |
! ! |
|
2603 |
||
2604 |
!Color methodsFor:'comparing'! |
|
2605 |
||
2606 |
= aColor |
|
2607 |
"two colors are considered equal, if the color components are; |
|
2608 |
independent of the device, the color is on" |
|
2609 |
||
2610 |
aColor == self ifTrue:[^ self]. |
|
2611 |
aColor isColor ifTrue:[ |
|
2612 |
(red == aColor scaledRed) ifTrue:[ |
|
2613 |
(green == aColor scaledGreen) ifTrue:[ |
|
2614 |
(blue == aColor scaledBlue) ifTrue:[ |
|
2615 |
^ true |
|
2616 |
] |
|
2617 |
] |
|
2618 |
] |
|
2619 |
]. |
|
2620 |
^ false |
|
2621 |
! |
|
2622 |
||
2623 |
hash |
|
2624 |
"return an integer useful as hash key for the receiver. |
|
2625 |
Redefined since = is redefined" |
|
2626 |
||
2627 |
^ red + green + blue |
|
2628 |
! ! |
|
2629 |
||
2630 |
!Color methodsFor:'converting'! |
|
2631 |
||
2632 |
fromLiteralArrayEncoding:encoding |
|
2633 |
"read my values from an encoding. |
|
2634 |
The encoding is supposed to be of the form: |
|
2635 |
(#Color redPart greenPart bluePart) |
|
2636 |
This is the reverse operation to #literalArrayEncoding." |
|
2637 |
||
2638 |
red := ((encoding at:2) / 100.0 * MaxValue) rounded. |
|
2639 |
green := ((encoding at:3) / 100.0 * MaxValue) rounded. |
|
2640 |
blue := ((encoding at:4) / 100.0 * MaxValue) rounded. |
|
2641 |
||
2642 |
" |
|
2643 |
Color new fromLiteralArrayEncoding:#(#Color 50 25 25) |
|
2644 |
" |
|
2645 |
! |
|
2646 |
||
2647 |
literalArrayEncoding |
|
2648 |
"encode myself as an array, from which a copy of the receiver |
|
2649 |
can be reconstructed with #decodeAsLiteralArray. |
|
2650 |
The encoding is: |
|
2651 |
(#Color redPart greenPart bluePart) |
|
2652 |
" |
|
2653 |
||
2654 |
^ Array |
|
2655 |
with:self class name asSymbol |
|
2656 |
with:(red * 100.0 / MaxValue) |
|
2657 |
with:(green * 100.0 / MaxValue) |
|
2658 |
with:(blue * 100.0 / MaxValue) |
|
2659 |
||
2660 |
" |
|
2661 |
Color new fromLiteralArrayEncoding:#(#Color 50 25 25) |
|
2662 |
(Color red:25 green:30 blue:70) literalArrayEncoding |
|
2663 |
" |
|
2664 |
||
2665 |
"Modified: 22.4.1996 / 13:00:11 / cg" |
|
2666 |
! ! |
|
2667 |
||
2668 |
!Color methodsFor:'copying'! |
|
2669 |
||
2670 |
postCopy |
|
2671 |
"redefined to clear out any device handles in the copy" |
|
2672 |
||
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2673 |
device := colorId := ditherForm := replacementColor := nil |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2674 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2675 |
"Modified: 17.1.1997 / 00:03:42 / cg" |
1023 | 2676 |
! ! |
2677 |
||
2678 |
!Color methodsFor:'getting a device color'! |
|
2679 |
||
2680 |
exactOn:aDevice |
|
2681 |
"create a new Color representing the same color as |
|
2682 |
myself on aDevice; if one already exists, return the one. |
|
2683 |
Do not dither or otherwise approximate the color, but return |
|
2684 |
nil, if the exact color is not available. |
|
2685 |
Used to aquire primary colors for dithering, during startup." |
|
2686 |
||
2687 |
|newColor id r g b| |
|
2688 |
||
2689 |
"if Iam already assigned to that device ..." |
|
2690 |
(device == aDevice and:[ditherForm isNil]) ifTrue:[^ self]. |
|
2691 |
||
2692 |
r := red. |
|
2693 |
g := green. |
|
2694 |
b := blue. |
|
2695 |
||
1239
f4bd3bc9f3f9
oops - dont round when searching for existing colors
Claus Gittinger <cg@exept.de>
parents:
1238
diff
changeset
|
2696 |
"first look if not already there" |
f4bd3bc9f3f9
oops - dont round when searching for existing colors
Claus Gittinger <cg@exept.de>
parents:
1238
diff
changeset
|
2697 |
newColor := Color existingColorScaledRed:r scaledGreen:g scaledBlue:b on:aDevice. |
f4bd3bc9f3f9
oops - dont round when searching for existing colors
Claus Gittinger <cg@exept.de>
parents:
1238
diff
changeset
|
2698 |
(newColor notNil and:[newColor ditherForm isNil]) ifTrue:[^ newColor]. |
f4bd3bc9f3f9
oops - dont round when searching for existing colors
Claus Gittinger <cg@exept.de>
parents:
1238
diff
changeset
|
2699 |
|
1023 | 2700 |
r := (r bitAnd:16rFF00) bitOr:(r bitShift:-8). |
2701 |
g := (g bitAnd:16rFF00) bitOr:(g bitShift:-8). |
|
2702 |
b := (b bitAnd:16rFF00) bitOr:(b bitShift:-8). |
|
2703 |
||
2704 |
"ask that device for the color" |
|
2705 |
id := aDevice colorScaledRed:r scaledGreen:g scaledBlue:b. |
|
2706 |
id isNil ifTrue:[ |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2707 |
"/ this is a kludge: scavenge to free unused colors |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2708 |
"/ and try again ... |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2709 |
ObjectMemory scavenge; finalize. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2710 |
id := aDevice colorScaledRed:r scaledGreen:g scaledBlue:b |
1023 | 2711 |
]. |
2712 |
id isNil ifTrue:[ |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2713 |
"no such color - fail" |
1023 | 2714 |
|
2715 |
"/ 'COLOR: no color for ' infoPrint. self displayString infoPrintCR. |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2716 |
^ nil |
1023 | 2717 |
]. |
2718 |
||
2719 |
"receiver was not associated - do it now" |
|
2720 |
device isNil ifTrue:[ |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2721 |
device := aDevice. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2722 |
colorId := id. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2723 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2724 |
aDevice visualType ~~ #TrueColor ifTrue:[ |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2725 |
Lobby register:self. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2726 |
]. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2727 |
^ self |
1023 | 2728 |
]. |
2729 |
||
2730 |
"receiver was already associated to another device - need a new color" |
|
2731 |
newColor := (self class basicNew) setScaledRed:r scaledGreen:g scaledBlue:b device:aDevice. |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2732 |
newColor setColorId:id. |
1023 | 2733 |
aDevice visualType ~~ #TrueColor ifTrue:[ |
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2734 |
Lobby register:newColor. |
1023 | 2735 |
]. |
2736 |
^ newColor |
|
2737 |
||
1239
f4bd3bc9f3f9
oops - dont round when searching for existing colors
Claus Gittinger <cg@exept.de>
parents:
1238
diff
changeset
|
2738 |
"Modified: 17.1.1997 / 00:15:35 / cg" |
1023 | 2739 |
! |
2740 |
||
2741 |
nearestOn:aDevice |
|
2742 |
"create a new Color representing the same color as myself on aDevice; |
|
2743 |
if one already exists, return the one. If no exact match is found, |
|
2744 |
search for the nearest match" |
|
2745 |
||
2746 |
|newColor id| |
|
2747 |
||
2748 |
"if I'am already assigned to that device ..." |
|
2749 |
(device == aDevice) ifTrue:[^ self]. |
|
2750 |
||
2751 |
"first look if not already there" |
|
2752 |
newColor := Color nearestColorScaledRed:red scaledGreen:green scaledBlue:blue on:aDevice. |
|
2753 |
newColor notNil ifTrue:[^ newColor]. |
|
2754 |
||
2755 |
"ask that device for the color" |
|
2756 |
id := aDevice colorScaledRed:red scaledGreen:green scaledBlue:blue. |
|
2757 |
id isNil ifTrue:[ |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2758 |
"this is a kludge: |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2759 |
scavenge to possuby free unused colors and try again ... |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2760 |
this is a compromise: actually a full GC is required here, |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2761 |
but that is too expensive. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2762 |
" |
1023 | 2763 |
" " |
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2764 |
ObjectMemory scavenge; finalize. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2765 |
id := aDevice colorScaledRed:red scaledGreen:green scaledBlue:blue |
1023 | 2766 |
" " |
2767 |
]. |
|
2768 |
id isNil ifTrue:[ |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2769 |
"no color - fail" |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2770 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2771 |
^ nil |
1023 | 2772 |
]. |
2773 |
||
2774 |
"receiver was not associated - do it now" |
|
2775 |
device isNil ifTrue:[ |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2776 |
device := aDevice. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2777 |
colorId := id. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2778 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2779 |
aDevice visualType ~~ #TrueColor ifTrue:[ |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2780 |
Lobby register:self. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2781 |
]. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2782 |
^ self |
1023 | 2783 |
]. |
2784 |
||
2785 |
"receiver was already associated to another device - need a new color" |
|
2786 |
newColor := (self class basicNew) setScaledRed:red scaledGreen:green sclaedBlue:blue device:aDevice. |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2787 |
newColor setColorId:id. |
1023 | 2788 |
aDevice visualType ~~ #TrueColor ifTrue:[ |
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2789 |
Lobby register:newColor. |
1023 | 2790 |
]. |
2791 |
^ newColor |
|
2792 |
||
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2793 |
"Modified: 17.1.1997 / 00:06:56 / cg" |
1023 | 2794 |
! |
2795 |
||
2796 |
on:aDevice |
|
2797 |
"create a new Color representing the same color as |
|
2798 |
myself on aDevice; if one already exists, return the one" |
|
2799 |
||
2800 |
|newColor id grey form |
|
2801 |
greyV "{ Class: SmallInteger }" |
|
2802 |
rV "{ Class: SmallInteger }" |
|
2803 |
gV "{ Class: SmallInteger }" |
|
2804 |
bV "{ Class: SmallInteger }" |
|
1144 | 2805 |
deviceVisual deviceFixColors| |
1023 | 2806 |
|
1233 | 2807 |
"/ the most common cases (already allocated) first |
2808 |
||
2809 |
colorId notNil ifTrue:[ |
|
2810 |
||
2811 |
"/ is someone validating me before drawing on aDevice ? |
|
2812 |
||
2813 |
aDevice notNil ifTrue:[ |
|
2814 |
aDevice == device ifTrue:[ |
|
1144 | 2815 |
^ self |
2816 |
] |
|
1233 | 2817 |
]. |
2818 |
||
2819 |
"/ a special case for pseudo-colors (0 and 1 in bitmaps) |
|
2820 |
"/ those have nil r/g/b values, but a nonNil colorId |
|
2821 |
||
2822 |
red isNil ifTrue:[^ self]. |
|
2823 |
||
2824 |
"/ want to release color ? |
|
2825 |
||
2826 |
(aDevice isNil and:[device notNil]) ifTrue:[ |
|
2827 |
"/ trueColor device-colors are not registered |
|
2828 |
device visualType ~~ #TrueColor ifTrue:[ |
|
1205 | 2829 |
Lobby unregister:self. |
2830 |
device freeColor:colorId |
|
2831 |
]. |
|
1233 | 2832 |
device := nil. |
2833 |
colorId := nil. |
|
2834 |
^ self |
|
1205 | 2835 |
]. |
2836 |
]. |
|
2837 |
||
1233 | 2838 |
"/ on high-resolution true-color systems, dont care for dithering and/or |
1023 | 2839 |
"/ especially freeing colors |
2840 |
"/ (no need to remember in Lobby) |
|
2841 |
||
2842 |
(deviceVisual := aDevice visualType) == #TrueColor ifTrue:[ |
|
1144 | 2843 |
aDevice depth >= 15 ifTrue:[ |
2844 |
id := aDevice colorScaledRed:red scaledGreen:green scaledBlue:blue. |
|
2845 |
id notNil ifTrue:[ |
|
2846 |
device isNil ifTrue:[ |
|
2847 |
colorId := id. |
|
2848 |
ditherForm := nil. |
|
2849 |
^ self |
|
1233 | 2850 |
]. |
2851 |
newColor := (self class basicNew) |
|
2852 |
setScaledRed:red |
|
2853 |
scaledGreen:green |
|
2854 |
scaledBlue:blue |
|
2855 |
device:aDevice. |
|
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
2856 |
newColor setColorId:id. |
1233 | 2857 |
^ newColor |
1144 | 2858 |
] |
2859 |
] |
|
1023 | 2860 |
]. |
2861 |
||
2862 |
"/ round a bit within 1% in red & green, 2% in blue |
|
2863 |
||
1239
f4bd3bc9f3f9
oops - dont round when searching for existing colors
Claus Gittinger <cg@exept.de>
parents:
1238
diff
changeset
|
2864 |
rV := red. |
f4bd3bc9f3f9
oops - dont round when searching for existing colors
Claus Gittinger <cg@exept.de>
parents:
1238
diff
changeset
|
2865 |
gV := green. |
f4bd3bc9f3f9
oops - dont round when searching for existing colors
Claus Gittinger <cg@exept.de>
parents:
1238
diff
changeset
|
2866 |
bV := blue. |
f4bd3bc9f3f9
oops - dont round when searching for existing colors
Claus Gittinger <cg@exept.de>
parents:
1238
diff
changeset
|
2867 |
|
f4bd3bc9f3f9
oops - dont round when searching for existing colors
Claus Gittinger <cg@exept.de>
parents:
1238
diff
changeset
|
2868 |
"/ rV := (red / 100.0) rounded * 100. |
f4bd3bc9f3f9
oops - dont round when searching for existing colors
Claus Gittinger <cg@exept.de>
parents:
1238
diff
changeset
|
2869 |
"/ gV := (green / 100.0) rounded * 100. |
f4bd3bc9f3f9
oops - dont round when searching for existing colors
Claus Gittinger <cg@exept.de>
parents:
1238
diff
changeset
|
2870 |
"/ bV := (blue / 50.0) rounded * 50. |
1023 | 2871 |
|
1233 | 2872 |
"/ if I am already assigned to that device ... |
2873 |
||
2874 |
((device == aDevice) and:[ditherForm notNil]) ifTrue:[ |
|
1023 | 2875 |
|
1144 | 2876 |
"/ mhmh - if I was dithered the last time (not enough colors then) |
2877 |
"/ try again - maybe some colors were reclaimed in the meanwhile |
|
2878 |
||
1233 | 2879 |
deviceFixColors := aDevice fixColors. |
2880 |
||
2881 |
(deviceFixColors isNil |
|
2882 |
and:[RetryAllocation]) ifTrue:[ |
|
2883 |
" |
|
2884 |
but there is no chance on b&w displays - so don't try |
|
2885 |
" |
|
1144 | 2886 |
aDevice depth > 2 ifTrue:[ |
2887 |
id := aDevice colorScaledRed:rV scaledGreen:gV scaledBlue:bV. |
|
2888 |
id notNil ifTrue:[ |
|
2889 |
colorId := id. |
|
2890 |
ditherForm := nil. |
|
2891 |
Lobby register:self |
|
2892 |
] |
|
2893 |
] |
|
2894 |
]. |
|
2895 |
^ self |
|
1023 | 2896 |
]. |
2897 |
||
2898 |
newColor := Color existingColorScaledRed:rV scaledGreen:gV scaledBlue:bV on:aDevice. |
|
2899 |
newColor notNil ifTrue:[^ newColor]. |
|
2900 |
||
2901 |
"/ |
|
2902 |
"/ ok, we are going to dither that color. |
|
2903 |
"/ if its 'almost' grey, make it grey and round it a bit (1%) |
|
2904 |
"/ |
|
2905 |
greyV := (3 * red) + (6 * green) + (1 * blue). |
|
2906 |
greyV := (greyV / 1000.0) rounded * 10. |
|
2907 |
||
2908 |
"/ allow an error of 1% in red & green, 2% in blue |
|
2909 |
||
2910 |
((rV - greyV) abs <= 655 "/ MaxValue // 100 |
|
2911 |
and:[(gV - greyV) abs <= 655 "/ MaxValue // 100 |
|
2912 |
and:[(bV - greyV) abs <= 1310]]) ifTrue:[ "/ MaxValue // 100 * 2 |
|
1144 | 2913 |
rV := gV := bV := greyV. |
1023 | 2914 |
] ifFalse:[ |
1144 | 2915 |
rV := red. gV := green. bV := blue. |
1023 | 2916 |
]. |
2917 |
||
2918 |
aDevice hasColors ifTrue:[ |
|
1144 | 2919 |
deviceFixColors isNil ifTrue:[ |
2920 |
"/ ask that device for the exact color |
|
2921 |
||
2922 |
id := aDevice colorScaledRed:rV scaledGreen:gV scaledBlue:bV. |
|
2923 |
id isNil ifTrue:[ |
|
2924 |
"/ this is a kludge: scavenge to free unused colors |
|
2925 |
"/ and try again ... |
|
2926 |
ObjectMemory scavenge; finalize. |
|
2927 |
id := aDevice colorScaledRed:rV scaledGreen:gV scaledBlue:bV |
|
2928 |
]. |
|
2929 |
||
2930 |
id isNil ifTrue:[ |
|
2931 |
"/ no such color - try color dithers |
|
2932 |
||
2933 |
self ditherRed:rV green:gV blue:bV on:aDevice |
|
2934 |
into:[:c :f | newColor := c. form := f]. |
|
2935 |
newColor notNil ifTrue:[^ newColor]. |
|
2936 |
]. |
|
2937 |
]. |
|
2938 |
||
2939 |
"/ none found ? -> do a hard dither using fixColors |
|
2940 |
||
2941 |
(id isNil and:[form isNil]) ifTrue:[ |
|
2942 |
(deviceFixColors notNil "and:[aDevice == Display]") ifTrue:[ |
|
2943 |
self fixDitherRed:rV green:gV blue:bV on:aDevice |
|
2944 |
into:[:c :f | newColor := c. form := f]. |
|
2945 |
newColor notNil ifTrue:[^ newColor]. |
|
2946 |
] |
|
2947 |
]. |
|
2948 |
||
2949 |
"/ still none found ? -> do a very hard dither using existing colors |
|
2950 |
||
2951 |
(id isNil and:[form isNil]) ifTrue:[ |
|
2952 |
self complexDitherRed:rV green:gV blue:bV on:aDevice |
|
2953 |
into:[:c :f | newColor := c. form := f]. |
|
2954 |
newColor notNil ifTrue:[^ newColor]. |
|
2955 |
]. |
|
1023 | 2956 |
]. |
2957 |
||
2958 |
(id isNil and:[form isNil]) ifTrue:[ |
|
1144 | 2959 |
"still no result - try greying" |
2960 |
||
2961 |
greyV == 0 ifTrue:[ |
|
2962 |
id := aDevice blackpixel |
|
2963 |
] ifFalse:[ |
|
2964 |
greyV == MaxValue ifTrue:[ |
|
2965 |
id := aDevice whitepixel |
|
2966 |
] ifFalse:[ |
|
2967 |
aDevice hasGrayscales ifTrue:[ |
|
2968 |
self ditherGrayFor:(greyV / MaxValue) |
|
2969 |
on:aDevice |
|
2970 |
into:[:c :f | newColor := c. form := f]. |
|
2971 |
newColor notNil ifTrue:[^ newColor]. |
|
2972 |
]. |
|
2973 |
] |
|
2974 |
]. |
|
1023 | 2975 |
]. |
2976 |
||
2977 |
device isNil ifTrue:[ |
|
1144 | 2978 |
"/ receiver was not associated - do it now & return mySelf |
2979 |
||
2980 |
device := aDevice. |
|
2981 |
id isNil ifTrue:[ |
|
2982 |
ditherForm := form |
|
2983 |
]. |
|
2984 |
colorId := id. |
|
2985 |
||
2986 |
"/ have to tell Lobby - otherwise it keeps old info around |
|
2987 |
||
2988 |
id notNil ifTrue:[ |
|
2989 |
deviceVisual ~~ #TrueColor ifTrue:[ |
|
2990 |
Lobby register:self |
|
2991 |
] |
|
2992 |
]. |
|
2993 |
^ self |
|
1023 | 2994 |
]. |
2995 |
||
2996 |
"/ receiver was already associated to another device |
|
2997 |
"/ - need a new color and return it |
|
2998 |
||
2999 |
newColor := (self class basicNew) |
|
1144 | 3000 |
setScaledRed:red |
3001 |
scaledGreen:green |
|
3002 |
scaledBlue:blue |
|
3003 |
device:aDevice. |
|
1023 | 3004 |
id isNil ifTrue:[ |
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3005 |
newColor setDitherForm:form |
1023 | 3006 |
] ifFalse:[ |
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3007 |
newColor setColorId:id. |
1233 | 3008 |
|
3009 |
"/ trueColor device-colors are not registered |
|
1144 | 3010 |
deviceVisual ~~ #TrueColor ifTrue:[ |
3011 |
Lobby register:newColor. |
|
3012 |
] |
|
1023 | 3013 |
]. |
3014 |
^ newColor |
|
3015 |
||
3016 |
"Created: 16.11.1995 / 20:16:42 / cg" |
|
1239
f4bd3bc9f3f9
oops - dont round when searching for existing colors
Claus Gittinger <cg@exept.de>
parents:
1238
diff
changeset
|
3017 |
"Modified: 17.1.1997 / 00:15:19 / cg" |
1023 | 3018 |
! ! |
3019 |
||
3020 |
!Color methodsFor:'inspecting'! |
|
3021 |
||
3022 |
inspectorClass |
|
3023 |
"return the class of an appropriate inspector. |
|
3024 |
ST/X has a specialized ColorInspectorView for that" |
|
3025 |
||
3026 |
^ ColorInspectorView |
|
3027 |
||
3028 |
"Modified: 23.4.1996 / 13:39:50 / cg" |
|
3029 |
! ! |
|
3030 |
||
3031 |
!Color methodsFor:'instance creation'! |
|
3032 |
||
3033 |
asHiliteColor |
|
3034 |
"same as lightened - for ST-80 compatibility" |
|
3035 |
||
3036 |
^ self lightened |
|
3037 |
! |
|
3038 |
||
3039 |
asShadowColor |
|
3040 |
"same as darkened - for ST-80 compatibility" |
|
3041 |
||
3042 |
^ self darkened |
|
3043 |
! |
|
3044 |
||
3045 |
blendWith:aColor |
|
3046 |
"create a new color from equally mixing the receiver |
|
3047 |
and the argument, aColor. |
|
3048 |
Mixing is done by adding components |
|
3049 |
(which is different from mixing colors on paper ..)" |
|
3050 |
||
3051 |
red isNil ifTrue:[ |
|
1326
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3052 |
^ aColor |
1023 | 3053 |
]. |
3054 |
||
3055 |
^ (self class) |
|
1326
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3056 |
scaledRed:(red + aColor scaledRed) // 2 |
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3057 |
scaledGreen:(green + aColor scaledGreen) // 2 |
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3058 |
scaledBlue:(blue + aColor scaledBlue) // 2 |
1023 | 3059 |
|
3060 |
" |
|
3061 |
(Color red) blendWith:(Color yellow) |
|
3062 |
(Color red) blendWith:(Color blue) |
|
1326
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3063 |
(Color red) blendWith:(Color black) |
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3064 |
(Color red) blendWith:(Color white) |
1023 | 3065 |
" |
3066 |
||
1326
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3067 |
"Modified: 10.2.1997 / 22:08:14 / cg" |
1023 | 3068 |
! |
3069 |
||
3070 |
darkened |
|
3071 |
"return a new color, which is slightly darker than the receiver" |
|
3072 |
||
3073 |
^ self blendWith:Black |
|
3074 |
||
3075 |
" |
|
3076 |
(Color red) darkened |
|
3077 |
(Color red) darkened darkened |
|
3078 |
" |
|
3079 |
||
3080 |
"Modified: 11.6.1996 / 18:10:37 / cg" |
|
3081 |
! |
|
3082 |
||
3083 |
lightened |
|
3084 |
"return a new color, which is slightly lighter than the receiver" |
|
3085 |
||
3086 |
^ self blendWith:White |
|
3087 |
||
3088 |
" |
|
3089 |
(Color red) lightened |
|
3090 |
(Color red) lightened lightened |
|
3091 |
" |
|
3092 |
||
3093 |
"Modified: 11.6.1996 / 18:10:49 / cg" |
|
3094 |
! ! |
|
3095 |
||
3096 |
!Color methodsFor:'instance release'! |
|
3097 |
||
3098 |
shallowCopyForFinalization |
|
3099 |
"redefined, since for finalization only device and colorIndex |
|
3100 |
are needed - thus a faster copy is possible here" |
|
3101 |
||
3102 |
|aCopy| |
|
3103 |
||
1234 | 3104 |
aCopy := DeviceColorHandle basicNew. |
1023 | 3105 |
aCopy setDevice:device colorId:colorId. |
3106 |
^ aCopy |
|
3107 |
! ! |
|
3108 |
||
3109 |
!Color methodsFor:'printing & storing'! |
|
3110 |
||
3111 |
printOn:aStream |
|
3112 |
"append a string representing of the receiver |
|
3113 |
to the argument, aStream" |
|
3114 |
||
3115 |
self storeOn:aStream |
|
3116 |
! |
|
3117 |
||
3118 |
storeOn:aStream |
|
3119 |
"append a string representing an expression to reconstruct the receiver |
|
3120 |
to the argument, aStream" |
|
3121 |
||
3122 |
red isNil ifTrue:[ |
|
3123 |
colorId notNil ifTrue:[ |
|
3124 |
aStream nextPutAll:'(Color colorId:'. |
|
3125 |
colorId storeOn:aStream. |
|
3126 |
aStream nextPut:$). |
|
3127 |
^ self |
|
3128 |
] |
|
3129 |
]. |
|
3130 |
(red == green and:[red == blue]) ifTrue:[ |
|
3131 |
red == 0 ifTrue:[ |
|
3132 |
aStream nextPutAll:'(Color black)'. |
|
3133 |
] ifFalse:[ |
|
3134 |
red == MaxValue ifTrue:[ |
|
3135 |
aStream nextPutAll:'(Color white)'. |
|
3136 |
] ifFalse:[ |
|
3137 |
aStream nextPutAll:'(Color grey:'. |
|
3138 |
(self red) storeOn:aStream. |
|
3139 |
aStream nextPut:$). |
|
3140 |
] |
|
3141 |
]. |
|
3142 |
^ self |
|
3143 |
]. |
|
3144 |
aStream nextPutAll:'(Color red:'. |
|
3145 |
(self red) storeOn:aStream. |
|
3146 |
aStream nextPutAll:' green:'. |
|
3147 |
(self green) storeOn:aStream. |
|
3148 |
aStream nextPutAll:' blue:'. |
|
3149 |
(self blue) storeOn:aStream. |
|
3150 |
aStream nextPut:$). |
|
3151 |
! ! |
|
3152 |
||
3153 |
!Color methodsFor:'private'! |
|
3154 |
||
3155 |
complexDitherRed:red green:green blue:blue on:aDevice into:aBlock |
|
3156 |
"get a deep dither form for an rgb value. |
|
3157 |
Use all available colors for error dithering into a form." |
|
3158 |
||
3159 |
|errR errG errB f wantR wantG wantB clr |
|
3160 |
dir "{ Class: SmallInteger }" |
|
3161 |
start "{ Class: SmallInteger }" |
|
3162 |
end "{ Class: SmallInteger }" |
|
3163 |
map| |
|
3164 |
||
3165 |
errR := 0. |
|
3166 |
errG := 0. |
|
3167 |
errB := 0. |
|
3168 |
||
3169 |
"get a form and clear it" |
|
3170 |
f := Form width:4 height:4 depth:(aDevice depth) on:aDevice. |
|
3171 |
map := IdentityDictionary new. |
|
3172 |
||
3173 |
0 to:3 do:[:x | |
|
3174 |
x even ifTrue:[ |
|
3175 |
dir := 1. |
|
3176 |
start := 0. |
|
3177 |
end := 3. |
|
3178 |
] ifFalse:[ |
|
3179 |
dir := -1. |
|
3180 |
start := 3. |
|
3181 |
end := 0. |
|
3182 |
]. |
|
3183 |
start to:end by:dir do:[:y | |
|
3184 |
wantR := red + errR. |
|
3185 |
wantR > MaxValue ifTrue:[ |
|
3186 |
wantR := MaxValue |
|
3187 |
] ifFalse:[ wantR < 0 ifTrue:[ |
|
3188 |
wantR := 0 |
|
3189 |
]]. |
|
3190 |
||
3191 |
wantG := green + errG. |
|
3192 |
wantG > MaxValue ifTrue:[ |
|
3193 |
wantG := MaxValue |
|
3194 |
] ifFalse:[ wantG < 0 ifTrue:[ |
|
3195 |
wantG := 0 |
|
3196 |
]]. |
|
3197 |
||
3198 |
wantB := blue + errB. |
|
3199 |
wantB > MaxValue ifTrue:[ |
|
3200 |
wantB := MaxValue |
|
3201 |
] ifFalse:[ wantB < 0 ifTrue:[ |
|
3202 |
wantB := 0 |
|
3203 |
]]. |
|
3204 |
||
3205 |
"find the nearest color" |
|
3206 |
||
3207 |
" " |
|
3208 |
clr := Color quickNearestColorScaledRed:wantR scaledGreen:wantG scaledBlue:wantB on:aDevice. |
|
3209 |
" " |
|
3210 |
" |
|
3211 |
clr := Color nearestColorScaledRed:wantR green:wantG blue:wantB on:aDevice. |
|
3212 |
" |
|
3213 |
clr isNil ifTrue:[ |
|
3214 |
clr := Color scaledRed:wantR scaledGreen:wantG scaledBlue:wantB. |
|
3215 |
clr brightness > 0.5 ifTrue:[ |
|
3216 |
clr := Color white on:aDevice |
|
3217 |
] ifFalse:[ |
|
3218 |
clr := Color black on:aDevice |
|
3219 |
] |
|
3220 |
" |
|
3221 |
^ aBlock value:nil value:nil |
|
3222 |
" |
|
3223 |
]. |
|
3224 |
||
3225 |
f paint:clr. |
|
3226 |
f displayPointX:x y:y. |
|
3227 |
map at:clr colorId + 1 put:clr. |
|
3228 |
||
3229 |
"compute the new error" |
|
3230 |
errR := wantR - clr scaledRed. |
|
3231 |
errG := wantG - clr scaledGreen. |
|
3232 |
errB := wantB - clr scaledBlue. |
|
3233 |
]. |
|
3234 |
]. |
|
3235 |
||
3236 |
f colorMap:map. |
|
3237 |
" |
|
3238 |
'hard dither' printNewline. |
|
3239 |
" |
|
3240 |
^ aBlock value:nil value:f |
|
3241 |
||
3242 |
"Modified: 14.6.1996 / 20:13:39 / cg" |
|
3243 |
! |
|
3244 |
||
3245 |
ditherGrayFor:fraction on:aDevice into:aBlock |
|
3246 |
"get a dither form or colorId for a brightness value. |
|
3247 |
Returns 2 values (either color or ditherForm) through aBlock." |
|
3248 |
||
3249 |
|d nGray grayBelow scaledGrey scaledGray1 scaledGray2 clr1 clr2 newFraction step| |
|
3250 |
||
3251 |
d := aDevice depth. |
|
3252 |
||
3253 |
"/ special code for b&w displays |
|
3254 |
||
3255 |
d == 1 ifTrue:[ |
|
3256 |
aDevice blackpixel == 0 ifTrue:[ |
|
3257 |
clr1 := Black. |
|
3258 |
clr2 := White. |
|
3259 |
newFraction := fraction. |
|
3260 |
] ifFalse:[ |
|
3261 |
clr1 := White. |
|
3262 |
clr2 := Black. |
|
3263 |
newFraction := 1 - fraction |
|
3264 |
] |
|
3265 |
] ifFalse:[ |
|
3266 |
"/ special code for 2-plane displays (NeXT) |
|
3267 |
||
3268 |
d == 2 ifTrue:[ |
|
3269 |
fraction <= 0.01 ifTrue:[ |
|
3270 |
clr1 := Black exactOn:aDevice |
|
3271 |
] ifFalse:[ |
|
3272 |
(fraction between:0.32 and:0.34) ifTrue:[ |
|
3273 |
clr1 := (Color gray:33) exactOn:aDevice |
|
3274 |
] ifFalse:[ |
|
3275 |
(fraction between:0.66 and:0.68) ifTrue:[ |
|
3276 |
clr1 := (Color gray:67) exactOn:aDevice |
|
3277 |
] ifFalse:[ |
|
3278 |
fraction >= 0.99 ifTrue:[ |
|
3279 |
clr1 := White exactOn:aDevice |
|
3280 |
] |
|
3281 |
] |
|
3282 |
] |
|
3283 |
]. |
|
3284 |
clr1 notNil ifTrue:[ |
|
3285 |
^ aBlock value:clr1 value:nil |
|
3286 |
]. |
|
3287 |
||
3288 |
(fraction between:0 and:0.33) ifTrue:[ |
|
3289 |
clr1 := Black. |
|
3290 |
clr2 := Color gray:33. |
|
3291 |
] ifFalse:[ |
|
3292 |
(fraction between:0.34 and:0.66) ifTrue:[ |
|
3293 |
clr1 := Color gray:33. |
|
3294 |
clr2 := Color gray:67. |
|
3295 |
] ifFalse:[ |
|
3296 |
clr1 := Color gray:67. |
|
3297 |
clr2 := White. |
|
3298 |
] |
|
3299 |
]. |
|
3300 |
scaledGray1 := clr1 scaledRed. |
|
3301 |
scaledGray2 := clr2 scaledRed. |
|
3302 |
||
3303 |
scaledGrey := (MaxValue * fraction) rounded. |
|
3304 |
||
3305 |
newFraction := (scaledGrey - scaledGray1) asFloat / (scaledGray2 - scaledGray1). |
|
3306 |
] ifFalse:[ |
|
3307 |
nGray := (1 bitShift:d) - 1. |
|
3308 |
||
3309 |
"/ scale greyValue into grey levels |
|
3310 |
||
3311 |
grayBelow := (fraction * nGray) truncated. |
|
3312 |
||
3313 |
grayBelow < 0 ifTrue:[ |
|
3314 |
^ Color black exactOn:aDevice |
|
3315 |
]. |
|
3316 |
grayBelow >= nGray ifTrue:[ |
|
3317 |
^ Color white exactOn:aDevice |
|
3318 |
]. |
|
3319 |
||
3320 |
scaledGrey := (MaxValue * fraction) rounded. |
|
3321 |
||
3322 |
step := MaxValue // nGray. |
|
3323 |
scaledGray1 := grayBelow * step. |
|
3324 |
scaledGray2 := scaledGray1 + step. |
|
3325 |
||
3326 |
clr1 := Color scaledGray:scaledGray1. |
|
3327 |
clr2 := Color scaledGray:scaledGray2. |
|
3328 |
||
3329 |
"/ scale remainder in between low..high |
|
3330 |
newFraction := (scaledGrey - scaledGray1) asFloat / (scaledGray2 - scaledGray1). |
|
3331 |
||
3332 |
"/ dither between those two colors |
|
3333 |
]. |
|
3334 |
]. |
|
3335 |
clr1 := clr1 exactOn:aDevice. |
|
3336 |
clr2 := clr2 exactOn:aDevice. |
|
3337 |
||
3338 |
^ self monoDitherFor:newFraction |
|
3339 |
between:clr1 and:clr2 |
|
3340 |
on:aDevice into:aBlock |
|
3341 |
||
3342 |
" |
|
3343 |
Color basicNew |
|
3344 |
ditherGrayFor:0.5 |
|
3345 |
on:Display |
|
3346 |
into:[:clr :form | clr notNil ifTrue:[clr inspect]. |
|
3347 |
form notNil ifTrue:[(form magnifiedBy:16) inspect].] |
|
3348 |
" |
|
3349 |
" |
|
3350 |
Color basicNew |
|
3351 |
ditherGrayFor:0.25 |
|
3352 |
on:Display |
|
3353 |
into:[:clr :form | clr notNil ifTrue:[clr inspect]. |
|
3354 |
form notNil ifTrue:[(form magnifiedBy:16) inspect].] |
|
3355 |
" |
|
3356 |
||
3357 |
"Modified: 11.6.1996 / 17:08:14 / cg" |
|
3358 |
! |
|
3359 |
||
3360 |
ditherRed:rV green:gV blue:bV on:aDevice into:aBlock |
|
3361 |
"get a dither form or colorId for an rgb value. |
|
3362 |
Returns 2 values (either color or ditherForm) through |
|
3363 |
aBlock. |
|
3364 |
This code is just a minimum of what is really needed, |
|
3365 |
and needs much more work. Currently only some special cases |
|
3366 |
are handled" |
|
3367 |
||
3368 |
|rh rl rs |
|
3369 |
lowL hiL lowValL hiValL lowS hiS lowValS hiValS lowH hiH lowValH hiValH d| |
|
3370 |
||
3371 |
"get hls (since we dither anyway, round them a bit" |
|
3372 |
||
3373 |
Color withHLSFromScaledRed:rV scaledGreen:gV scaledBlue:bV do:[:h :l :s | |
|
3374 |
h notNil ifTrue:[ |
|
3375 |
rh := (h * 3.0) rounded / 3.0. |
|
3376 |
]. |
|
3377 |
rl := (l * 3.0) rounded / 3.0. |
|
3378 |
rs := (s * 3.0) rounded / 3.0. |
|
3379 |
]. |
|
3380 |
||
3381 |
rh isNil ifTrue:[ |
|
3382 |
"achromatic, dither between achromatic colors" |
|
3383 |
||
3384 |
lowL := nil. |
|
3385 |
hiL := nil. |
|
3386 |
||
3387 |
"find the 2 bounding colors" |
|
3388 |
Lobby do:[:aColor | |
|
3389 |
aColor colorId notNil ifTrue:[ |
|
3390 |
||
3391 |
Color withHLSFromRed:aColor red green:aColor green blue:aColor blue do:[:h :l :s | |
|
3392 |
| cl | |
|
3393 |
||
3394 |
h isNil ifTrue:[ |
|
3395 |
cl := (l * 3.0) rounded / 3.0. |
|
3396 |
||
3397 |
cl > rl ifTrue:[ |
|
3398 |
hiL isNil ifTrue:[ |
|
3399 |
hiL := aColor. |
|
3400 |
hiValL := cl. |
|
3401 |
] ifFalse:[ |
|
3402 |
cl < hiValL ifTrue:[ |
|
3403 |
hiL := aColor. |
|
3404 |
hiValL := cl. |
|
3405 |
] |
|
3406 |
] |
|
3407 |
] ifFalse:[ |
|
3408 |
lowL isNil ifTrue:[ |
|
3409 |
lowL := aColor. |
|
3410 |
lowValL := cl |
|
3411 |
] ifFalse:[ |
|
3412 |
cl > lowValL ifTrue:[ |
|
3413 |
lowL := aColor. |
|
3414 |
lowValL := cl |
|
3415 |
] |
|
3416 |
] |
|
3417 |
] |
|
3418 |
] |
|
3419 |
] |
|
3420 |
] |
|
3421 |
]. |
|
3422 |
||
3423 |
(lowL notNil and:[hiL notNil]) ifTrue:[ |
|
3424 |
^ self monoDitherFor:1.0 / (hiValL - lowValL) * (rl - lowValL) |
|
3425 |
between:lowL |
|
3426 |
and:hiL |
|
3427 |
on:aDevice |
|
3428 |
into:aBlock |
|
3429 |
]. |
|
3430 |
"cannot happen, should always find at least black and white" |
|
3431 |
self error:'cannot happen'. |
|
3432 |
^ aBlock value:nil value:nil |
|
3433 |
]. |
|
3434 |
||
3435 |
"chromatic case" |
|
3436 |
||
3437 |
aDevice hasColors ifFalse:[ |
|
3438 |
"no chance, return nil values" |
|
3439 |
^ aBlock value:nil value:nil |
|
3440 |
]. |
|
3441 |
(Red isNil or:[Green isNil or:[Blue isNil]]) ifTrue:[ |
|
3442 |
"if we where not able to get primary colors: no chance" |
|
3443 |
^ aBlock value:nil value:nil |
|
3444 |
]. |
|
3445 |
||
3446 |
"try to find two bounding colors with same hue and saturation; |
|
3447 |
dither on light between those" |
|
3448 |
||
3449 |
lowL := nil. |
|
3450 |
hiL := nil. |
|
3451 |
lowS := nil. |
|
3452 |
hiS := nil. |
|
3453 |
lowH := nil. |
|
3454 |
hiH := nil. |
|
3455 |
||
3456 |
Lobby do:[:aColor | |
|
3457 |
aColor colorId notNil ifTrue:[ |
|
3458 |
Color withHLSFromRed:aColor red green:aColor green blue:aColor blue do:[:h :l :s | |
|
3459 |
| cl ch cs| |
|
3460 |
||
3461 |
h notNil ifTrue:[ |
|
3462 |
ch := (h * 3.0) rounded / 3.0. |
|
3463 |
] ifFalse:[ |
|
3464 |
ch := nil |
|
3465 |
]. |
|
3466 |
cl := (l * 3.0) rounded / 3.0. |
|
3467 |
cs := (s * 3.0) rounded / 3.0. |
|
3468 |
||
3469 |
((ch = rh) and:[cs = rs]) ifTrue:[ |
|
3470 |
"found a color with same saturation and same hue, keep for light" |
|
3471 |
||
3472 |
cl > rl ifTrue:[ |
|
3473 |
hiL isNil ifTrue:[ |
|
3474 |
hiL := aColor. |
|
3475 |
hiValL := cl |
|
3476 |
] ifFalse:[ |
|
3477 |
cl < hiValL ifTrue:[ |
|
3478 |
hiL := aColor. |
|
3479 |
hiValL := cl |
|
3480 |
] |
|
3481 |
]. |
|
3482 |
] ifFalse:[ |
|
3483 |
lowL isNil ifTrue:[ |
|
3484 |
lowL := aColor. |
|
3485 |
lowValL := cl |
|
3486 |
] ifFalse:[ |
|
3487 |
cl > lowValL ifTrue:[ |
|
3488 |
lowL := aColor. |
|
3489 |
lowValL := cl |
|
3490 |
] |
|
3491 |
] |
|
3492 |
] |
|
3493 |
]. |
|
3494 |
||
3495 |
(((ch = rh) or:[ch == nil]) and:[cl = rl]) ifTrue:[ |
|
3496 |
"found a color with same light and same hue, keep for saturation" |
|
3497 |
||
3498 |
cs > rs ifTrue:[ |
|
3499 |
hiS isNil ifTrue:[ |
|
3500 |
hiS := aColor. |
|
3501 |
hiValS := cs |
|
3502 |
] ifFalse:[ |
|
3503 |
cs < hiValS ifTrue:[ |
|
3504 |
hiS := aColor. |
|
3505 |
hiValS := cs |
|
3506 |
] |
|
3507 |
]. |
|
3508 |
] ifFalse:[ |
|
3509 |
lowS isNil ifTrue:[ |
|
3510 |
lowS := aColor. |
|
3511 |
lowValS := cs |
|
3512 |
] ifFalse:[ |
|
3513 |
cs > lowValS ifTrue:[ |
|
3514 |
lowS := aColor. |
|
3515 |
lowValS := cs |
|
3516 |
] |
|
3517 |
] |
|
3518 |
] |
|
3519 |
]. |
|
3520 |
||
3521 |
rh notNil ifTrue:[ |
|
3522 |
cl = rl ifTrue:[ |
|
3523 |
cs = rs ifTrue:[ |
|
3524 |
ch notNil ifTrue:[ |
|
3525 |
d := (ch - rh) abs. |
|
3526 |
d > 300 ifTrue:[ |
|
3527 |
rh > 180 ifTrue:[ |
|
3528 |
ch := ch + 360 |
|
3529 |
] ifFalse:[ |
|
3530 |
ch := ch - 360 |
|
3531 |
]. |
|
3532 |
]. |
|
3533 |
ch > rh ifTrue:[ |
|
3534 |
hiH isNil ifTrue:[ |
|
3535 |
hiH := aColor. |
|
3536 |
hiValH := ch |
|
3537 |
] ifFalse:[ |
|
3538 |
ch < hiValH ifTrue:[ |
|
3539 |
hiH := aColor. |
|
3540 |
hiValH := ch |
|
3541 |
] |
|
3542 |
] |
|
3543 |
] ifFalse:[ |
|
3544 |
lowH isNil ifTrue:[ |
|
3545 |
lowH := aColor. |
|
3546 |
lowValH := ch |
|
3547 |
] ifFalse:[ |
|
3548 |
ch > lowValH ifTrue:[ |
|
3549 |
lowH := aColor. |
|
3550 |
lowValH := ch |
|
3551 |
] |
|
3552 |
] |
|
3553 |
] |
|
3554 |
] |
|
3555 |
] |
|
3556 |
] |
|
3557 |
] |
|
3558 |
] |
|
3559 |
] |
|
3560 |
]. |
|
3561 |
||
3562 |
"found bounds for light ?" |
|
3563 |
||
3564 |
lowL notNil ifTrue:[ |
|
3565 |
rl = lowValL ifTrue:[ |
|
3566 |
^ aBlock value:lowL value:nil |
|
3567 |
]. |
|
3568 |
hiL notNil ifTrue:[ |
|
3569 |
^ self monoDitherFor:1.0 / (hiValL - lowValL) / (rl - lowValL) |
|
3570 |
between:lowL |
|
3571 |
and:hiL |
|
3572 |
on:aDevice |
|
3573 |
into:aBlock |
|
3574 |
]. |
|
3575 |
"found bound for light - dither with white" |
|
3576 |
^ self monoDitherFor:1.0 / (100 - lowValL) / (rl - lowValL) |
|
3577 |
between:lowL |
|
3578 |
and:White |
|
3579 |
on:aDevice |
|
3580 |
into:aBlock |
|
3581 |
]. |
|
3582 |
||
3583 |
"found bound for light - dither with black" |
|
3584 |
hiL notNil ifTrue:[ |
|
3585 |
^ self monoDitherFor:1.0 / (hiValL) / (rl) |
|
3586 |
between:Black |
|
3587 |
and:hiL |
|
3588 |
on:aDevice |
|
3589 |
into:aBlock |
|
3590 |
]. |
|
3591 |
||
3592 |
||
3593 |
"found bounds for saturation?" |
|
3594 |
||
3595 |
(lowS notNil and:[hiS notNil]) ifTrue:[ |
|
3596 |
" |
|
3597 |
'saturation dither' printNewline. |
|
3598 |
" |
|
3599 |
^ self monoDitherFor:1.0 / (hiValS - lowValS) / (rs - lowValS) |
|
3600 |
between:lowS |
|
3601 |
and:hiS |
|
3602 |
on:aDevice |
|
3603 |
into:aBlock |
|
3604 |
]. |
|
3605 |
||
3606 |
"found bounds for hue ?" |
|
3607 |
||
3608 |
(lowH notNil and:[hiH notNil]) ifTrue:[ |
|
3609 |
" |
|
3610 |
'hue dither' printNewline. |
|
3611 |
" |
|
3612 |
hiValH < lowValH ifTrue:[ |
|
3613 |
hiValH := hiValH + 360 |
|
3614 |
]. |
|
3615 |
||
3616 |
d := hiValH - lowValH. |
|
3617 |
||
3618 |
^ self monoDitherFor:1.0 / (d / (rh - lowValH)) |
|
3619 |
between:lowH |
|
3620 |
and:hiH |
|
3621 |
on:aDevice |
|
3622 |
into:aBlock |
|
3623 |
]. |
|
3624 |
||
3625 |
^ aBlock value:nil value:nil |
|
3626 |
||
3627 |
"Modified: 11.6.1996 / 17:26:57 / cg" |
|
3628 |
! |
|
3629 |
||
3630 |
fixDitherRed:redVal green:greenVal blue:blueVal on:aDevice into:aBlock |
|
3631 |
"get a dither form for an rgb value. |
|
3632 |
Returns 2 values (either color or ditherForm) through aBlock. |
|
3633 |
This code uses the table of preallocated fix-colors to find |
|
3634 |
dither colors." |
|
3635 |
||
3636 |
| |
|
3637 |
nR "{ Class: SmallInteger }" |
|
3638 |
nG "{ Class: SmallInteger }" |
|
3639 |
nB "{ Class: SmallInteger }" |
|
3640 |
hR "{ Class: SmallInteger }" |
|
3641 |
hG "{ Class: SmallInteger }" |
|
3642 |
hB "{ Class: SmallInteger }" |
|
3643 |
eR eG eB |
|
3644 |
rI "{ Class: SmallInteger }" |
|
3645 |
gI "{ Class: SmallInteger }" |
|
3646 |
bI "{ Class: SmallInteger }" |
|
3647 |
idx "{ Class: SmallInteger }" |
|
3648 |
f clr |
|
3649 |
r "{ Class: SmallInteger }" |
|
3650 |
g "{ Class: SmallInteger }" |
|
3651 |
b "{ Class: SmallInteger }" |
|
3652 |
x1 "{ Class: SmallInteger }" |
|
3653 |
x2 "{ Class: SmallInteger }" |
|
3654 |
step "{ Class: SmallInteger }" |
|
3655 |
lastIdx mx |
|
3656 |
dS "{ Class: SmallInteger }" |
|
3657 |
cube| |
|
3658 |
||
3659 |
(cube := aDevice fixColors) notNil ifTrue:[ |
|
3660 |
dS := 4. |
|
3661 |
||
3662 |
f := Form width:dS height:dS depth:(aDevice depth) on:aDevice. |
|
3663 |
f initGC. |
|
3664 |
||
3665 |
mx := MaxValue asFloat. |
|
3666 |
||
3667 |
nR := aDevice numFixRed. |
|
3668 |
nG := aDevice numFixGreen. |
|
3669 |
nB := aDevice numFixBlue. |
|
3670 |
||
3671 |
hR := nR // 2. |
|
3672 |
hG := nG // 2. |
|
3673 |
hB := nB // 2. |
|
3674 |
||
3675 |
eR := eG := eB := 0. |
|
3676 |
r := redVal. |
|
3677 |
g := greenVal. |
|
3678 |
b := blueVal. |
|
3679 |
||
3680 |
step := -1. |
|
3681 |
||
3682 |
0 to:dS-1 do:[:y | |
|
3683 |
step == -1 ifTrue:[ |
|
3684 |
x1 := 0. x2 := dS-1. step := 1. |
|
3685 |
] ifFalse:[ |
|
3686 |
x1 := dS-1. x2 := 0. step := -1. |
|
3687 |
]. |
|
3688 |
||
3689 |
x1 to:x2 by:step do:[:x | |
|
3690 |
"/ the nearest along the grid |
|
3691 |
||
3692 |
r := redVal + eR. |
|
3693 |
r > MaxValue ifTrue:[r := MaxValue] |
|
3694 |
ifFalse:[r < 0 ifTrue:[r := 0]]. |
|
3695 |
g := greenVal + eG. |
|
3696 |
g > MaxValue ifTrue:[g := MaxValue] |
|
3697 |
ifFalse:[g < 0 ifTrue:[g := 0]]. |
|
3698 |
||
3699 |
b := blueVal + eB. |
|
3700 |
b > MaxValue ifTrue:[b := MaxValue] |
|
3701 |
ifFalse:[b < 0 ifTrue:[b := 0]]. |
|
3702 |
||
3703 |
rI := (r * (nR-1) + hR / mx) rounded. |
|
3704 |
gI := (g * (nG-1) + hG / mx) rounded . |
|
3705 |
bI := (b * (nB-1) + hB / mx) rounded . |
|
3706 |
||
3707 |
idx := (((rI * nG) + gI) * nB + bI) + 1. |
|
3708 |
clr := (cube at:idx) exactOn:aDevice. |
|
3709 |
lastIdx isNil ifTrue:[lastIdx := idx] |
|
3710 |
ifFalse:[lastIdx ~~ idx ifTrue:[lastIdx := -1]]. |
|
3711 |
||
3712 |
f foreground:clr. |
|
3713 |
f displayPointX:x y:y. |
|
3714 |
||
3715 |
eR := r - clr scaledRed. |
|
3716 |
eG := g - clr scaledGreen. |
|
3717 |
eB := b - clr scaledBlue. |
|
3718 |
]. |
|
3719 |
]. |
|
3720 |
f releaseGC. |
|
3721 |
||
3722 |
lastIdx ~~ -1 ifTrue:[ |
|
3723 |
^ aBlock value:clr value:nil |
|
3724 |
]. |
|
3725 |
^ aBlock value:nil value:f |
|
3726 |
||
3727 |
]. |
|
3728 |
||
3729 |
^ aBlock value:nil value:nil |
|
3730 |
||
3731 |
"Modified: 11.7.1996 / 18:30:28 / cg" |
|
3732 |
! |
|
3733 |
||
3734 |
monoDitherFor:fraction between:color1 and:color2 on:aDevice into:aBlock |
|
3735 |
"get a dither form or colorId for dithering between 2 colors. |
|
3736 |
Returns 2 values (either color or ditherForm) through aBlock." |
|
3737 |
||
3738 |
|form c1 c2 |
|
3739 |
index "{ Class:SmallInteger }"| |
|
3740 |
||
3741 |
"/ |
|
3742 |
"/ having forms with: [1 .. 63] of 64 pixels (see Form), |
|
3743 |
"/ we get dithers for: 1/64, 2/64, ... 63/64 |
|
3744 |
"/ |
|
3745 |
||
3746 |
index := (fraction * 64) rounded. |
|
3747 |
||
3748 |
c1 := color1 exactOn:aDevice. |
|
3749 |
index < 1 ifTrue:[ |
|
3750 |
^ aBlock value:c1 value:nil |
|
3751 |
]. |
|
3752 |
||
3753 |
c2 := color2 exactOn:aDevice. |
|
3754 |
index >= 64 ifTrue:[ |
|
3755 |
^ aBlock value:c2 value:nil |
|
3756 |
]. |
|
3757 |
||
3758 |
form := Form width:8 height:8 fromArray:(DitherBits at:index) on:aDevice. |
|
3759 |
form colorMap:(Array with:c1 with:c2). |
|
3760 |
^ aBlock value:nil value:form |
|
3761 |
||
3762 |
" |
|
3763 |
Color basicNew |
|
3764 |
monoDitherFor:(MaxValue // 2) |
|
3765 |
between:Color black |
|
3766 |
and:Color white |
|
3767 |
on:Display |
|
3768 |
into:[:clr :dither | clr inspect. dither inspect] |
|
3769 |
" |
|
3770 |
||
3771 |
"Modified: 11.6.1996 / 16:55:37 / cg" |
|
3772 |
! |
|
3773 |
||
3774 |
restored |
|
3775 |
"private: color has been restored (either from snapin or binary store); |
|
3776 |
flush device stuff or reallocate a cell." |
|
3777 |
||
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3778 |
ditherForm := replacementColor := nil. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3779 |
|
1023 | 3780 |
red notNil ifTrue:[ |
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3781 |
device := nil. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3782 |
colorId := nil |
1023 | 3783 |
] ifFalse:[ |
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3784 |
"a variable color has been restored" |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3785 |
(colorId notNil and:[writable == true and:[device notNil]]) ifTrue:[ |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3786 |
colorId := device colorCell. |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3787 |
device setColor:colorId scaledRed:red scaledGreen:green scaledBlue:blue |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3788 |
] |
1023 | 3789 |
] |
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3790 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3791 |
"Modified: 17.1.1997 / 00:04:14 / cg" |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3792 |
! |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3793 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3794 |
setColorId:anId |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3795 |
"private: set the deviceId" |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3796 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3797 |
colorId := anId |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3798 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3799 |
"Created: 17.1.1997 / 00:05:41 / cg" |
1023 | 3800 |
! |
3801 |
||
3802 |
setDevice:aDevice colorId:aNumber |
|
3803 |
"private:set device and colorId" |
|
3804 |
||
3805 |
device := aDevice. |
|
3806 |
colorId := aNumber |
|
3807 |
! |
|
3808 |
||
1238
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3809 |
setDitherForm:aForm |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3810 |
"private: set the ditherForm" |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3811 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3812 |
ditherForm := aForm |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3813 |
|
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3814 |
"Created: 17.1.1997 / 00:04:57 / cg" |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3815 |
! |
b363894182d0
prepare for replacement colors
Claus Gittinger <cg@exept.de>
parents:
1237
diff
changeset
|
3816 |
|
1023 | 3817 |
setScaledRed:r scaledGreen:g scaledBlue:b device:aDevice |
3818 |
"private: set the components" |
|
3819 |
||
3820 |
red notNil ifTrue:[ |
|
1233 | 3821 |
"oops cannot change (you want to make red be green - or what)" |
3822 |
||
3823 |
self error:'Colors cannot change their components'. |
|
3824 |
^ self |
|
1023 | 3825 |
]. |
3826 |
red := r. |
|
3827 |
green := g. |
|
3828 |
blue := b. |
|
3829 |
device := aDevice |
|
1233 | 3830 |
|
3831 |
"Modified: 16.1.1997 / 22:39:26 / cg" |
|
1023 | 3832 |
! |
3833 |
||
3834 |
setWritable:aBoolean |
|
3835 |
"set/clear the writable attribute. Highly private" |
|
3836 |
||
3837 |
writable := aBoolean |
|
3838 |
||
3839 |
"Modified: 23.4.1996 / 13:40:18 / cg" |
|
3840 |
! ! |
|
3841 |
||
3842 |
!Color methodsFor:'queries'! |
|
3843 |
||
3844 |
averageColor |
|
3845 |
"return the average color - thats myself. |
|
3846 |
This method has been added for compatibility with the image |
|
3847 |
protocol." |
|
3848 |
||
3849 |
^ self |
|
3850 |
! |
|
3851 |
||
3852 |
averageColorIn:aRectangle |
|
3853 |
"return the average color - thats myself. |
|
3854 |
This method has been added for compatibility with the image |
|
3855 |
protocol." |
|
3856 |
||
3857 |
^ self |
|
3858 |
! |
|
3859 |
||
3860 |
brightness |
|
3861 |
"ST80 compatibility: return the grey intensity in [0..1]" |
|
3862 |
||
3863 |
^ ((3 * red) + (6 * green) + (blue)) / 10.0 / MaxValue |
|
3864 |
||
3865 |
"Modified: 7.6.1996 / 19:42:21 / cg" |
|
3866 |
! |
|
3867 |
||
3868 |
deltaFrom:aColor |
|
3869 |
"return the distance of the receiver from some color specified |
|
3870 |
by r/g/b values" |
|
3871 |
||
3872 |
^ aColor deltaFromRed:self red green:self green blue:self blue |
|
3873 |
||
3874 |
"Created: 14.6.1996 / 20:07:22 / cg" |
|
3875 |
"Modified: 14.6.1996 / 20:49:32 / cg" |
|
3876 |
! |
|
3877 |
||
3878 |
deltaFromRed:r green:g blue:b |
|
3879 |
"return the distance of the receiver from some color specified |
|
3880 |
by r/g/b values" |
|
3881 |
||
3882 |
" |
|
3883 |
Q: how should component errors be weighted ? |
|
3884 |
" |
|
3885 |
^ (self red - r) abs |
|
3886 |
+ (self green - g) abs |
|
3887 |
+ (self blue - b) abs. |
|
3888 |
||
3889 |
"Created: 14.6.1996 / 20:03:58 / cg" |
|
3890 |
"Modified: 14.6.1996 / 20:20:24 / cg" |
|
3891 |
! |
|
3892 |
||
3893 |
deltaFromScaledRed:r scaledGreen:g scaledBlue:b |
|
3894 |
"return the distance of the receiver from some color specified |
|
3895 |
by r/g/b values" |
|
3896 |
||
3897 |
" |
|
3898 |
Q: how should component errors be weighted ? |
|
3899 |
" |
|
3900 |
^ (red - r) abs |
|
3901 |
+ (green - g) abs |
|
3902 |
+ (blue - b) abs. |
|
3903 |
||
3904 |
"Created: 11.6.1996 / 18:01:12 / cg" |
|
3905 |
"Modified: 14.6.1996 / 20:36:14 / cg" |
|
3906 |
! |
|
3907 |
||
3908 |
errorFrom:aColor |
|
3909 |
"return some value which can be used to compare colors. |
|
3910 |
The following simply returns the vector distance of the r/g/b vectors. |
|
3911 |
This may not be a very good idea; probably, we should honor the |
|
3912 |
fact that the hue difference should have more weight than saturation and/or light" |
|
3913 |
||
3914 |
^ (red - aColor scaledRed) squared |
|
3915 |
+ (green - aColor scaledGreen) squared |
|
3916 |
+ (blue - aColor scaledBlue) squared. |
|
3917 |
! |
|
3918 |
||
3919 |
grayIntensity |
|
3920 |
"return the grey intensity in percent [0..100] (US version ;-)" |
|
3921 |
||
3922 |
^ ((3 * red) + (6 * green) + (1 * blue)) * 10.0 / MaxValue |
|
3923 |
||
3924 |
"Created: 2.5.1996 / 11:38:21 / cg" |
|
3925 |
! |
|
3926 |
||
3927 |
greyIntensity |
|
3928 |
"return the grey intensity in percent [0..100] (English version ;-)" |
|
3929 |
||
3930 |
^ self grayIntensity |
|
3931 |
||
3932 |
"Modified: 28.5.1996 / 20:45:41 / cg" |
|
3933 |
! |
|
3934 |
||
3935 |
isColor |
|
3936 |
"return true if the receiver is a Color." |
|
3937 |
||
3938 |
^ true |
|
3939 |
! |
|
3940 |
||
3941 |
isDithered |
|
3942 |
"return true, if this is a dithered Color. |
|
3943 |
Only makes sense if the receiver is a device color." |
|
3944 |
||
3945 |
^ ditherForm notNil |
|
3946 |
! |
|
3947 |
||
1326
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3948 |
isGray |
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3949 |
"same as isGrayColor - for ST80 compatibility." |
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3950 |
|
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3951 |
^ self isGrayColor |
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3952 |
|
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3953 |
" |
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3954 |
(Color grey:50) isGray |
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3955 |
(Color red) isGray |
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3956 |
" |
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3957 |
|
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3958 |
"Created: 10.2.1997 / 22:10:25 / cg" |
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3959 |
! |
a519593747e8
#isGray - ST80 compatibility
Claus Gittinger <cg@exept.de>
parents:
1269
diff
changeset
|
3960 |
|
1023 | 3961 |
isGrayColor |
3962 |
"return true, if this color is a gray one - |
|
3963 |
i.e. red = green = blue" |
|
3964 |
||
3965 |
red ~~ green ifTrue:[^ false]. |
|
3966 |
^ red == blue |
|
3967 |
||
3968 |
" |
|
3969 |
(Color grey:50) isGrayColor |
|
3970 |
(Color red) isGrayColor |
|
3971 |
" |
|
3972 |
||
3973 |
"Created: 2.5.1996 / 11:38:48 / cg" |
|
3974 |
! |
|
3975 |
||
3976 |
isGreyColor |
|
3977 |
"return true, if this color is a grey one (English version ;-) - |
|
3978 |
i.e. red = green = blue" |
|
3979 |
||
3980 |
^ self isGrayColor |
|
3981 |
||
3982 |
"(Color grey:50) isGreyColor" |
|
3983 |
"(Color red) isGreyColor" |
|
3984 |
||
3985 |
"Modified: 28.5.1996 / 20:44:36 / cg" |
|
3986 |
! ! |
|
3987 |
||
1347 | 3988 |
!Color methodsFor:'testing'! |
3989 |
||
3990 |
isColorObject |
|
3991 |
^ true |
|
3992 |
! ! |
|
3993 |
||
1144 | 3994 |
!Color class methodsFor:'documentation'! |
1023 | 3995 |
|
3996 |
version |
|
1347 | 3997 |
^ '$Header: /cvs/stx/stx/libview/Color.st,v 1.86 1997-02-13 21:58:54 ca Exp $' |
1023 | 3998 |
! ! |
3999 |
Color initialize! |