Next: , Up: Graphics   [Contents][Index]

5.3.1 Colors

Merriam-Webster defines color as “a phenomenon of light (such as red, brown, pink, or gray) or visual perception that enables one to differentiate otherwise identical objects.” In this essay, I will…

Okay, okay. We all know what colors are. Chickadee provides a data type to represent color and some convenient procedures to work with them in the (chickadee graphics color) module. Colors are made up of four components, or channels: red, green, blue, and alpha (transparency.) Each of these values is expressed as a uniform floating point value in the range [0, 1]. 0 means that color channel is unrepresented in the resulting color, whereas 1 means that color channel is at full intensity.

Making a color object is easy, and there’s a few ways to do it depending on what’s most convenient. The first is make-color, where you specify each channel exactly as described above. This is fully opaque magenta:

(make-color 1.0 0.0 1.0 1.0)

Many people are used to representing colors as 6 or 8 digit hexadecimal numbers, so Chickadee also allows that. Here’s magenta, again:

(rgba #xFF00FFFF)
(rgb #xFF00FF) ; equivalent to the above
Procedure: make-color r g b a

Return a new color object with a red value of r, a green value of g, a blue value of b, and an alpha (transparency) value of a. All values are clamped to the range [0, 1].

Procedure: rgba color-code

Return a new color object using the values of the first 32 bits of color-code. Each channel occupies 8 bits. Starting from the most significant bit, red is first, followed by green, then blue, then alpha. Color codes are often represented as 6 or 8 digit hexadecimal numbers in various other programs.

Procedure: rgb color-code

Like rgba, but color-code is a 24 bit code with no alpha channel.

Procedure: color? obj

Return #t if obj is a color object.

Procedure: color-r color

Return the red channel of color.

Procedure: color-g color

Return the green channel of color.

Procedure: color-b color

Return the blue channel of color.

Procedure: color-a color

Return the alpha channel of color.

Procedure: transparency alpha

Return a new color that is white (RGB channels set to 1) with an alpha channel value of alpha. This can be useful for creating a color that can be multiplied against another color to make it more transparent.

Procedure: string->color s

Convert the hexadecimal color code in the string s to a color object. The following string formats are supported:

(string->color "#FF00FFFF")
(string->color "FF00FFFF")
(string->color "#FF00FF")
(string->color "FF00FF")
Procedure: color* a b

Multiply the color a with the color or number b and return a new color with the result.

Procedure: color+ a b

Add the color a to the color b and return a new color with the result.

Procedure: color- a b

Subtract the color b from the color a and return a new color with the result.

Procedure: color-inverse color

Invert the red, green, and blue channels of color and return a new color with the result.

Procedure: color-lerp start end alpha

Linearly interpolate the colors start and end using the factor alpha, a number in the range [0, 1]. Stock Colors

For convenience, Chickadee comes with some basic colors predefined:

Variable: white
Variable: black
Variable: red
Variable: green
Variable: blue
Variable: yellow
Variable: magenta
Variable: cyan

For fun, there are also predefined colors for the classic Tango color palette.

Variable: tango-light-butter
Variable: tango-butter
Variable: tango-dark-butter
Variable: tango-light-orange
Variable: tango-orange
Variable: tango-dark-orange
Variable: tango-light-chocolate
Variable: tango-chocolate
Variable: tango-dark-chocolate
Variable: tango-light-chameleon
Variable: tango-chameleon
Variable: tango-dark-chameleon
Variable: tango-light-sky-blue
Variable: tango-sky-blue
Variable: tango-dark-sky-blue
Variable: tango-light-plum
Variable: tango-plum
Variable: tango-dark-plum
Variable: tango-light-scarlet-red
Variable: tango-scarlet-red
Variable: tango-dark-scarlet-red
Variable: tango-aluminium-1
Variable: tango-aluminium-2
Variable: tango-aluminium-3
Variable: tango-aluminium-4
Variable: tango-aluminium-5
Variable: tango-aluminium-6

Next: , Up: Graphics   [Contents][Index]