On this page:
6.1 Core Color Functions
alpha-max
alpha-mask
split-rgb
split-argb
make-rgb
make-argb
extract-argb-alpha
set-argb-alpha
rgb-distance^2
is-argb?
6.2 Color Blending
rgb-average
rgb-average2
rgb-average4
argb-average
argb-average2
argb-average4
alpha-blend-alpha
rgb-blend-argb
rgb-blend-rgb+  alpha
rgb-blend-argb+  alpha
rgb-linear
argb-linear
rgb-bilinear
argb-bilinear
6.3 Color Names
CGA-COLORS
6.4 Flonum Colors
make-flrgb
make-flargb
split-flrgb
split-flargb
rgb->flrgb
flrgb->rgb
argb->flargb
flargb->argb
flrgb-add
flargb-add
flrgb-div
flargb-div
flrgb-mul
flargb-mul
6.5 Color Lighting
make-light
light-rgb
light-argb
light-add
6.6 Random Colors
random-rgb
random-argb
6.7 Color Interpolation
rgb+  add
make-rgb+
rgb+  >rgb
argb+  add
make-argb+
argb+  >argb
6.8 Fixed-Point Color Light
fxlight-rgb
fxlight-argb
8.10

6 RRLL: Color Support

Dominik Pantůček <dominik.pantucek@trustica.cz>

Racket Rogue-Like Library: Color Support

Various utilitis for color manipulation, blending and lighting.

6.1 Core Color Functions

 (require rrll/color/core) package: rrll-color

Requiring this module raises error if the system word size is less than 64 bits.

value

alpha-max : fixnum? = #xff

Defines the maximum alpha channel value for 32-bit ARGB color representation.

value

alpha-mask : fixnum? = #xff000000

Bit mask of the alpha channel part or 32-bit ARGB color representation.

procedure

(split-rgb color)  
fixnum? fixnum? fixnum?
  color : fixnum?
Splits given 24-bit RGB value into its 8-bit components.

procedure

(split-argb color)  
fixnum? fixnum? fixnum? fixnum?
  color : fixnum?
Splits given 32-bit ARGB value into its 8-bit components.

procedure

(make-rgb r g b)  fixnum?

  r : fixnum?
  g : fixnum?
  b : fixnum?
Creates a 24-bit RGB color from 8-bit components.

procedure

(make-argb a r g b)  fixnum?

  a : fixnum?
  r : fixnum?
  g : fixnum?
  b : fixnum?
Creates a 32-bit ARGB color from 8-bit components.

procedure

(extract-argb-alpha argb)  fixnum?

  argb : fixnum?
Extracts only alpha-channel value from given 32-bit ARGB color.

procedure

(set-argb-alpha argb alpha)  fixnum?

  argb : fixnum?
  alpha : fixnum?
Modifies given ARGB value and returns a new one with the alpha channel replaced by the explicit value given.

procedure

(rgb-distance^2 c1 c2)  fixnum?

  c1 : fixnum?
  c2 : fixnum?
Computes the square of euclidean distance in RGB space of the two colors given.

d=(r_1-r_2)^2+(g_1-g_2)^2+(b_1-b_2)^2

procedure

(is-argb? -rgb)  boolean?

  -rgb : fixnum?
Returns #t if the given -rgb value has non-zero alpha channel.

6.2 Color Blending

 (require rrll/color/blending) package: rrll-color

procedure

(rgb-average color ...)  fixnum?

  color : fixnum?
Averages given RGB values.

procedure

(rgb-average2 c0 c1)  fixnum?

  c0 : fixnum?
  c1 : fixnum?
Optimized implementation that averages two RGB values.

procedure

(rgb-average4 c0 c1 c2 c3)  fixnum?

  c0 : fixnum?
  c1 : fixnum?
  c2 : fixnum?
  c3 : fixnum?
Optimized implementation that averages four RGB values.

procedure

(argb-average color ...)  fixnum?

  color : fixnum?
Averages given ARGB values.

procedure

(argb-average2 c0 c1)  fixnum?

  c0 : fixnum?
  c1 : fixnum?
Optimized implementation that averages two ARGB values.

procedure

(argb-average4 c0 c1 c2 c3)  fixnum?

  c0 : fixnum?
  c1 : fixnum?
  c2 : fixnum?
  c3 : fixnum?
Optimized implementation that averages four ARGB values.

procedure

