0
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1992-93 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:'redVal greenVal blueVal device colorId ditherForm'
|
|
15 |
classVariableNames:'lobby
|
|
16 |
Black White LightGrey Grey DarkGrey
|
|
17 |
Pseudo0 Pseudo1 PseudoAll
|
|
18 |
Red Green Blue DitherColors'
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Graphics-Support'
|
|
21 |
!
|
|
22 |
|
|
23 |
Color comment:'
|
|
24 |
|
|
25 |
COPYRIGHT (c) 1992-93 by Claus Gittinger
|
|
26 |
All Rights Reserved
|
|
27 |
|
|
28 |
see Color documentation for more info
|
|
29 |
|
|
30 |
%W% %E%
|
|
31 |
totally rewritten summer 92 by claus (from XColor)
|
|
32 |
'!
|
|
33 |
|
|
34 |
!Color class methodsFor:'documentation'!
|
|
35 |
|
|
36 |
documentation
|
|
37 |
"
|
|
38 |
Color represents colors in a device independent manner, main info I keep about
|
|
39 |
mySelf are the red, green and blue components in percent (0 .. 100).
|
|
40 |
The device specific color can be aquired by sending a color the 'on:aDevice' message,
|
|
41 |
which will return a color with the same rgb values as the receiver but specific
|
|
42 |
for that device.
|
|
43 |
|
|
44 |
Colors can be pure or dithered, depending on the capabilities of the device.
|
|
45 |
For plain colors, the colorId-instvar is a handle (usually lookup-table entry) for that
|
|
46 |
device. For dithered colors, the colorId is nil and ditherForm specifies the form
|
|
47 |
used to dither that color (which can have a colormap and need 2 or more plain colors).
|
|
48 |
|
|
49 |
Instance variables:
|
|
50 |
|
|
51 |
redVal <Number> the red component (0..100)
|
|
52 |
greenVal <Number> the green component (0..100)
|
|
53 |
blueVal <Number> the blue component (0..100)
|
|
54 |
device <aDevice> the device I am on, or nil
|
|
55 |
colorId <anObject> some device dependent identifier (or nil if dithered)
|
|
56 |
ditherForm <aForm> the Form to dither this color (if non-nil)
|
|
57 |
|
|
58 |
Class variables:
|
|
59 |
|
|
60 |
lobby <Registry> keeps track of dead colors
|
|
61 |
|
|
62 |
Black <Color> for fast return of black
|
|
63 |
White <Color> for fast return of white
|
|
64 |
Grey <Color> for fast return of grey
|
|
65 |
LightGrey <Color> for fast return of lightGrey
|
|
66 |
DarkGrey <Color> for fast return of darkGrey
|
|
67 |
|
|
68 |
Pseudo0 <Color> a color with 0 as handle (for forms and bitblit)
|
|
69 |
Pseudo1 <Color> a color with 1 as handle (for forms)
|
|
70 |
PseudoAll <Color> a color with allPlanes as handle (for bitblit)
|
|
71 |
|
|
72 |
Red <Color> red, needed for dithering
|
|
73 |
Green <Color> green, for dithering
|
|
74 |
Blue <Color> blue, for dithering
|
|
75 |
|
|
76 |
DitherColors <Collection> some preallocated colors for dithering
|
|
77 |
(kept, so they are available when needed)
|
|
78 |
|
|
79 |
%W% %E%
|
|
80 |
totally rewritten summer 92 by claus (from XColor)
|
|
81 |
"
|
|
82 |
! !
|
|
83 |
|
|
84 |
!Color class methodsFor:'initialization'!
|
|
85 |
|
|
86 |
initialize
|
|
87 |
"setup tracker of known colors and initialize classvars with
|
|
88 |
heavily used colors"
|
|
89 |
|
|
90 |
lobby isNil ifTrue:[
|
|
91 |
lobby := Registry new.
|
|
92 |
|
|
93 |
self getPrimaryColors.
|
|
94 |
|
|
95 |
"want to be informed when returning from snapshot"
|
|
96 |
ObjectMemory addDependent:self.
|
|
97 |
|
|
98 |
Smalltalk at:#ColorValue put:self "for ST-80 compatibility"
|
|
99 |
].
|
|
100 |
!
|
|
101 |
|
|
102 |
getPrimaryColors
|
|
103 |
White := (self red:100 green:100 blue:100) exactOn:Display.
|
|
104 |
Black := (self red:0 green:0 blue:0) exactOn:Display.
|
|
105 |
|
|
106 |
Display hasColors ifTrue:[
|
|
107 |
Red := (self red:100 green:0 blue:0) exactOn:Display.
|
|
108 |
Green := (self red:0 green:100 blue:0) exactOn:Display.
|
|
109 |
Blue := (self red:0 green:0 blue:100) exactOn:Display.
|
|
110 |
|
|
111 |
Display ncells < 256 ifTrue:[
|
|
112 |
"on low-color resolution displays, allocate some colors
|
|
113 |
for dithering - otherwise, they may not be available when
|
|
114 |
we need them ..."
|
|
115 |
|
|
116 |
DitherColors := OrderedCollection new.
|
|
117 |
DitherColors add:((self red:100 green:100 blue:0) exactOn:Display).
|
|
118 |
DitherColors add:((self red:100 green:0 blue:100) exactOn:Display).
|
|
119 |
DitherColors add:((self red:0 green:100 blue:100) exactOn:Display).
|
|
120 |
]
|
|
121 |
]
|
|
122 |
!
|
|
123 |
|
|
124 |
flushDeviceColors
|
|
125 |
"unassign all colors from their device"
|
|
126 |
|
|
127 |
lobby contentsDo:[:aColor |
|
|
128 |
aColor resetDevice.
|
|
129 |
lobby changed:aColor
|
|
130 |
]
|
|
131 |
!
|
|
132 |
|
|
133 |
update:something
|
|
134 |
(something == #restarted) ifTrue:[
|
|
135 |
self flushDeviceColors
|
|
136 |
].
|
|
137 |
(something == #returnFromSnapshot) ifTrue:[
|
|
138 |
self getPrimaryColors
|
|
139 |
]
|
|
140 |
! !
|
|
141 |
|
|
142 |
!Color class methodsFor:'instance creation'!
|
|
143 |
|
|
144 |
white
|
|
145 |
"return the white-color"
|
|
146 |
|
|
147 |
White isNil ifTrue:[
|
|
148 |
White := (self red:100 green:100 blue:100) exactOn:Display
|
|
149 |
].
|
|
150 |
^ White
|
|
151 |
!
|
|
152 |
|
|
153 |
black
|
|
154 |
"return the black-color"
|
|
155 |
|
|
156 |
Black isNil ifTrue:[
|
|
157 |
Black := (self red:0 green:0 blue:0) exactOn:Display
|
|
158 |
].
|
|
159 |
^ Black
|
|
160 |
!
|
|
161 |
|
|
162 |
mediumGrey
|
|
163 |
"return medium-grey color"
|
|
164 |
|
|
165 |
^ self grey
|
|
166 |
!
|
|
167 |
|
|
168 |
veryLightGrey
|
|
169 |
^ self grey:87
|
|
170 |
!
|
|
171 |
|
|
172 |
lightGrey
|
|
173 |
"return light-grey color -
|
|
174 |
take value from resource file - 67% is very dark on some, very light
|
|
175 |
on other displays ... sigh"
|
|
176 |
|
|
177 |
LightGrey isNil ifTrue:[
|
|
178 |
LightGrey := self grey:(Resource name:'COLOR_LIGHTGREY_VALUE'
|
|
179 |
default:67
|
|
180 |
fromFile:'Smalltalk.rs')
|
|
181 |
].
|
|
182 |
^ LightGrey
|
|
183 |
!
|
|
184 |
|
|
185 |
darkGrey
|
|
186 |
"return dark-grey color -
|
|
187 |
take value from resource file - 33% is very dark on some, very light
|
|
188 |
on other displays ... sigh"
|
|
189 |
|
|
190 |
DarkGrey isNil ifTrue:[
|
|
191 |
DarkGrey := self grey:(Resource name:'COLOR_DARKGREY_VALUE'
|
|
192 |
default:33
|
|
193 |
fromFile:'Smalltalk.rs')
|
|
194 |
].
|
|
195 |
^ DarkGrey
|
|
196 |
!
|
|
197 |
|
|
198 |
veryDarkGrey
|
|
199 |
^ self grey:13
|
|
200 |
!
|
|
201 |
|
|
202 |
grey
|
|
203 |
"return a medium grey color -
|
|
204 |
take value from resource file - 50% is very dark on some, very light
|
|
205 |
on other displays ... sigh"
|
|
206 |
|
|
207 |
Grey isNil ifTrue:[
|
|
208 |
Grey := self grey:(Resource name:'COLOR_GREY_VALUE'
|
|
209 |
default:50
|
|
210 |
fromFile:'Smalltalk.rs')
|
|
211 |
].
|
|
212 |
^ Grey
|
|
213 |
!
|
|
214 |
|
|
215 |
grey:grey
|
|
216 |
"return a grey color. The argument, grey is interpreted as
|
|
217 |
percent (0..100)."
|
|
218 |
|
|
219 |
^ self red:grey green:grey blue:grey
|
|
220 |
!
|
|
221 |
|
|
222 |
gray
|
|
223 |
"return grey"
|
|
224 |
|
|
225 |
^ self grey
|
|
226 |
!
|
|
227 |
|
|
228 |
lightGray
|
|
229 |
"return lightGrey"
|
|
230 |
|
|
231 |
^ self lightGrey
|
|
232 |
!
|
|
233 |
|
|
234 |
darkGray
|
|
235 |
"return darkGrey"
|
|
236 |
|
|
237 |
^ self darkGrey
|
|
238 |
!
|
|
239 |
|
|
240 |
red
|
|
241 |
"return red"
|
|
242 |
|
|
243 |
Red isNil ifTrue:[
|
|
244 |
Red := self red:100 green:0 blue:0.
|
|
245 |
].
|
|
246 |
^ Red
|
|
247 |
!
|
|
248 |
|
|
249 |
green
|
|
250 |
"return green"
|
|
251 |
|
|
252 |
Green isNil ifTrue:[
|
|
253 |
Green := self red:0 green:100 blue:0
|
|
254 |
].
|
|
255 |
^ Green
|
|
256 |
!
|
|
257 |
|
|
258 |
blue
|
|
259 |
"return blue"
|
|
260 |
|
|
261 |
Blue isNil ifTrue:[
|
|
262 |
Blue := self red:0 green:0 blue:100
|
|
263 |
].
|
|
264 |
^ Blue
|
|
265 |
!
|
|
266 |
|
|
267 |
red:r green:g blue:b
|
|
268 |
"return a color from red, green and blue values;
|
|
269 |
the arguments, r, g and b are interpreted as percent (0..100)"
|
|
270 |
|
|
271 |
|newColor rr rg rb|
|
|
272 |
|
|
273 |
"round to 1/300 i.e. to about 0.33%"
|
|
274 |
|
|
275 |
rr := (r * 3) rounded / 3.0.
|
|
276 |
rg := (g * 3) rounded / 3.0.
|
|
277 |
rb := (b * 3) rounded / 3.0.
|
|
278 |
|
|
279 |
"look if already known"
|
|
280 |
lobby contentsDo:[:aColor |
|
|
281 |
(rr = aColor red) ifTrue:[
|
|
282 |
(rg = aColor green) ifTrue:[
|
|
283 |
(rb = aColor blue) ifTrue:[
|
|
284 |
^ aColor
|
|
285 |
]
|
|
286 |
]
|
|
287 |
]
|
|
288 |
].
|
|
289 |
newColor := self basicNew setRed:rr green:rg blue:rb device:nil.
|
|
290 |
lobby register:newColor.
|
|
291 |
^ newColor
|
|
292 |
!
|
|
293 |
|
|
294 |
name:aString
|
|
295 |
"return a named color - or try do do as good as possible"
|
|
296 |
|
|
297 |
^ self nameOrDither:aString
|
|
298 |
!
|
|
299 |
|
|
300 |
nameOrDither:aString
|
|
301 |
"return a named color - if the exact color is not available,
|
|
302 |
return a dithered color"
|
|
303 |
|
|
304 |
Display getRGBFromName:aString into:[:r :g :b |
|
|
305 |
r notNil ifTrue:[
|
|
306 |
^ self red:r green:g blue:b
|
|
307 |
].
|
|
308 |
].
|
|
309 |
self error:'no color named ' , aString.
|
|
310 |
^ nil
|
|
311 |
|
|
312 |
"Color nameOrDither:'Brown'"
|
|
313 |
!
|
|
314 |
|
|
315 |
nameOrNearest:aString
|
|
316 |
"return a named color - or its nearest match"
|
|
317 |
|
|
318 |
|id newColor|
|
|
319 |
|
|
320 |
id := Display colorNamed:aString.
|
|
321 |
id isNil ifTrue:[
|
|
322 |
ObjectMemory scavenge.
|
|
323 |
id := Display colorNamed:aString.
|
|
324 |
id isNil ifTrue:[^ nil].
|
|
325 |
].
|
|
326 |
newColor := self basicNew.
|
|
327 |
Display getRGBFrom:id into:[:r :g :b |
|
|
328 |
newColor setRed:r green:g blue:b device:Display
|
|
329 |
].
|
|
330 |
newColor colorId:id.
|
|
331 |
lobby register:newColor.
|
|
332 |
^ newColor
|
|
333 |
!
|
|
334 |
|
|
335 |
nearestColorRed:r green:g blue:b error:error on:aDevice
|
|
336 |
"return a device color on aDevice with rgb values
|
|
337 |
same or near r/g/b, if there is one, nil otherwise.
|
|
338 |
Near is defined as having an error less than the argument
|
|
339 |
error (in percent). The error is computed by the color
|
|
340 |
vector distance (which is not the best possible solution)."
|
|
341 |
|
|
342 |
"first try exact color"
|
|
343 |
|
|
344 |
|delta minDelta bestSoFar rr rg rb|
|
|
345 |
|
|
346 |
"round to 1/300 i.e. to about 0.3%"
|
|
347 |
|
|
348 |
rr := (r * 3) rounded / 3.0.
|
|
349 |
rg := (g * 3) rounded / 3.0.
|
|
350 |
rb := (b * 3) rounded / 3.0.
|
|
351 |
|
|
352 |
lobby contentsDo:[:aColor |
|
|
353 |
(aColor device == aDevice) ifTrue:[
|
|
354 |
(aColor colorId notNil) ifTrue:[
|
|
355 |
(rr = aColor red) ifTrue:[
|
|
356 |
(rg = aColor green) ifTrue:[
|
|
357 |
(rb = aColor blue) ifTrue:[
|
|
358 |
^ aColor
|
|
359 |
]
|
|
360 |
]
|
|
361 |
]
|
|
362 |
]
|
|
363 |
]
|
|
364 |
].
|
|
365 |
|
|
366 |
"exact color was not available, search for the one with the
|
|
367 |
smallest delta"
|
|
368 |
|
|
369 |
minDelta := 999999.
|
|
370 |
lobby contentsDo:[:aColor |
|
|
371 |
(aColor device == aDevice) ifTrue:[
|
|
372 |
(aColor colorId notNil) ifTrue:[
|
|
373 |
delta := ((rr - aColor red) squared * 0.3)
|
|
374 |
+ ((rg - aColor green) squared * 0.6)
|
|
375 |
+ ((rb - aColor blue) squared * 0.1).
|
|
376 |
|
|
377 |
delta < minDelta ifTrue:[
|
|
378 |
bestSoFar := aColor.
|
|
379 |
minDelta := delta
|
|
380 |
]
|
|
381 |
]
|
|
382 |
]
|
|
383 |
].
|
|
384 |
|
|
385 |
minDelta < error squared ifTrue:[
|
|
386 |
^ bestSoFar
|
|
387 |
].
|
|
388 |
|
|
389 |
^ nil
|
|
390 |
!
|
|
391 |
|
|
392 |
hue:h light:l saturation:s
|
|
393 |
"return a color from hue, light and saturation values"
|
|
394 |
|
|
395 |
self withRGBFromHue:h light:l saturation:s do:[:r :g :b |
|
|
396 |
^ self red:r green:g blue:b
|
|
397 |
]
|
|
398 |
!
|
|
399 |
|
|
400 |
noColor
|
|
401 |
"return a special color which, when used for bit-blitting will
|
|
402 |
behave like a 0-color (i.e. have a device-pixel value of all-0s)"
|
|
403 |
|
|
404 |
^ self basicNew colorId:0
|
|
405 |
!
|
|
406 |
|
|
407 |
allColor
|
|
408 |
"return a special color which, when used for bit-blitting will
|
|
409 |
behave like a all-1-color (i.e. have a device-pixel value of all-1s)"
|
|
410 |
|
|
411 |
^ self basicNew colorId:-1
|
|
412 |
!
|
|
413 |
|
|
414 |
colorId:id
|
|
415 |
"return a color for a specific colorid without associating it to a
|
|
416 |
specific device. Use this only for bitmaps which want 0- or 1-color,
|
|
417 |
or for bitblits if you want to manipulate a specific colorplane."
|
|
418 |
|
|
419 |
id == 0 ifTrue:[
|
|
420 |
Pseudo0 isNil ifTrue:[
|
|
421 |
Pseudo0 := self basicNew colorId:0
|
|
422 |
].
|
|
423 |
^ Pseudo0
|
|
424 |
].
|
|
425 |
id == 1 ifTrue:[
|
|
426 |
Pseudo1 isNil ifTrue:[
|
|
427 |
Pseudo1 := self basicNew colorId:1
|
|
428 |
].
|
|
429 |
^ Pseudo1
|
|
430 |
].
|
|
431 |
id == -1 ifTrue:[
|
|
432 |
PseudoAll isNil ifTrue:[
|
|
433 |
PseudoAll := self basicNew colorId:-1
|
|
434 |
].
|
|
435 |
^ PseudoAll
|
|
436 |
].
|
|
437 |
^ self basicNew colorId:id
|
|
438 |
! !
|
|
439 |
|
|
440 |
!Color class methodsFor:'private'!
|
|
441 |
|
|
442 |
existingColorRed:r green:g blue:b on:aDevice
|
|
443 |
"return a device color on aDevice with rgb values
|
|
444 |
if there is one, nil otherwise."
|
|
445 |
|
|
446 |
|rr rg rb|
|
|
447 |
|
|
448 |
rr := (r * 3) rounded / 3.0.
|
|
449 |
rg := (g * 3) rounded / 3.0.
|
|
450 |
rb := (b * 3) rounded / 3.0.
|
|
451 |
|
|
452 |
lobby contentsDo:[:aColor |
|
|
453 |
(aColor device == aDevice) ifTrue:[
|
|
454 |
aColor colorId notNil ifTrue:[
|
|
455 |
(rr = aColor red) ifTrue:[
|
|
456 |
(rg = aColor green) ifTrue:[
|
|
457 |
(rb = aColor blue) ifTrue:[
|
|
458 |
^ aColor
|
|
459 |
]
|
|
460 |
]
|
|
461 |
]
|
|
462 |
]
|
|
463 |
]
|
|
464 |
].
|
|
465 |
^ nil
|
|
466 |
!
|
|
467 |
|
|
468 |
colorNearRed:r green:g blue:b on:aDevice
|
|
469 |
"return a device color on aDevice with rgb values
|
|
470 |
almost matching. If there is one, nil otherwise.
|
|
471 |
This is tried as a last chance before dithering.
|
|
472 |
The algorithm needs rework, the color components
|
|
473 |
should be weighted according some theory :-)"
|
|
474 |
|
|
475 |
|bestColor minDelta diff rr rg rb|
|
|
476 |
|
|
477 |
rr := (r * 3) rounded / 3.0.
|
|
478 |
rg := (g * 3) rounded / 3.0.
|
|
479 |
rb := (b * 3) rounded / 3.0.
|
|
480 |
|
|
481 |
minDelta := 100*100*100.
|
|
482 |
lobby contentsDo:[:aColor |
|
|
483 |
(aColor device == aDevice) ifTrue:[
|
|
484 |
(aColor colorId notNil) ifTrue:[
|
|
485 |
diff := (rr - aColor red) asInteger squared
|
|
486 |
+ (rg - aColor green) asInteger squared
|
|
487 |
+ (rb - aColor blue) asInteger squared.
|
|
488 |
diff < minDelta ifTrue:[
|
|
489 |
bestColor := aColor.
|
|
490 |
minDelta := diff
|
|
491 |
]
|
|
492 |
]
|
|
493 |
]
|
|
494 |
].
|
|
495 |
|
|
496 |
"allow an error of 10% per component"
|
|
497 |
minDelta < (100+100+100) ifTrue:[ ^ bestColor ].
|
|
498 |
^ nil
|
|
499 |
! !
|
|
500 |
|
|
501 |
!Color class methodsFor:'color space conversions'!
|
|
502 |
|
|
503 |
withRGBFromHue:h light:l saturation:s do:aBlock
|
|
504 |
"compute rgb form hls, evaluate aBlock with r,g and b as arguments"
|
|
505 |
|
|
506 |
|valueFunc s1 l1 r g b m1 m2|
|
|
507 |
|
|
508 |
valueFunc := [:n1 :n2 :hIn |
|
|
509 |
|hue|
|
|
510 |
|
|
511 |
hue := hIn.
|
|
512 |
hue > 360 ifTrue:[
|
|
513 |
hue := hue - 360
|
|
514 |
] ifFalse:[
|
|
515 |
hue < 0 ifTrue:[
|
|
516 |
hue := hue + 360
|
|
517 |
].
|
|
518 |
].
|
|
519 |
hue < 60 ifTrue:[
|
|
520 |
n1 + ((n2 - n1) * hue / 60)
|
|
521 |
] ifFalse:[
|
|
522 |
hue < 180 ifTrue:[
|
|
523 |
n2
|
|
524 |
] ifFalse:[
|
|
525 |
hue < 240 ifTrue:[
|
|
526 |
n1 + ((n2 - n1) * (240 - hue) / 60)
|
|
527 |
] ifFalse:[
|
|
528 |
n1
|
|
529 |
]
|
|
530 |
]
|
|
531 |
]
|
|
532 |
].
|
|
533 |
|
|
534 |
"compute hls; h in 0..360; l 0..100; s 0..100"
|
|
535 |
|
|
536 |
s1 := s / 100.0. "scale to 0..1"
|
|
537 |
l1 := l / 100.0.
|
|
538 |
|
|
539 |
l1 <= 0.5 ifTrue:[
|
|
540 |
m2 := l1 * (1 + s1)
|
|
541 |
] ifFalse:[
|
|
542 |
m2 := l1 + s1 - (l1 * s1)
|
|
543 |
].
|
|
544 |
|
|
545 |
m1 := 2 * l1 - m2.
|
|
546 |
|
|
547 |
s1 = 0 ifTrue:[
|
|
548 |
"achromatic, ignore hue"
|
|
549 |
r := g := b := l1
|
|
550 |
] ifFalse:[
|
|
551 |
r := valueFunc value:m1 value:m2 value:h + 120.
|
|
552 |
g := valueFunc value:m1 value:m2 value:h.
|
|
553 |
b := valueFunc value:m1 value:m2 value:h - 120.
|
|
554 |
].
|
|
555 |
aBlock value:r*100 value:g*100 value:b*100
|
|
556 |
!
|
|
557 |
|
|
558 |
withHLSFromRed:r green:g blue:b do:aBlock
|
|
559 |
"compute hls form rgb, evaluate aBlock with h,l and s as arguments"
|
|
560 |
|
|
561 |
|max min r1 g1 b1 delta h l s|
|
|
562 |
|
|
563 |
"compute hls; h in 0..360; l 0..100; s 0..100"
|
|
564 |
|
|
565 |
r1 := r / 100. "scale to 0..1"
|
|
566 |
g1 := g / 100.
|
|
567 |
b1 := b / 100.
|
|
568 |
|
|
569 |
max := (r1 max:g1) max:b1.
|
|
570 |
min := (r1 min:g1) min:b1.
|
|
571 |
l := (max + min) / 2.
|
|
572 |
|
|
573 |
max = min ifTrue:[
|
|
574 |
"achromatic, r=g=b"
|
|
575 |
|
|
576 |
s := 0.
|
|
577 |
h := nil
|
|
578 |
] ifFalse:[
|
|
579 |
l < 0.5 ifTrue:[
|
|
580 |
s := (max - min) / (max + min)
|
|
581 |
] ifFalse:[
|
|
582 |
s := (max - min) / (2 - max - min)
|
|
583 |
].
|
|
584 |
|
|
585 |
"calc hue"
|
|
586 |
|
|
587 |
delta := max - min.
|
|
588 |
r1 = max ifTrue:[
|
|
589 |
h := (g1 - b1) / delta
|
|
590 |
] ifFalse:[
|
|
591 |
g1 = max ifTrue:[
|
|
592 |
h := 2 + ((b1 - r1) / delta)
|
|
593 |
] ifFalse:[
|
|
594 |
h := 4 + ((r1 - g1) / delta)
|
|
595 |
]
|
|
596 |
].
|
|
597 |
h := h * 60.
|
|
598 |
h < 0 ifTrue:[
|
|
599 |
h := h + 360
|
|
600 |
].
|
|
601 |
].
|
|
602 |
aBlock value:h value:l * 100 value:s * 100
|
|
603 |
! !
|
|
604 |
|
|
605 |
!Color methodsFor:'instance release'!
|
|
606 |
|
|
607 |
disposed
|
|
608 |
"a color died - free the device color"
|
|
609 |
|
|
610 |
colorId notNil ifTrue:[
|
|
611 |
device freeColor:colorId
|
|
612 |
]
|
|
613 |
! !
|
|
614 |
|
|
615 |
!Color methodsFor:'private'!
|
|
616 |
|
|
617 |
resetDevice
|
|
618 |
"private: color has been restored (either from snapin or binary store);
|
|
619 |
flush device stuff"
|
|
620 |
|
|
621 |
ditherForm := nil.
|
|
622 |
device := nil.
|
|
623 |
colorId := nil
|
|
624 |
!
|
|
625 |
|
|
626 |
setRed:r green:g blue:b device:aDevice
|
|
627 |
"private: set the components"
|
|
628 |
|
|
629 |
redVal notNil ifTrue:[
|
|
630 |
"oops cannot change (you want to make red be green - or what)"
|
|
631 |
self error:'Colors cannot change their components'.
|
|
632 |
^ self
|
|
633 |
].
|
|
634 |
redVal := r.
|
|
635 |
greenVal := g.
|
|
636 |
blueVal := b.
|
|
637 |
device := aDevice
|
|
638 |
!
|
|
639 |
|
|
640 |
colorId:anId
|
|
641 |
"private: set the deviceId"
|
|
642 |
|
|
643 |
colorId := anId
|
|
644 |
!
|
|
645 |
|
|
646 |
ditherForm:aForm
|
|
647 |
"private: set the ditherForm"
|
|
648 |
|
|
649 |
ditherForm := aForm
|
|
650 |
!
|
|
651 |
|
|
652 |
device:aDevice
|
|
653 |
"private: set the device"
|
|
654 |
|
|
655 |
device := aDevice
|
|
656 |
!
|
|
657 |
|
|
658 |
ditherRed:redVal green:greenVal blue:blueVal on:aDevice into:aBlock
|
|
659 |
"get a dither form or colorId for an rgb value.
|
|
660 |
Returns 2 values (either color or ditherForm) through
|
|
661 |
aBlock.
|
|
662 |
This code is just a minimum of what is really needed,
|
|
663 |
and needs much more work. Currently only some special cases
|
|
664 |
are handled"
|
|
665 |
|
|
666 |
|full none rest primary val gr values primaries sum
|
|
667 |
rr rg rb rh rl rs color1 color2
|
|
668 |
lowL hiL lowValL hiValL lowS hiS lowValS hiValS lowH hiH lowValH hiValH d|
|
|
669 |
|
|
670 |
"get hls (since we dither anyway, round them a bit"
|
|
671 |
|
|
672 |
Color withHLSFromRed:redVal green:greenVal blue:blueVal do:[:h :l :s |
|
|
673 |
h notNil ifTrue:[
|
|
674 |
rh := (h * 3) rounded / 3.0.
|
|
675 |
].
|
|
676 |
rl := (l * 3) rounded / 3.0.
|
|
677 |
rs := (s * 3) rounded / 3.0.
|
|
678 |
].
|
|
679 |
|
|
680 |
rh isNil ifTrue:[
|
|
681 |
"achromatic, dither between achromatic colors"
|
|
682 |
|
|
683 |
lowL := nil.
|
|
684 |
hiL := nil.
|
|
685 |
|
|
686 |
"find the 2 bounding colors"
|
|
687 |
lobby contentsDo:[:aColor |
|
|
688 |
aColor colorId notNil ifTrue:[
|
|
689 |
Color withHLSFromRed:aColor red green:aColor green blue:aColor blue do:[:h :l :s |
|
|
690 |
| cl |
|
|
691 |
|
|
692 |
h isNil ifTrue:[
|
|
693 |
cl := (l * 3) rounded / 3.0.
|
|
694 |
|
|
695 |
cl > rl ifTrue:[
|
|
696 |
hiL isNil ifTrue:[
|
|
697 |
hiL := aColor.
|
|
698 |
hiValL := cl.
|
|
699 |
] ifFalse:[
|
|
700 |
cl < hiValL ifTrue:[
|
|
701 |
hiL := aColor.
|
|
702 |
hiValL := cl.
|
|
703 |
]
|
|
704 |
]
|
|
705 |
] ifFalse:[
|
|
706 |
lowL isNil ifTrue:[
|
|
707 |
lowL := aColor.
|
|
708 |
lowValL := cl
|
|
709 |
] ifFalse:[
|
|
710 |
cl > lowValL ifTrue:[
|
|
711 |
lowL := aColor.
|
|
712 |
lowValL := cl
|
|
713 |
]
|
|
714 |
]
|
|
715 |
]
|
|
716 |
]
|
|
717 |
]
|
|
718 |
]
|
|
719 |
].
|
|
720 |
|
|
721 |
(lowL notNil and:[hiL notNil]) ifTrue:[
|
|
722 |
^ self monoDitherFor:100 / ((hiValL - lowValL)/(rl - lowValL))
|
|
723 |
between:lowL
|
|
724 |
and:hiL
|
|
725 |
on:aDevice
|
|
726 |
into:aBlock
|
|
727 |
].
|
|
728 |
"cannot happen, should always find at least black and white"
|
|
729 |
self error:'cannot happen'.
|
|
730 |
^ aBlock value:nil value:nil
|
|
731 |
].
|
|
732 |
|
|
733 |
"chromatic case"
|
|
734 |
|
|
735 |
aDevice hasColors ifFalse:[
|
|
736 |
"no chance, return nil values"
|
|
737 |
^ aBlock value:nil value:nil
|
|
738 |
].
|
|
739 |
(Red isNil or:[Green isNil or:[Blue isNil]]) ifTrue:[
|
|
740 |
"if we where not able to get primary colors: no chance"
|
|
741 |
^ aBlock value:nil value:nil
|
|
742 |
].
|
|
743 |
|
|
744 |
"try to find two bounding colors with same hue and saturation;
|
|
745 |
dither on light between those"
|
|
746 |
|
|
747 |
lowL := nil.
|
|
748 |
hiL := nil.
|
|
749 |
lowS := nil.
|
|
750 |
hiS := nil.
|
|
751 |
lowH := nil.
|
|
752 |
hiH := nil.
|
|
753 |
|
|
754 |
lobby contentsDo:[:aColor |
|
|
755 |
|
|
756 |
aColor colorId notNil ifTrue:[
|
|
757 |
Color withHLSFromRed:aColor red green:aColor green blue:aColor blue do:[:h :l :s |
|
|
758 |
| cl ch cs|
|
|
759 |
|
|
760 |
h notNil ifTrue:[
|
|
761 |
ch := (h * 3) rounded / 3.0.
|
|
762 |
] ifFalse:[
|
|
763 |
ch := nil
|
|
764 |
].
|
|
765 |
cl := (l * 3) rounded / 3.0.
|
|
766 |
cs := (s * 3) rounded / 3.0.
|
|
767 |
|
|
768 |
((ch = rh) and:[cs = rs]) ifTrue:[
|
|
769 |
"found a color with same saturation and same hue, keep for light"
|
|
770 |
|
|
771 |
cl > rl ifTrue:[
|
|
772 |
hiL isNil ifTrue:[
|
|
773 |
hiL := aColor.
|
|
774 |
hiValL := cl
|
|
775 |
] ifFalse:[
|
|
776 |
cl < hiValL ifTrue:[
|
|
777 |
hiL := aColor.
|
|
778 |
hiValL := cl
|
|
779 |
]
|
|
780 |
].
|
|
781 |
] ifFalse:[
|
|
782 |
lowL isNil ifTrue:[
|
|
783 |
lowL := aColor.
|
|
784 |
lowValL := cl
|
|
785 |
] ifFalse:[
|
|
786 |
cl > lowValL ifTrue:[
|
|
787 |
lowL := aColor.
|
|
788 |
lowValL := cl
|
|
789 |
]
|
|
790 |
]
|
|
791 |
]
|
|
792 |
].
|
|
793 |
|
|
794 |
(((ch = rh) or:[ch == nil]) and:[cl = rl]) ifTrue:[
|
|
795 |
"found a color with same light and same hue, keep for saturation"
|
|
796 |
|
|
797 |
cs > rs ifTrue:[
|
|
798 |
hiS isNil ifTrue:[
|
|
799 |
hiS := aColor.
|
|
800 |
hiValS := cs
|
|
801 |
] ifFalse:[
|
|
802 |
cs < hiValS ifTrue:[
|
|
803 |
hiS := aColor.
|
|
804 |
hiValS := cs
|
|
805 |
]
|
|
806 |
].
|
|
807 |
] ifFalse:[
|
|
808 |
lowS isNil ifTrue:[
|
|
809 |
lowS := aColor.
|
|
810 |
lowValS := cs
|
|
811 |
] ifFalse:[
|
|
812 |
cs > lowValS ifTrue:[
|
|
813 |
lowS := aColor.
|
|
814 |
lowValS := cs
|
|
815 |
]
|
|
816 |
]
|
|
817 |
]
|
|
818 |
].
|
|
819 |
|
|
820 |
rh notNil ifTrue:[
|
|
821 |
cl = rl ifTrue:[
|
|
822 |
cs = rs ifTrue:[
|
|
823 |
ch notNil ifTrue:[
|
|
824 |
d := (ch - rh) abs.
|
|
825 |
d > 300 ifTrue:[
|
|
826 |
rh > 180 ifTrue:[
|
|
827 |
ch := ch + 360
|
|
828 |
] ifFalse:[
|
|
829 |
ch := ch - 360
|
|
830 |
].
|
|
831 |
].
|
|
832 |
ch > rh ifTrue:[
|
|
833 |
hiH isNil ifTrue:[
|
|
834 |
hiH := aColor.
|
|
835 |
hiValH := ch
|
|
836 |
] ifFalse:[
|
|
837 |
ch < hiValH ifTrue:[
|
|
838 |
hiH := aColor.
|
|
839 |
hiValH := ch
|
|
840 |
]
|
|
841 |
]
|
|
842 |
] ifFalse:[
|
|
843 |
lowH isNil ifTrue:[
|
|
844 |
lowH := aColor.
|
|
845 |
lowValH := ch
|
|
846 |
] ifFalse:[
|
|
847 |
ch > lowValH ifTrue:[
|
|
848 |
lowH := aColor.
|
|
849 |
lowValH := ch
|
|
850 |
]
|
|
851 |
]
|
|
852 |
]
|
|
853 |
]
|
|
854 |
]
|
|
855 |
]
|
|
856 |
]
|
|
857 |
]
|
|
858 |
]
|
|
859 |
].
|
|
860 |
|
|
861 |
"found bounds for light ?"
|
|
862 |
|
|
863 |
(lowL notNil and:[hiL notNil]) ifTrue:[
|
|
864 |
rl = lowValL ifTrue:[
|
|
865 |
^ aBlock value:lowL value:nil
|
|
866 |
].
|
|
867 |
^ self monoDitherFor:100 / ((hiValL - lowValL)/(rl - lowValL))
|
|
868 |
between:lowL
|
|
869 |
and:hiL
|
|
870 |
on:aDevice
|
|
871 |
into:aBlock
|
|
872 |
].
|
|
873 |
|
|
874 |
"found bounds for saturation?"
|
|
875 |
|
|
876 |
(lowS notNil and:[hiS notNil]) ifTrue:[
|
|
877 |
'saturation dither' printNewline.
|
|
878 |
|
|
879 |
^ self monoDitherFor:100 / ((hiValS - lowValS)/(rs - lowValS))
|
|
880 |
between:lowS
|
|
881 |
and:hiS
|
|
882 |
on:aDevice
|
|
883 |
into:aBlock
|
|
884 |
].
|
|
885 |
|
|
886 |
"found one for light, dither with black or white"
|
|
887 |
|
|
888 |
lowL notNil ifTrue:[
|
|
889 |
^ self monoDitherFor:100 / ((100 - lowValL)/(rl - lowValL))
|
|
890 |
between:lowL
|
|
891 |
and:White
|
|
892 |
on:aDevice
|
|
893 |
into:aBlock
|
|
894 |
].
|
|
895 |
|
|
896 |
hiL notNil ifTrue:[
|
|
897 |
^ self monoDitherFor:100 / ((hiValL - 0)/(rl - 0))
|
|
898 |
between:Black
|
|
899 |
and:hiL
|
|
900 |
on:aDevice
|
|
901 |
into:aBlock
|
|
902 |
].
|
|
903 |
|
|
904 |
"found bounds for hue ?"
|
|
905 |
|
|
906 |
(lowH notNil and:[hiH notNil]) ifTrue:[
|
|
907 |
'hue dither' printNewline.
|
|
908 |
hiValH < lowValH ifTrue:[
|
|
909 |
hiValH := hiValH + 360
|
|
910 |
].
|
|
911 |
|
|
912 |
d := hiValH - lowValH.
|
|
913 |
|
|
914 |
^ self monoDitherFor:100 / (d / (rh - lowValH))
|
|
915 |
between:lowH
|
|
916 |
and:hiH
|
|
917 |
on:aDevice
|
|
918 |
into:aBlock
|
|
919 |
].
|
|
920 |
|
|
921 |
^ aBlock value:nil value:nil
|
|
922 |
!
|
|
923 |
|
|
924 |
monoDitherFor:grey on:aDevice into:aBlock
|
|
925 |
"get a dither form or colorId for a grey color.
|
|
926 |
Returns 2 values (either color or ditherForm) through
|
|
927 |
aBlock."
|
|
928 |
|
|
929 |
^ self monoDitherFor:grey
|
|
930 |
between:Black and:White
|
|
931 |
on:aDevice into:aBlock
|
|
932 |
!
|
|
933 |
|
|
934 |
monoDitherFor:grey between:color1 and:color2 on:aDevice into:aBlock
|
|
935 |
"get a dither form or colorId for a grey color.
|
|
936 |
Returns 2 values (either color or ditherForm) through
|
|
937 |
aBlock."
|
|
938 |
|
|
939 |
|form bits color clr1 clr2
|
|
940 |
gr index|
|
|
941 |
|
|
942 |
"having forms with: [1 .. 31] of 64 pixels,
|
|
943 |
we get dithers for: 0, 1/64, 2/64, ... 32/64"
|
|
944 |
|
|
945 |
grey <= 50 ifTrue:[
|
|
946 |
clr1 := color1.
|
|
947 |
clr2 := color2.
|
|
948 |
gr := grey.
|
|
949 |
] ifFalse:[
|
|
950 |
clr1 := color2.
|
|
951 |
clr2 := color1.
|
|
952 |
gr := 100 - grey.
|
|
953 |
].
|
|
954 |
|
|
955 |
gr := gr * 64.
|
|
956 |
index := (gr // 100) asInteger.
|
|
957 |
index < 1 ifTrue:[
|
|
958 |
color := color1 exactOn:aDevice.
|
|
959 |
] ifFalse:[
|
|
960 |
index > 63 ifTrue:[
|
|
961 |
color := color2 exactOn:aDevice
|
|
962 |
] ifFalse:[
|
|
963 |
bits := Form ditherBitsForXin64:index
|
|
964 |
]
|
|
965 |
].
|
|
966 |
bits notNil ifTrue:[
|
|
967 |
form := Form width:8 height:8 fromArray:bits on:aDevice.
|
|
968 |
form colorMap:(Array with:(clr1 exactOn:aDevice)
|
|
969 |
with:(clr2 exactOn:aDevice))
|
|
970 |
].
|
|
971 |
^ aBlock value:color value:form
|
|
972 |
!
|
|
973 |
|
|
974 |
dither2PlaneFor:grey on:aDevice into:aBlock
|
|
975 |
"get a dither form or colorId for a grey color.
|
|
976 |
Returns 2 values (either color or ditherForm) through
|
|
977 |
aBlock.
|
|
978 |
This code optimized for 2-plane displays (NeXT),
|
|
979 |
- must be generalized for any number of planes."
|
|
980 |
|
|
981 |
|form color
|
|
982 |
gr "{ Class:SmallInteger }"
|
|
983 |
color1 color2 low high scaled|
|
|
984 |
|
|
985 |
gr := grey asInteger.
|
|
986 |
|
|
987 |
gr <= 1 ifTrue:[
|
|
988 |
color := Black exactOn:aDevice
|
|
989 |
] ifFalse:[
|
|
990 |
(gr between:32 and:34) ifTrue:[
|
|
991 |
color := (Color grey:33) exactOn:aDevice
|
|
992 |
] ifFalse:[
|
|
993 |
(gr between:66 and:68) ifTrue:[
|
|
994 |
color := (Color grey:67) exactOn:aDevice
|
|
995 |
] ifFalse:[
|
|
996 |
gr >= 99 ifTrue:[
|
|
997 |
color := White exactOn:aDevice
|
|
998 |
]
|
|
999 |
]
|
|
1000 |
]
|
|
1001 |
].
|
|
1002 |
|
|
1003 |
color notNil ifTrue:[
|
|
1004 |
^ aBlock value:color value:nil
|
|
1005 |
].
|
|
1006 |
|
|
1007 |
(gr between:0 and:33) ifTrue:[
|
|
1008 |
color1 := Black on:aDevice.
|
|
1009 |
color2 := (Color grey:33) on:aDevice.
|
|
1010 |
] ifFalse:[
|
|
1011 |
(gr between:34 and:66) ifTrue:[
|
|
1012 |
color1 := (Color grey:33) on:aDevice.
|
|
1013 |
color2 := (Color grey:67) on:aDevice.
|
|
1014 |
] ifFalse:[
|
|
1015 |
color1 := (Color grey:67) on:aDevice.
|
|
1016 |
color2 := White on:aDevice.
|
|
1017 |
]
|
|
1018 |
].
|
|
1019 |
low := color1 red.
|
|
1020 |
high := color2 red.
|
|
1021 |
|
|
1022 |
"scale gr in between low..high"
|
|
1023 |
scaled := ((gr - low) * 100 / (high - low)) rounded.
|
|
1024 |
|
|
1025 |
^ self monoDitherFor:scaled
|
|
1026 |
between:color1
|
|
1027 |
and:color2
|
|
1028 |
on:aDevice
|
|
1029 |
into:aBlock.
|
|
1030 |
! !
|
|
1031 |
|
|
1032 |
!Color methodsFor:'getting a device color'!
|
|
1033 |
|
|
1034 |
on:aDevice
|
|
1035 |
"create a new Color representing the same color as
|
|
1036 |
myself on aDevice; if one already exists, return the one"
|
|
1037 |
|
|
1038 |
|newColor index id grey form sav|
|
|
1039 |
|
|
1040 |
"if Iam already assigned to that device ..."
|
|
1041 |
(device == aDevice) ifTrue:[^ self].
|
|
1042 |
|
|
1043 |
"the is a special case for pseudo-colors (0 and 1 in bitmaps)"
|
|
1044 |
(redVal isNil and:[colorId notNil]) ifTrue:[^ self].
|
|
1045 |
|
|
1046 |
"want to release color ?"
|
|
1047 |
(aDevice isNil and:[device notNil and:[colorId notNil]]) ifTrue:[
|
|
1048 |
(device notNil and:[colorId notNil]) ifTrue:[
|
|
1049 |
device freeColor:colorId
|
|
1050 |
].
|
|
1051 |
device := nil.
|
|
1052 |
colorId := nil.
|
|
1053 |
|
|
1054 |
"have to tell lobby - otherwise it keeps old info around"
|
|
1055 |
lobby changed:self.
|
|
1056 |
^ self
|
|
1057 |
].
|
|
1058 |
|
|
1059 |
newColor := Color existingColorRed:redVal green:greenVal blue:blueVal on:aDevice.
|
|
1060 |
newColor notNil ifTrue:[^ newColor].
|
|
1061 |
|
|
1062 |
aDevice hasColors ifTrue:[
|
|
1063 |
"ask that device for the exact color"
|
|
1064 |
|
|
1065 |
id := aDevice colorRed:redVal green:greenVal blue:blueVal.
|
|
1066 |
id isNil ifTrue:[
|
|
1067 |
"this is a kludge: scavenge to free unused colors
|
|
1068 |
and try again ..."
|
|
1069 |
ObjectMemory scavenge.
|
|
1070 |
id := aDevice colorRed:redVal green:greenVal blue:blueVal
|
|
1071 |
].
|
|
1072 |
id isNil ifTrue:[
|
|
1073 |
"no such color, look for a near-by one"
|
|
1074 |
|
|
1075 |
"
|
|
1076 |
newColor := Color colorNearRed:redVal green:greenVal blue:blueVal on:aDevice.
|
|
1077 |
newColor notNil ifTrue:[^ newColor].
|
|
1078 |
"
|
|
1079 |
|
|
1080 |
"no such color - try color dithers"
|
|
1081 |
self ditherRed:redVal green:greenVal blue:blueVal on:aDevice
|
|
1082 |
into:[:c :f | newColor := c. form := f].
|
|
1083 |
newColor notNil ifTrue:[^ newColor].
|
|
1084 |
]
|
|
1085 |
].
|
|
1086 |
|
|
1087 |
(id isNil and:[form isNil]) ifTrue:[
|
|
1088 |
"still no result - try greying"
|
|
1089 |
|
|
1090 |
grey := (0.3 * redVal) + (0.6 * greenVal) + (0.1 * blueVal).
|
|
1091 |
"avoid things like 100.00000001"
|
|
1092 |
grey := ((grey * 100) rounded) / 100.0.
|
|
1093 |
|
|
1094 |
((grey = 0) or:[(grey = 100) or:[aDevice hasGreyscales]]) ifTrue:[
|
|
1095 |
"kludge for 2-plane display - dither using 4 grey levels"
|
|
1096 |
|
|
1097 |
(aDevice depth == 2) ifTrue:[
|
|
1098 |
grey := grey rounded.
|
|
1099 |
self dither2PlaneFor:grey on:aDevice
|
|
1100 |
into:[:c :f | newColor := c. form := f].
|
|
1101 |
newColor notNil ifTrue:[^ newColor].
|
|
1102 |
] ifFalse:[
|
|
1103 |
id := aDevice colorRed:grey green:grey blue:grey.
|
|
1104 |
id isNil ifTrue:[
|
|
1105 |
ObjectMemory scavenge.
|
|
1106 |
id := aDevice colorRed:redVal green:greenVal blue:blueVal
|
|
1107 |
].
|
|
1108 |
]
|
|
1109 |
].
|
|
1110 |
|
|
1111 |
"now we have either a form (2-plane dithering)
|
|
1112 |
or an id (a real color).
|
|
1113 |
if both are nil, fall back to very simple dithering"
|
|
1114 |
|
|
1115 |
(form isNil and:[id isNil]) ifTrue:[
|
|
1116 |
self monoDitherFor:grey on:aDevice
|
|
1117 |
into:[:c :f | newColor := c. form := f].
|
|
1118 |
newColor notNil ifTrue:[^ newColor].
|
|
1119 |
].
|
|
1120 |
].
|
|
1121 |
|
|
1122 |
device isNil ifTrue:[
|
|
1123 |
"receiver was not associated - do it now"
|
|
1124 |
device := aDevice.
|
|
1125 |
id isNil ifTrue:[
|
|
1126 |
ditherForm := form
|
|
1127 |
].
|
|
1128 |
colorId := id.
|
|
1129 |
|
|
1130 |
"have to tell lobby - otherwise it keeps old info around"
|
|
1131 |
lobby changed:self.
|
|
1132 |
^ self
|
|
1133 |
].
|
|
1134 |
|
|
1135 |
"receiver was already associated to another device - need a new color"
|
|
1136 |
newColor := (self class basicNew) setRed:redVal green:greenVal blue:blueVal device:aDevice.
|
|
1137 |
id isNil ifTrue:[
|
|
1138 |
newColor ditherForm:form
|
|
1139 |
] ifFalse:[
|
|
1140 |
newColor colorId:id.
|
|
1141 |
].
|
|
1142 |
lobby register:newColor.
|
|
1143 |
^ newColor
|
|
1144 |
!
|
|
1145 |
|
|
1146 |
exactOn:aDevice
|
|
1147 |
"create a new Color representing the same color as
|
|
1148 |
myself on aDevice; if one already exists, return the one.
|
|
1149 |
Do not dither or otherwise approximate the color, but return
|
|
1150 |
nil, if the exact color is not available.
|
|
1151 |
Used to aquire primary colors for dithering, during startup."
|
|
1152 |
|
|
1153 |
|newColor index id|
|
|
1154 |
|
|
1155 |
"if Iam already assigned to that device ..."
|
|
1156 |
(device == aDevice) ifTrue:[^ self].
|
|
1157 |
|
|
1158 |
"first look if not already there"
|
|
1159 |
newColor := Color existingColorRed:redVal green:greenVal blue:blueVal on:aDevice.
|
|
1160 |
newColor notNil ifTrue:[^ newColor].
|
|
1161 |
|
|
1162 |
"ask that device for the color"
|
|
1163 |
id := aDevice colorRed:redVal green:greenVal blue:blueVal.
|
|
1164 |
id isNil ifTrue:[
|
|
1165 |
"this is a kludge: scavenge to free unused colors
|
|
1166 |
and try again ..."
|
|
1167 |
ObjectMemory scavenge.
|
|
1168 |
id := aDevice colorRed:redVal green:greenVal blue:blueVal
|
|
1169 |
].
|
|
1170 |
id isNil ifTrue:[
|
|
1171 |
"no such color - fail"
|
|
1172 |
|
|
1173 |
^ nil
|
|
1174 |
].
|
|
1175 |
|
|
1176 |
"receiver was not associated - do it now"
|
|
1177 |
device isNil ifTrue:[
|
|
1178 |
device := aDevice.
|
|
1179 |
colorId := id.
|
|
1180 |
|
|
1181 |
"have to tell lobby - otherwise it keeps old info around"
|
|
1182 |
lobby changed:self.
|
|
1183 |
^ self
|
|
1184 |
].
|
|
1185 |
|
|
1186 |
"receiver was already associated to another device - need a new color"
|
|
1187 |
newColor := (self class basicNew) setRed:redVal green:greenVal blue:blueVal device:aDevice.
|
|
1188 |
newColor colorId:id.
|
|
1189 |
lobby register:newColor.
|
|
1190 |
^ newColor
|
|
1191 |
!
|
|
1192 |
|
|
1193 |
nearestOn:aDevice error:error
|
|
1194 |
"create a new Color representing the same color as myself on aDevice;
|
|
1195 |
if one already exists, return the one. If no exact match is found,
|
|
1196 |
search for one with an error less than the argument error (in percent)."
|
|
1197 |
|
|
1198 |
|newColor index id|
|
|
1199 |
|
|
1200 |
"if Iam already assigned to that device ..."
|
|
1201 |
(device == aDevice) ifTrue:[^ self].
|
|
1202 |
|
|
1203 |
"first look if not already there"
|
|
1204 |
newColor := Color nearestColorRed:redVal green:greenVal blue:blueVal
|
|
1205 |
error:error on:aDevice.
|
|
1206 |
newColor notNil ifTrue:[^ newColor].
|
|
1207 |
|
|
1208 |
"ask that device for the color"
|
|
1209 |
id := aDevice colorRed:redVal green:greenVal blue:blueVal.
|
|
1210 |
id isNil ifTrue:[
|
|
1211 |
"this is a kludge: scavenge to free unused colors
|
|
1212 |
and try again ..."
|
|
1213 |
ObjectMemory scavenge.
|
|
1214 |
id := aDevice colorRed:redVal green:greenVal blue:blueVal
|
|
1215 |
].
|
|
1216 |
id isNil ifTrue:[
|
|
1217 |
"no color - fail"
|
|
1218 |
|
|
1219 |
^ nil
|
|
1220 |
].
|
|
1221 |
|
|
1222 |
"receiver was not associated - do it now"
|
|
1223 |
device isNil ifTrue:[
|
|
1224 |
device := aDevice.
|
|
1225 |
colorId := id.
|
|
1226 |
|
|
1227 |
"have to tell lobby - otherwise it keeps old info around"
|
|
1228 |
lobby changed:self.
|
|
1229 |
^ self
|
|
1230 |
].
|
|
1231 |
|
|
1232 |
"receiver was already associated to another device - need a new color"
|
|
1233 |
newColor := (self class basicNew) setRed:redVal green:greenVal blue:blueVal device:aDevice.
|
|
1234 |
newColor colorId:id.
|
|
1235 |
lobby register:newColor.
|
|
1236 |
^ newColor
|
|
1237 |
! !
|
|
1238 |
|
|
1239 |
!Color methodsFor:'comparing'!
|
|
1240 |
|
|
1241 |
= aColor
|
|
1242 |
"two colors are considered equal, if the color components are;
|
|
1243 |
independent of the device, the color is on"
|
|
1244 |
|
|
1245 |
(aColor isKindOf:Color) ifTrue:[
|
|
1246 |
(redVal = aColor red) ifTrue:[
|
|
1247 |
(greenVal = aColor green) ifTrue:[
|
|
1248 |
(blueVal = aColor blue) ifTrue:[
|
|
1249 |
^ true
|
|
1250 |
]
|
|
1251 |
]
|
|
1252 |
]
|
|
1253 |
].
|
|
1254 |
^ false
|
|
1255 |
! !
|
|
1256 |
|
|
1257 |
!Color methodsFor:'instance creation'!
|
|
1258 |
|
|
1259 |
darkened
|
|
1260 |
"return a new color, which is slightly darker than the receiver"
|
|
1261 |
|
|
1262 |
^ Color red:(redVal / 2) green:(greenVal / 2) blue:(blueVal / 2)
|
|
1263 |
|
|
1264 |
"(Color red) darkened"
|
|
1265 |
!
|
|
1266 |
|
|
1267 |
lightened
|
|
1268 |
"return a new color, which is slightly lighter than the receiver"
|
|
1269 |
|
|
1270 |
^ Color red:((100 - redVal) / 2 + redVal)
|
|
1271 |
green:((100 - greenVal) / 2 + greenVal)
|
|
1272 |
blue:((100 - blueVal) / 2 + blueVal)
|
|
1273 |
|
|
1274 |
"(Color red) lightened"
|
|
1275 |
! !
|
|
1276 |
|
|
1277 |
!Color methodsFor:'queries'!
|
|
1278 |
|
|
1279 |
isGreyColor
|
|
1280 |
"return true, if this color is a grey one -
|
|
1281 |
i.e. red = green = blue"
|
|
1282 |
|
|
1283 |
^ (redVal = greenVal) and:[redVal = blueVal]
|
|
1284 |
|
|
1285 |
"(Color grey:50) isGreyColor"
|
|
1286 |
"(Color red) isGreyColor"
|
|
1287 |
! !
|
|
1288 |
|
|
1289 |
!Color methodsFor:'accessing'!
|
|
1290 |
|
|
1291 |
red
|
|
1292 |
"return the red component in percent"
|
|
1293 |
|
|
1294 |
^ redVal
|
|
1295 |
!
|
|
1296 |
|
|
1297 |
green
|
|
1298 |
"return the green component in percent"
|
|
1299 |
|
|
1300 |
^ greenVal
|
|
1301 |
!
|
|
1302 |
|
|
1303 |
blue
|
|
1304 |
"return the blue component in percent"
|
|
1305 |
|
|
1306 |
^ blueVal
|
|
1307 |
!
|
|
1308 |
|
|
1309 |
greyIntensity
|
|
1310 |
"return the grey intensity in percent"
|
|
1311 |
|
|
1312 |
^ (0.3 * redVal) + (0.6 * greenVal) + (0.1 * blueVal)
|
|
1313 |
!
|
|
1314 |
|
|
1315 |
hue
|
|
1316 |
"return the hue"
|
|
1317 |
|
|
1318 |
self class withHLSFromRed:redVal green:greenVal blue:blueVal do:[:h :l :s |
|
|
1319 |
^ h
|
|
1320 |
]
|
|
1321 |
!
|
|
1322 |
|
|
1323 |
light
|
|
1324 |
"return the hue"
|
|
1325 |
|
|
1326 |
self class withHLSFromRed:redVal green:greenVal blue:blueVal do:[:h :l :s |
|
|
1327 |
^ l
|
|
1328 |
]
|
|
1329 |
!
|
|
1330 |
|
|
1331 |
saturation
|
|
1332 |
"return the hue"
|
|
1333 |
|
|
1334 |
self class withHLSFromRed:redVal green:greenVal blue:blueVal do:[:h :l :s |
|
|
1335 |
^ s
|
|
1336 |
]
|
|
1337 |
!
|
|
1338 |
|
|
1339 |
colorId
|
|
1340 |
"return the device-dependent color-id"
|
|
1341 |
|
|
1342 |
^ colorId
|
|
1343 |
!
|
|
1344 |
|
|
1345 |
ditherForm
|
|
1346 |
"return the form to dither the color"
|
|
1347 |
|
|
1348 |
^ ditherForm
|
|
1349 |
!
|
|
1350 |
|
|
1351 |
device
|
|
1352 |
"return the device I am on"
|
|
1353 |
|
|
1354 |
^ device
|
|
1355 |
!
|
|
1356 |
|
|
1357 |
deviceRedValue
|
|
1358 |
"return the value of the red component in device metrics"
|
|
1359 |
|
|
1360 |
^ device redComponentOfColor:colorId
|
|
1361 |
!
|
|
1362 |
|
|
1363 |
deviceGreenValue
|
|
1364 |
"return the value of the green component in device metrics"
|
|
1365 |
|
|
1366 |
^ device greenComponentOfColor:colorId
|
|
1367 |
!
|
|
1368 |
|
|
1369 |
deviceBlueValue
|
|
1370 |
"return the value of the blue component in device metrics"
|
|
1371 |
|
|
1372 |
^ device blueComponentOfColor:colorId
|
|
1373 |
!
|
|
1374 |
|
|
1375 |
deviceRedValue:r deviceGreenValue:g deviceBlueValue:b
|
|
1376 |
"set r/g/b components in device metrics"
|
|
1377 |
|
|
1378 |
device setColor:colorId red:r green:g blue:b
|
|
1379 |
! !
|
|
1380 |
|
|
1381 |
!Color methodsFor:'printing & storing'!
|
|
1382 |
|
|
1383 |
printString
|
|
1384 |
"return a string representing the receiver"
|
|
1385 |
|
|
1386 |
^ self storeString
|
|
1387 |
!
|
|
1388 |
|
|
1389 |
storeString
|
|
1390 |
"return a string representing an expression to reconstruct the receiver"
|
|
1391 |
|
|
1392 |
redVal isNil ifTrue:[
|
|
1393 |
colorId notNil ifTrue:[
|
|
1394 |
^ 'Color colorId:' , colorId storeString
|
|
1395 |
]
|
|
1396 |
].
|
|
1397 |
(redVal = greenVal and:[redVal = blueVal]) ifTrue:[
|
|
1398 |
^ 'Color grey:' , redVal storeString
|
|
1399 |
].
|
|
1400 |
^ 'Color red:' , redVal storeString ,
|
|
1401 |
' green:' , greenVal storeString ,
|
|
1402 |
' blue:' , blueVal storeString
|
|
1403 |
! !
|