Squeak Class Documentation category index | class index  
 
Color
  category: Graphics-Primitives
  superclass: Object
  subclasses: TranslucentColor

This class represents abstract color, regardless of the depth of bitmap it will be shown in. At the very last moment a Color is converted to a pixelValue that depends on the depth of the actual Bitmap inside the Form it will be used with. The supported depths (in bits) are 1, 2, 4, 8, 16, and 32. The number of actual colors at these depths are: 2, 4, 16, 256, 32768, and 16 million. (See comment in BitBlt.) To change the depth of the Display and set how many colors you can see, execute: (Display newDepth: 8). (See comment in DisplayMedium)
Color is represented as the amount of light in red, green, and blue. White is (1.0, 1.0, 1.0) and black is (0, 0, 0). Pure red is (1.0, 0, 0). These colors are "additive". Think of Color's instance variables as:
r amount of red, a Float between 0.0 and 1.0.
g amount of green, a Float between 0.0 and 1.0.
b amount of blue, a Float between 0.0 and 1.0.
(But, in fact, the three are encoded as values from 0 to 1023 and combined in a single integer, rgb. The user does not need to know this.)
Many colors are named. You find a color by name by sending a message to class Color, for example (Color lightBlue). Also, (Color red: 0.2 green: 0.6 blue: 1.0) or (Color r: 0.2 g: 0.6 b: 1.0) creates a color. (see below)
A color is essentially immutable. Once you set red, green, and blue, you cannot change them. Instead, create a new Color and use it.
Applications such as contour maps and bar graphs will want to display one of a set of shades based on a number. Convert the range of this number to an integer from 1 to N. Then call (Color green lightShades: N) to get an Array of colors from white to green. Use the Array messages at:, atPin:, or atWrap: to pull out the correct color from the array. atPin: gives the first (or last) color if the index is out of range. atWrap: wraps around to the other end if the index is out of range.
Here are some fun things to run in when your screen has color:
Pen new mandala: 30 diameter: Display height-100.
Pen new web "Draw with the mouse, opt-click to end"
Display fillWhite. Pen new hilberts: 5.
Form toothpaste: 30 "Draw with mouse, opt-click to end"
You might also want to try the comment in
Form>class>examples>tinyText...


Messages:
mixed: proportion with: aColor Answer this color mixed with the given color additively. The proportion, a number between 0.0 and 1.0, determines what what fraction of the receiver to use in the mix.