(alpha-blend-alpha a1 a2)  fixnum?

  a1 : fixnum?
  a2 : fixnum?
Blends alpha2 alpha value over alpha1 value using:

\alpha=\alpha_1\cdot\alpha_2

\alpha_1,\alpha_2\in\langle 0;255\rangle \equiv \langle 0.0;1.0\rangle

procedure

(rgb-blend-argb rgb argb)  fixnum?

  rgb : fixnum?
  argb : fixnum?
Blends argb over rgb based on the alpha channel value.

procedure

(rgb-blend-rgb+alpha rgb0 rgb1 alpha)  fixnum?

  rgb0 : fixnum?
  rgb1 : fixnum?
  alpha : fixnum?
Blends rgb1 over rgb0 based on the alpha value.

\alpha\in\langle 0;255\rangle\equiv\langle 0.0;1.0\rangle

procedure

(rgb-blend-argb+alpha rgb argb alpha)  fixnum?

  rgb : fixnum?
  argb : fixnum?
  alpha : fixnum?
Blends argb over rgb based on the combined alpha channel and given alpha value.

\alpha\in\langle 0;255\rangle\equiv\langle 0.0;1.0\rangle

Uses alpha-blend-alpha to merge argb’s alpha channel with the alpha value.

procedure

(rgb-linear c0 c1 f)  fixnum?

  c0 : fixnum?
  c1 : fixnum?
  f : fixnum?
Linearly interpolates two RGB colors using given f factor.

f\in\langle 0;256\rangle\equiv\langle 0.0;1.0\rangle

procedure

(argb-linear c0 c1 f)  fixnum?

  c0 : fixnum?
  c1 : fixnum?
  f : fixnum?
Linearly interpolates two ARGB colors using given f factor.

f\in\langle 0;256\rangle\equiv\langle 0.0;1.0\rangle

procedure

(rgb-bilinear r0c0 r0c1 r1c0 r1c1 u v)  fixnum?

  r0c0 : fixnum?
  r0c1 : fixnum?
  r1c0 : fixnum?
  r1c1 : fixnum?
  u : fixnum?
  v : fixnum?
Performs bilinear interpolation between four RGB colors.

u,v\in\langle 0;256\rangle\equiv\langle 0.0;1.0\rangle

procedure

(argb-bilinear r0c0 r0c1 r1c0 r1c1 u v)  fixnum?

  r0c0 : fixnum?
  r0c1 : fixnum?
  r1c0 : fixnum?
  r1c1 : fixnum?
  u : fixnum?
  v : fixnum?
Performs bilinear interpolation between four ARGB colors.

u,v\in\langle 0;256\rangle\equiv\langle 0.0;1.0\rangle

6.3 Color Names

 (require rrll/color/names) package: rrll-color

The following 16 fixnum? constants are defined:

BLACK BLUE GREEN CYAN RED MAGENTA BROWN LIGHTGRAY DARKGRAY LIGHTBLUE LIGHTGREEN LIGHTCYAN LIGHTRED LIGHTMAGENTA YELLOW WHITE

value

CGA-COLORS : (vectorof fixnum?)

Contains the ordered vector of 16 aforementioned CGA colors.

6.4 Flonum Colors

 (require rrll/color/flcolor) package: rrll-color

This module provides flonum?-based RGB and ARGB processing used when color interpolation is required.

procedure

(make-flrgb r g b)  flvector?

  r : flonum?
  g : flonum?
  b : flonum?
Creates new RGB triplet. The result is (flvector b g r) to keep the in-memory ordering the same as with byte-channel RGB.

procedure

(make-flargb a r g b)  flvector?

  a : flonum?
  r : flonum?
  g : flonum?
  b : flonum?
Creates new RGB triplet. The result is (flvector b g r a) to keep the in-memory ordering the same as with byte-channel ARGB.

procedure

(split-flrgb fc)  
flonum? flonum? flonum?
  fc : flvector?
Returns the red, green and blue channels as separate values. See make-flrgb for the in-memory data ordering.

procedure

(split-flargb fc)  
flonum? flonum? flonum? flonum?
  fc : flvector?
Returns the alpha, red, green and blue channels as separate values. See make-flargb for the in-memory data ordering.

procedure

(rgb->flrgb c)  flvector?

  c : fixnum?
Convers given RGB color (see make-rgb) to flvector? RGB represenation (see make-flrgb).

procedure

(flrgb->rgb fc)  fixnum?

  fc : flvector?
