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