com.evocomputing.colors

Color manipulation routines. This code was heavily infuenced by the
color.rb module in the ruby SASS project, and the colorspace packages in R.


Further references:
HSL and HSV:
http://en.wikipedia.org/wiki/Luminance-Hue-Saturation
RGB color space:
http://en.wikipedia.org/wiki/RGB_color_space
http://en.wikipedia.org/wiki/Hue#Computing_hue_from_RGB
http://www.w3.org/TR/css3-color/#hsl-color

SASS color module:
http://github.com/nex3/haml/blob/master/lib/sass/script/color.rb

R colorspace package:
http://cran.r-project.org/web/packages/colorspace/index.html

->color

(->color rgba hsl)
Positional factory function for class com.evocomputing.colors.color.

adjust-alpha

(adjust-alpha color unit-float-adj)
Add an amount to the alpha (transparency) of the color, keeping it
within the allowable range of 0-255.

adjust-hue

(adjust-hue color degrees)
Shift the hue of the color around the color wheel by the specified
number of degrees. Will wrap around if it goes below 0.0 or above
360.0.

allowable-hsl-keys

The keyword arguments which may be used to create an HSL color.

allowable-rgb-keys

The keyword arguments which may be used to create an RGB color.

alpha

(alpha color)
Return the alpha (int) component of this color.

awt-color

(awt-color color)
Return a java.awt.Color object using this color's rgba components.

blue

(blue color)
Return the blue (int) component of this color.

check-hsl

(check-hsl hsl)(check-hsl h s l)
Check that every element is of the format:
- 1st, H (Hue): Float value in the range of: 0.0 - 360.0.
- 2nd, S (Saturation): Float value in the range: 0.0 - 100.0.
- 3rd, L (Lightness or Luminance): Float value in the range 0.0 - 100.0.

check-rgb

(check-rgb rgb)(check-rgb r g b)
Check that every element in the passed in rgba sequence is an
integer in the range 0 - 255.

check-rgba

(check-rgba rgba)(check-rgba r g b a)
Check that every element in the passed in rgba sequence is an
integer in the range 0 - 255.

circle-float?

(circle-float? fval)
Return true if passed in float is in the range 0.0 - 360.0, false otherwise.

clamp-hue

(clamp-hue hue)
Clamp the hue value so that it lies in the range 0.0 - 360.0.

clamp-percent-float

(clamp-percent-float pfloat)
Clamp the floating point value to be within the range 0 - 100.0.

clamp-rgb-int

(clamp-rgb-int rgb-int)
Clamp the integer value to be within the range 0 - 255, the legal
values for an RGB color component.

clamp-unit-float

(clamp-unit-float ufloat)
Clamp the floating point value to be within the range 0 - 1.0.

color-add

(color-add color1__3233__auto__ color2__3234__auto__)
Add the RGB values of two colors together, clamping each to a maximum of 255.

color-div

(color-div color1__3233__auto__ color2__3234__auto__)
Divide the RGB values of two colors.

color-epsilon

The maximum resolution for color math; if any value is less than or
equal to this value, it is treated as zero.

color-mult

(color-mult color1__3233__auto__ color2__3234__auto__)
Multiply the RGB values of two colors, clamping each to a maximum of 255

color-name

(color-name color)
If there is an entry for this color value in the symbolic color
names map, return that. Otherwise, return the hexcode string of this
color's rgba integer value.

color-sub

(color-sub color1__3233__auto__ color2__3234__auto__)
Subtract the RGB values of color2 from color 1, clamping each to a
minimum of 0.

color-tolerance

The tolerance for comparing the components of two colors. In general,
colors are considered equal if all of their components are within this
tolerance value of each other.

color=

(color= color1 color2)
Return true if rgba components are equal, and hsl float components
are within tolerance.

create-color

multimethod

Create a color struct using the passed in args.