Converts given flonum? RGB color to single-fixnum? color. See make-flrgb for the former and make-rgb for the latter.

procedure

(argb->flargb c)  flvector?

  c : fixnum?
Convers given ARGB color (see make-argb) to flvector? ARGB represenation (see make-flargb).

procedure

(flargb->argb fc)  fixnum?

  fc : flvector?
Converts given flonum? ARGB color to single-fixnum? color. See make-flargb for the former and make-argb for the latter.

procedure

(flrgb-add fc1 fc2)  flvector?

  fc1 : flvector?
  fc2 : flvector?
Adds two flonum? RGB triplets together. No clamping is performed.

procedure

(flargb-add fc1 fc2)  flvector?

  fc1 : flvector?
  fc2 : flvector?
Adds two flonum? ARGB quadruplets together. No clamping is performed.

procedure

(flrgb-div fc n)  flvector?

  fc : flvector?
  n : flonum?
Divides all components of a flonum? RGB color by given number.

procedure

(flargb-div fc n)  flvector?

  fc : flvector?
  n : flonum?
Divides all components of a flonum? ARGB color by given number.

procedure

(flrgb-mul fc n)  flvector?

  fc : flvector?
  n : flonum?
Multiplies all components of a flonum? RGB color by given number.

procedure

(flargb-mul fc n)  flvector?

  fc : flvector?
  n : flonum?
Multiplies all components of a flonum? ARGB color by given number.

6.5 Color Lighting

 (require rrll/color/lighting) package: rrll-color

This module provides reference implementaiton of colored lighting routines to be used by the renderer mostly for Goraud shading.

procedure

(make-light r g b)  flvector?

  r : flonum?
  g : flonum?
  b : flonum?
Creates a colored light data structure of three fload numbers in single flvector?.

procedure

(light-rgb rgb l)  fixnum?

  rgb : fixnum?
  l : flvector?
Applies colored light to given color value and returns the lit color.

Each component is adjusted according to:

c=c_0\cdot l

The result is fxclamped in \langle 0;255\rangle.

procedure

(light-argb argb l)  fixnum?

  argb : fixnum?
  l : flvector?
Applies colored light to given color value and returns the lit color like light-rgb. The alpha component of the color is kept unchanged.

procedure

(light-add l1 l2)  flvector?

  l1 : flvector?
  l2 : flvector?
An alias for flrgb-add.

6.6 Random Colors

 (require rrll/color/random) package: rrll-color

Helper module for random RGB and ARGB colors where needed.

procedure

(random-rgb)  fixnum?

Generates valid random RGB color.

procedure

(random-argb)  fixnum?

Generates valid random ARGB quadruplet.

6.7 Color Interpolation

 (require rrll/color/interpolate) package: rrll-color

This module implements a special format with more bits per channel to allow simple iteration over (possibly multi-dimensional) field of color values.

procedure

(rgb+add rgb+1 rgb+2)  fixnum?

  rgb+1 : fixnum?
  rgb+2 : fixnum?
Adds two RGB+ values maintaining the bit format.

procedure

(make-rgb+ r g b)  fixnum?

  r : flonum?
  g : flonum?
  b : flonum?
Returns RGB+ representation of given RGB triplet.

procedure

(rgb+>rgb rgb+)  fixnum?

  rgb+ : fixnum?
Converts to standard RGB representation.

procedure

(argb+add argb+1 argb+2)  fixnum?

  argb+1 : fixnum?
  argb+2 : fixnum?
Adds two ARGB+ values maintaining the bit format.

procedure

(make-argb+ a r g b)  fixnum?

  a : flonum?
  r : flonum?
  g : flonum?
  b : flonum?
Returns ARGB+ representation of given ARGB quadruplet.

procedure

(argb+>argb argb+)  fixnum?

  argb+ : fixnum?
Converts to standard ARGB representation.

6.8 Fixed-Point Color Light

 (require rrll/color/fxlight) package: rrll-color

procedure

(fxlight-rgb rgbc rgbl)  fixnum?

  rgbc : fixnum?
  rgbl : fixnum?
Lights given rgbc color using the rgbl light. The light components are interpreted as follows:

These values were chosen purely for performance reasons.

procedure

(fxlight-argb argbc rgbl)  fixnum?

  argbc : fixnum?
  rgbl : fixnum?
Lights the argbc color using rgbl light, preserving the alpha channel of the source.