+ add two colors
- subtract two colors
* multiply the values of r, g, b by a number or an Array of factors. ((Color named: #white) * 0.3) gives a darkish gray. (aColor * #(0 0 0.9)) gives a color with slightly less blue.
/ divide a color by a factor or an array of three factors.

errorForDepth: d How close the nearest color at this depth is to this abstract color. Sum of the squares of the RGB differences, square rooted and normalized to 1.0. Multiply by 100 to get percent.

hue Returns the hue of the color. On a wheel from 0 to 360 with pure red at 0 and again at 360.
saturation Returns the saturation of the color. 0.0 to 1.0
brightness Returns the brightness of the color. 0.0 to 1.0

name Look to see if this Color has a name.
display Show a swatch of this color tracking the cursor.

lightShades: thisMany An array of thisMany colors from white to the receiver.
darkShades: thisMany An array of thisMany colors from black to the receiver. Array is of length num.
mix: color2 shades: thisMany An array of thisMany colors from the receiver to color2.
wheel: thisMany An array of thisMany colors around the color wheel starting and ending at the receiver.

pixelValueForDepth: d Returns the bits that appear be in a Bitmap of this depth for this color. Represents the nearest available color at this depth. Normal users do not need to know which pixelValue is used for which color.

Messages to Class Color.
red: r green: g blue: b Return a color with the given r, g, and b components.
r: g: b: Same as above, for fast typing.

hue: h saturation: s brightness: b Create a color with the given hue, saturation, and brightness.

pink
blue
red ... Many colors have messages that return an instance of Color.
canUnderstand: #brown Returns true if #brown is a defined color.
names An OrderedCollection of the names of the colors.
named: #notAllThatGray put: aColor Add a new color to the list and create an access message and a class variable for it.
fromUser Shows the palette of colors available at this display depth. Click anywhere to return the color you clicked on.

hotColdShades: thisMany An array of thisMany colors showing temperature from blue to red to white hot.

stdColorsForDepth: d An Array of colors available at this depth. For 16 bit and 32 bits, returns a ColorGenerator. It responds to at: with a Color for that index, simulating a very big Array.

colorFromPixelValue: value depth: d Returns a Color whose bit pattern (inside a Bitmap) at this depth is the number specified. Normal users do not need to use this.

(See also comments in these classes: Form, Bitmap, BitBlt, Pattern, MaskedForm.)

instance methods
  Morphic menu
  addFillStyleMenuItems:hand:from:
changeColorIn:event:

  access
  alpha
blue
brightness
green
hue
luminance
red
saturation

  conversions
  asB3DColor
asColor
asColorref
asNontranslucentColor
balancedPatternForDepth:
bitPatternForDepth:
closestPixelValue1
closestPixelValue2
closestPixelValue4
closestPixelValue8
dominantColor
halfTonePattern1
halfTonePattern2
indexInMap:
makeForegroundColor
pixelValue32
pixelValueForDepth:
pixelWordFor:filledWith:
pixelWordForDepth:
scaledPixelValue32

  copying
  veryDeepCopyWith:

  equality
  =
diff:
hash

  groups of shades
  darkShades:
lightShades:
mix:shades:
wheel:

  other
  colorForInsets
display
name
newTileMorphRepresentative
raisedColor
rgbTriplet

  printing
  byteEncode:
encodeForRemoteCanvas
encodePostscriptOn:
printOn:
shortPrintString
storeArrayOn:
storeArrayValuesOn:
storeOn:

  private
  attemptToMutateError
flushCache
privateAlpha
privateBlue
privateGreen
privateRGB
privateRed
setHue:saturation:brightness:
setPrivateRed:green:blue:
setRGB:
setRed:green:blue:
setRed:green:blue:range:

  queries
  basicType
isBitmapFill
isBlack
isColor
isGradientFill
isGray
isOpaque
isOrientedFill
isSolidFill
isTranslucent
isTranslucentColor
isTransparent

  transformations
  *
+
-
/
alpha:
alphaMixed:with:
atLeastAsLuminentAs:
dansDarker
darker
lighter
mixed:with:
muchLighter
negated
orColorUnlike:
slightlyDarker
slightlyLighter
twiceDarker
twiceLighter
veryMuchLighter

class methods
  class initialization
  initialize
initializeGrayToIndexMap
initializeHighLights
initializeIndexedColors
initializeNames
initializeTranslucentPatterns
named:put:

  color from user
  colorPaletteForDepth:extent:
colorTest:extent:colorMapper:
fromUser
oldColorPaletteForDepth:extent:

  colormaps
  cachedColormapFrom:to:
colorMapIfNeededFrom:to:
computeColormapFrom:to:
computeRGBColormapFor:bitsPerColor:

  examples
  colorRampForDepth:extent:
hotColdShades:
showColorCube
showColors:
showHSVPalettes
showHuesInteractively
wheel:
wheel:saturation:brightness:

  instance creation
  colorFrom:
colorFromPixelValue:depth:
fromArray:
fromRgbTriplet:
fromString:
gray:
h:s:v:
new
r:g:b:
r:g:b:alpha:
r:g:b:range:
random

  named colors
  black
blue
brown
cyan
darkGray
gray
green
lightBlue
lightBrown
lightCyan
lightGray
lightGreen
lightMagenta
lightOrange
lightRed
lightYellow
magenta
orange
paleBlue
paleBuff
paleGreen
paleMagenta
paleOrange
palePeach
paleRed
paleTan
paleYellow
red
tan
transparent
veryDarkGray
veryLightGray
veryPaleRed
veryVeryDarkGray
veryVeryLightGray
white
yellow

  other
  colorNames
indexedColors
maskingMap:
pixelScreenForDepth:
quickHighLight:
shutDown
translucentMaskFor:depth:

instance methods
  Morphic menu top  
 

addFillStyleMenuItems:hand:from:

Add the items for changing the current fill style of the receiver


 

changeColorIn:event:

Note: This is just a workaround to make sure we don't use the old color inst var


  access top  
 

alpha

Return the opacity ('alpha') value of opaque so that normal colors can be compared to TransparentColors.


 

blue

Return the blue component of this color, a float in the range [0.0..1.0].


 

brightness

Return the brightness of this color, a float in the range [0.0..1.0].


 

green

Return the green component of this color, a float in the range [0.0..1.0].


 

hue

Return the hue of this color, an angle in the range [0.0..360.0].


 

luminance

Return the luminance of this color, a brightness value weighted by the human eye's color sensitivity.


 

red

Return the red component of this color, a float in the range [0.0..1.0].


 

saturation

Return the saturation of this color, a value between 0.0 and 1.0.


  conversions top  
 

asB3DColor

Convert the receiver into a color


 

asColor

Convert the receiver into a color


 

asColorref

Convert the receiver into a colorref


 

asNontranslucentColor


 

balancedPatternForDepth:

A generalization of bitPatternForDepth: as it exists. Generates a 2x2 stipple of color.
The topLeft and bottomRight pixel are closest approx to this color


 

bitPatternForDepth:

Return a Bitmap, possibly containing a stipple pattern, that best represents this color at the given depth. BitBlt calls this method to convert colors into Bitmaps. The resulting Bitmap may be multiple words to represent a stipple pattern of several lines.


 

closestPixelValue1

Return the nearest approximation to this color for a monochrome Form.


 

closestPixelValue2

Return the nearest approximation to this color for a 2-bit deep Form.


 

closestPixelValue4

Return the nearest approximation to this color for a 4-bit deep Form.


 

closestPixelValue8

Return the nearest approximation to this color for an 8-bit deep Form.


 

dominantColor


 

halfTonePattern1

Return a halftone-pattern to approximate luminance levels on 1-bit deep Forms.


 

halfTonePattern2

Return a halftone-pattern to approximate luminance levels on 2-bit deep Forms.


 

indexInMap:

Return the index corresponding to this color in the given color map. RGB colors are truncated to 3-, 4-, or 5-bits per color component when indexing into such a colorMap.


 

makeForegroundColor

Make a foreground color contrasting with me


 

pixelValue32

Note: pixelWord not pixelValue so we include translucency


 

pixelValueForDepth:

Returns an integer representing the bits that appear in a single pixel of this color in a Form of the given depth. The depth must be one of 1, 2, 4, 8, 16, or 32. Contrast with pixelWordForDepth: and bitPatternForDepth:, which return either a 32-bit word packed with the given pixel value or a multiple-word Bitmap containing a pattern. The inverse is the class message colorFromPixelValue:depth:


 

pixelWordFor:filledWith:

Return to a 32-bit word that concatenates enough copies of the given pixel value to fill the word (i.e., 32/depth copies). Depth should be one of 1, 2, 4, 8, 16, or 32. The pixel value should be an integer in 0..2^depth-1.


 

pixelWordForDepth:

Return to a 32-bit word that concatenates enough copies of the receiver's pixel value to fill the word (i.e., 32/depth copies). Depth should be one of 1, 2, 4, 8, 16, or 32. The pixel value should be an integer in 0..2^depth-1.


 

scaledPixelValue32

Return the alpha scaled pixel value for depth 32


  copying top  
 

veryDeepCopyWith:

Return self. I am immutable in the Morphic world. Do not record me.


  equality top  
 

=

Return true if the receiver equals the given color. This method handles TranslucentColors, too.


 

diff:

Returns a number between 0.0 and 1.0


 

hash

Answer a SmallInteger whose value is related to the receiver's identity.
May be overridden, and should be overridden in any classes that define =


  groups of shades top  
 

darkShades:

An array of thisMany colors from black to the receiver. Array is of length num. Very useful for displaying color based on a variable in your program.


 

lightShades:

An array of thisMany colors from white to self. Very useful for displaying color based on a variable in your program.


 

mix:shades:

Return an array of thisMany colors from self to color2. Very useful for displaying color based on a variable in your program.


 

wheel:

An array of thisMany colors around the color wheel starting at self and ending all the way around the hue space just before self. Array is of length thisMany. Very useful for displaying color based on a variable in your program.


  other top  
 

colorForInsets


 

display

Show a swatch of this color tracking the cursor until the next mouseClick.


 

name

Return this color's name, or nil if it has no name. Only returns a name if it exactly matches the named color.


 

newTileMorphRepresentative


 

raisedColor


 

rgbTriplet

Color fromUser rgbTriplet


  printing top  
 

byteEncode:


 

encodeForRemoteCanvas


 

encodePostscriptOn:


 

printOn:

Append to the argument, aStream, a sequence of characters that
identifies the receiver.


 

shortPrintString

Return a short (but less precise) print string for use where space is tight.


 

storeArrayOn:


 

storeArrayValuesOn:


 

storeOn:

Append to the argument aStream a sequence of characters that is an
expression whose evaluation creates an object similar to the receiver.


  private top  
 

attemptToMutateError

A color is immutable. Once a color's red, green, and blue have been initialized, you cannot change them. Instead, create a new Color and use it.


 

flushCache

Flush my cached bit pattern.


 

privateAlpha

Private! Return the raw alpha value for opaque. Used only for equality testing.


 

privateBlue

Private! Return the internal representation of my blue component.


 

privateGreen

Private! Return the internal representation of my green component.
Replaced >> by bitShift: 0 -. SqR! 2/25/1999 23:08


 

privateRGB

Private! Return the internal representation of my RGB components.


 

privateRed

Private! Return the internal representation of my red component.


 

setHue:saturation:brightness:

Initialize this color to the given hue, saturation, and brightness. See the comment in the instance creation method for details.


 

setPrivateRed:green:blue:

Initialize this color's r, g, and b components to the given values in the range [0..ComponentMax]. Encoded in a single variable as 3 integers in [0..1023].


 

setRGB:


 

setRed:green:blue:

Initialize this color's r, g, and b components to the given values in the range [0.0..1.0]. Encoded in a single variable as 3 integers in [0..1023].


 

setRed:green:blue:range:

Initialize this color's r, g, and b components to the given values in the range [0..r].


  queries top  
 

basicType

Answer a symbol representing the inherent type of the receiver


 

isBitmapFill


 

isBlack

Return true if the receiver represents black


 

isColor

Answer true if receiver is a Color. False by default.


 

isGradientFill


 

isGray

Return true if the receiver represents a shade of gray


 

isOpaque


 

isOrientedFill

Return true if the receiver keeps an orientation (e.g., origin, direction, and normal)


 

isSolidFill


 

isTranslucent


 

isTranslucentColor

This means: self isTranslucent, but isTransparent not


 

isTransparent


  transformations top  
 

*

Answer this color with its RGB multiplied by the given number.


 

+

Answer this color mixed with the given color in an additive color space.


 

-

Answer aColor is subtracted from the given color in an additive color space.


 

/

Answer this color with its RGB divided by the given number.


 

alpha:

Answer a new Color with the given amount of opacity ('alpha').


 

alphaMixed:with:

Answer this color mixed with the given color. The proportion, a number
between 0.0 and 1.0, determines what what fraction of the receiver to
use in the mix. For example, 0.9 would yield a color close to the
receiver. This method uses RGB interpolation; HSV interpolation can lead
to surprises. Mixes the alphas (for transparency) also.


 

atLeastAsLuminentAs:


 

dansDarker

Return a darker shade of the same color.
An attempt to do better than the current darker method.


 

darker

Answer a darker shade of this color.


 

lighter

Answer a lighter shade of this color.


 

mixed:with:

Mix with another color and do not preserve transpareny. Only use this for extracting the RGB value and mixing it. All other callers should use instead:
aColor alphaMixed: proportion with: anotherColor


 

muchLighter


 

negated

Return an RGB inverted color


 

orColorUnlike:

If this color is a lot like theOther, then return its complement, otherwide, return self


 

slightlyDarker

Answer a slightly darker shade of this color.


 

slightlyLighter

Answer a slightly lighter shade of this color.


 

twiceDarker

Answer a significantly darker shade of this color.


 

twiceLighter

Answer a significantly lighter shade of this color.


 

veryMuchLighter


class methods
  class initialization top  
 

initialize

Color initialize


 

initializeGrayToIndexMap

Build an array of gray values available in the 8-bit colormap. This array is indexed by a gray level between black (1) and white (256) and returns the pixel value for the corresponding gray level.


 

initializeHighLights

Create a set of Bitmaps for quickly reversing areas of the screen without converting colors.


 

initializeIndexedColors

Build an array of colors corresponding to the fixed colormap used
for display depths of 1, 2, 4, or 8 bits.


 

initializeNames

Name some colors.


 

initializeTranslucentPatterns

Color initializeTranslucentPatterns


 

named:put:

Add a new color to the list and create an access message and a class variable for it. The name should start with a lowercase letter. (The class variable will start with an uppercase letter.) (Color colorNames) returns a list of all color names.


  color from user top  
 

colorPaletteForDepth:extent:

Display a palette of colors sorted horizontally by hue and vertically by lightness. Useful for eyeballing the color gamut of the display, or for choosing a color interactively.


 

colorTest:extent:colorMapper:

Create a palette of colors sorted horizontally by hue and vertically by lightness. Useful for eyeballing the color gamut of the display, or for choosing a color interactively.


 

fromUser

Displays a color palette of colors, waits for a mouse click, and returns the selected color. Any pixel on the Display can be chosen, not just those in the color palette.


 

oldColorPaletteForDepth:extent:

Returns a form of the given size showing a color palette for the given depth.


  colormaps top  
 

cachedColormapFrom:to:

Return a cached colormap for mapping between the given depths. Always return a real colormap, not nil; this allows the client to get an identity colormap that can then be copied and modified to do color transformations.


 

colorMapIfNeededFrom:to:

Return a colormap for mapping between the given depths, or nil if no colormap is needed.


 

computeColormapFrom:to:

Compute a colorMap for translating between the given depths. A colormap is a Bitmap whose entries contain the pixel values for the destination depth. Typical clients use cachedColormapFrom:to: instead.


 

computeRGBColormapFor:bitsPerColor:

Compute a colorMap for translating from 16-bit or 32-bit RGB color to the given depth, using the given number of of bits per color component.


  examples top  
 

colorRampForDepth:extent:

Returns a form of the given size showing R, G, B, and gray ramps for the given depth. Useful for testing color conversions between different depths.


 

hotColdShades:

An array of thisMany colors showing temperature from blue to red to white hot. (Later improve this by swinging in hue.)


 

showColorCube

Show a 12x12x12 color cube.


 

showColors:

Display the given collection of colors across the top of the Display.


 

showHSVPalettes

Shows a palette of hues, varying the saturation and brightness for each one. Best results are with depths 16 and 32.


 

showHuesInteractively

Shows a palette of hues at a (saturation, brightness) point determined by the mouse position. Click the mouse button to exit and return the selected (saturation, brightness) point.


 

wheel:

Return a collection of thisMany colors evenly spaced around the color wheel.


 

wheel:saturation:brightness:

Return a collection of thisMany colors evenly spaced around the color wheel, all of the given saturation and brightness.


  instance creation top  
 

colorFrom:

Return an instantiated color from parm. If parm is already a color, return it, else return the result of my performing it if it's a symbol or, if it is a list, it better be a list of symbols, the first of which is sent the to me and then the others of which are in turn sent to the prior result. Else just return the thing


 

colorFromPixelValue:depth:

Convert a pixel value for the given display depth into a color.


 

fromArray:


 

fromRgbTriplet:


 

fromString:

for HTML color spec: #FFCCAA or white/black


 

gray:

Return a gray shade with the given brightness in the range [0.0..1.0].


 

h:s:v:

Create a color with the given hue, saturation, and brightness. Hue is given as the angle in degrees of the color on the color circle where red is zero degrees. Saturation and brightness are numbers in [0.0..1.0] where larger values are more saturated or brighter colors. For example, (Color h: 0 s: 1 v: 1) is pure red.


 

new

Answer a new instance of the receiver (which is a class) with no indexable variables. Fail if the class is indexable.


 

r:g:b:

Return a color with the given r, g, and b components in the range [0.0..1.0].


 

r:g:b:alpha:


 

r:g:b:range:

Return a color with the given r, g, and b components specified as integers in the range [0..r]. This avoids the floating point arithmetic in the red:green:blue: message and is thus a bit faster for certain applications (such as computing a sequence of colors for a palette).


 

random

Return a random color that isn't too dark or under-saturated.


  named colors top  
 

black


 

blue


 

brown


 

cyan


 

darkGray


 

gray


 

green


 

lightBlue


 

lightBrown


 

lightCyan


 

lightGray


 

lightGreen


 

lightMagenta


 

lightOrange


 

lightRed


 

lightYellow


 

magenta


 

orange


 

paleBlue


 

paleBuff


 

paleGreen


 

paleMagenta


 

paleOrange


 

palePeach


 

paleRed


 

paleTan


 

paleYellow


 

red


 

tan


 

transparent


 

veryDarkGray


 

veryLightGray


 

veryPaleRed


 

veryVeryDarkGray


 

veryVeryLightGray


 

white


 

yellow


  other top  
 

colorNames

Return a collection of color names.


 

indexedColors


 

maskingMap:

Return a color map that maps all colors except transparent to words of all ones. Used to create a mask for a Form whose transparent pixel value is zero. Cache the most recently used map.


 

pixelScreenForDepth:

Return a 50% stipple containing alternating pixels of all-zeros and all-ones to be used as a mask at the given depth.


 

quickHighLight:

Quickly return a Bitblt-ready raw colorValue for highlighting areas. 6/22/96 tk


 

shutDown

Color shutDown


 

translucentMaskFor:depth:

Return a pattern representing a mask usable for stipple transparency