This will create a color struct that has RGBA integer values in the range:
- R (Red): Integer in range 0 - 255
- G (Green): Integer in range 0 - 255
- B (Blue): Integer in range 0 - 255
- A (Alpha): Integer in range 0 - 255, with default as 255 (100% opacity)

And HSL values with the range:
- H (Hue): Float value in the range of: 0.0 - 360.0
- S (Saturation): Float value in the range: 0.0 - 100.0
- L (Lightness or Luminance): Float value in the range 0.0 - 100.0

This multimethod is very liberal in what it will accept to create a
color. Following is a list of acceptable formats:

Single Arg
- Symbolic: Either a string or keyword or symbol that matches an entry
in the symbolic color pallete. Currently, this is defaults to the
html4 colors map and x11 colors map, but the end user of this library
can set any named palette they want.

  examples:
  (create-color "blue")
  (create-color :blue)

- Hexstring: A hex string representation of an RGB(A) color
  examples:
  (create-color "0xFFCCAA")
  (create-color "#FFCCAA")
  (create-color "Ox80FFFF00") ;; alpha = 128

- Integer: An integer representation of an RGB(A) color
  examples:
  (create-color 0xFFCCAA) ;; integer in hexidecimal format
  (create-color 16764074) ;; same integer in decimal format

- Sequence or array of RGB(A) integers
  :examples
  (create-color [255 0 0])
  (create-color [255 0 0 128]) ;;alpha = 128

- Map of either RGB (A) kw/values or HSL(A) kw/values
  Allowable RGB keys: :r :red :g :green :b :blue
  Allowable HSL keys: :h :hue :s :saturation :l :lightness

  examples:
  (create-color {:r 255 :g 0 :blue 0})
  (create-color {:r 255 :g 0 :blue 0 :a 128})
  (create-color {:h 120.0 :s 100.0 :l 50.0})
  (create-color {:h 120.0 :s 100.0 :l 50.0 :a 128})

Multiple Arg
- Sequence or array of RGB(A) integers
  :examples
  (create-color 255 0 0)
  (create-color 255 0 0 128) ;;alpha = 128

- Assoc list of either RGB (A) kw/values or HSL(A) kw/values
  Allowable RGB keys: :r :red :g :green :b :blue
  Allowable HSL keys: :h :hue :s :saturation :l :lightness

  examples:
  (create-color :r 255 :g 0 :blue 0)
  (create-color :r 255 :g 0 :blue 0 :a 128)
  (create-color :h 120.0 :s 100.0 :l 50.0)
  (create-color :h 120.0 :s 100.0 :l 50.0 :a 128)

create-color-dispatch

(create-color-dispatch args)(create-color-dispatch arg & others)
Inspect the arguments and determine which version of the
create-color multimethod to invoke.

darken

(darken color percent)
Decrease the lightness of the color by the specified amount, down
to a minimum of 0.0.

desaturate

(desaturate color percent)
Decrease the saturation of the color by the specified amount, down
to a minimum of 0.0.

grayscale

(grayscale color)
Remove all hue from the color, converting it to a gray with the
same lightness level.

green

(green color)
Return the green (int) component of this color.

hexstring-to-rgba-int

(hexstring-to-rgba-int hexstr)
Convert a hexadecimal string to the corresponding array of RGBA
integer values.

hsl-to-rgb

(hsl-to-rgb hue saturation lightness)

hue

(hue color)
Return the hue (float) component of this color.

hue-to-rgb

(hue-to-rgb m1 m2 hue)
Convert hue color to rgb components
Based on algorithm described in:
http://en.wikipedia.org/wiki/Hue#Computing_hue_from_RGB
and:
http://www.w3.org/TR/css3-color/#hsl-color

lighten

(lighten color percent)
Increase the lightness of the color by the specified amount, up to
a maximum of 100.0.

lightness

(lightness color)
Return the lightness (float) component of this color.

map->color

(map->color m__7888__auto__)
Factory function for class com.evocomputing.colors.color, taking a map of keywords to field values.

maybe-convert-alpha

(maybe-convert-alpha alpha)
If alpha is a float value, try to convert to integer in range 0 -
255, otherwise return as-is.

mix

(mix color1 color2)(mix color1 color2 weight)
Produce a blend between two colors, optionally weighted by the
given percentage. Takes the average of each of the RGB components,
taking into account the opacity of each color. The weight specifies
the amount of the first color that should be included in the
returned color. The default value of 50.0 means that half the first
color and half the second color should be used. A value of 25.0
would count the second color three times as much as the first.

mix-hsl

(mix-hsl color1 color2)(mix-hsl color1 color2 weight)
Produce a blend between two colors in the HSL color space,
optionally weighted by the given percentage. Takes the average of
each of the HSL components (always going the shortest distance
around the hue circle), taking into account the opacity of each
color. The weight specifies the amount of the first color that
should be included in the returned color. The default value of 50.0
means that half the first color and half the second color should be
used. A value of 25.0 would count the second color three times as
much as the first.

named-colors-name-to-rgb

Maps the known color names to their RGB values.

named-colors-rgb-to-name

Maps the known color RGB values to their names.

near-one?

(near-one? fval)
Returns true if fvalue is within color-epsilon of one.

near-zero-or-less?

(near-zero-or-less? fval)
Returns true if the fvalue is within color-epsilon of zero or less
than zero.

near-zero?

(near-zero? fval)
Returns true if the fvalue is less than color-epsilon.

opposite

(opposite color)
Shift the hue of the color halfway around the color weel, to the
opposite color.

percent-float?

(percent-float? fval)
Return true if passed in float is in the range 0.0 - 100.0, false otherwise.

red

(red color)
Return the red (int) component of this color.

rgb-hexstr

(rgb-hexstr color)
Return the hexcode string representation of this color.

rgb-int

(rgb-int color)
Return a integer (RGB) representation of this color.

rgb-int-from-components

(rgb-int-from-components r g b)
Convert a vector of the 3 rgb integer values into a color given in
numeric rgb format.

rgb-int-to-components

(rgb-int-to-components rgb-int)
Convert a color given in numeric rgb format into a vector of the 3
rgb integer values.

rgb-int-to-unit-float

(rgb-int-to-unit-float rgb-int)
Convert the integer in range 0 - 255 to float in range 0.0 - 1.0.

rgb-int?

(rgb-int? rgb-int)
If the passed in value is an integer in the range 0 - 255
inclusive, return true, otherwise return false.

rgb-to-hsl

(rgb-to-hsl red green blue)
Given the three RGB values, convert to HSL and return vector of
Hue, Saturation, Lightness.

Based on algorithm described in:
http://en.wikipedia.org/wiki/Luminance-Hue-Saturation#Conversion_from_RGB_to_HSL_overview

rgba-hexstr

(rgba-hexstr color)
Return the hexcode string representation of this color.

rgba-int

(rgba-int color)
Return a integer (RGBA) representation of this color.

rgba-int-from-components

(rgba-int-from-components r g b a)
Convert a vector of the 4 rgba integer values into a color given in
numeric rgb format.

rgba-int-to-components

(rgba-int-to-components rgba-int)
Convert a color given in numeric rgb format into a vector of the 4
rgba integer values.

saturate

(saturate color percent)
Increase the saturation of the color by the specified amount, up to
a maximum of 100.0.

saturation

(saturation color)
Return the saturation (float) component of this color.

unit-float-to-rgba-int

(unit-float-to-rgba-int fval)
Check that the passed in float is in the range 0.0 - 1.0, then
convert it to the appropriate integer in the range 0 - 255.

unit-float?

(unit-float? fval)
Return true if passed in float is in the range 0.0 - 1.0, false otherwise.

within-tolerance?

(within-tolerance? fval1 fval2)
Check whether the two values are close enough to be considered the same.