nicheware.platform.utilities.common.graphics.color
Functions for dealing with colors and conversion between different color representations.
There are groups of functions and variables within the color namespace that deal with:
Function group | Functions |
---|---|
constants for argb packed int colors | blue-color, black-color, light-grey-color, dark-red-color, transparent-blue-color, dark-blue-color, white-color, dark-green-color, green-color, yellow-color |
constants for css string hex colors | bright-red-color, css-yellow-color, css-blue-color, css-pink-color, css-white-color |
rgb utility functions | normalise-rgb, int-rgb, ratio-rgba-to-int-rgba, nudge-color, unique-color |
rgb format conversion | color-as-rgba, color-as-map, rgba-as-css-hex, color-as-css-hex, react-color-as-rgba |
rgba, hsla color model conversion | rgba-to-hsla, hsla-to-rgba |
color model keyword functions | model-from-to, model-ranges |
color gradient functions | make-color-increment-fn, color-difference-seq |
The different color representations that are used by functions include:
Representation | Description |
---|---|
RGBA | an RGB color with an alpha channel, represented as a vector with 4 values [R G B A] . Each value is an int from 0 to 255 |
RGB or packed RGB | an RGB color with an alpha channel, packed as a Clojure integer, with each byte representing A, R, G, B. The unpacked values are from 0 to 255 |
RGB CSS | String hex representation of an RGB color with no alpha channel, as used in CSS color specifications. |
HSLA | Hue, Saturation, Lightness, Alpha as a clojure vector [H S L A] . Hue is a degree on the color wheel, 0 to 360. Saturation is a percentage (100 full color), Lightness is percentage of reflecting color (0 is dark, 100 is light), Alpha is 0 to 255. |
color-as-css-hex
(color-as-css-hex color-value)
Converts a color array of [R G B A]
all from 0 to 255 or a packed int ARGB, into a CSS hex, which will ignore the alpha.
- color-value: RGB color value as either
[R G B]
or[R G B A]
, or a packed int of form<alpha><R><G><B>
. - returns: CSS hex RGB string
#FFFFFF
color-as-map
(color-as-map color-value)
Converts color values to map of values from 0 to 255 for RGB and 0 to 1 for Alpha to {:r <red> :g <green> :b <blue> :a <alpha> }
Accepts a packed int or existing vector of [R G B]
or [R G B A]
. Assumes alpha 255 on input.
Output suitable for use with react-color pickers.
- color-value: RGB color value as either
[R G B]
or[R G B A]
, or a packed int of form<alpha><R><G><B>
. - returns: color map of the form
{:r <red> :g <green> :b <blue> :a <alpha> }
color-as-rgba
(color-as-rgba color-value)
Converts color values to vector of values from 0 to 255 for [R G B A]
. Accepts a packed int or existing vector of [R G B]
or [R G B A]
.
- color-value: Either a vector of
[R G B]
or[R G B A]
, or a packed int of form<alpha><R><G><B>
- returns: vector of form
[R G B A]
. If no alpha provided on input, 255 (opaque) is used.
color-difference-seq
(color-difference-seq start-color end-color count)
Create a color sequence that follows on from the end color, using the start/end difference. Input colors in form of either Int32 or vector of RGBA
- start-color: Color value as starting point for difference. either
[R G B]
or[R G B A]
, or a packed int of form<alpha><R><G><B>
. - end-color: Color value as starting point for difference. either
[R G B]
or[R G B A]
, or a packed int of form<alpha><R><G><B>
. - count: Number of colors to generate using end-color as initial input and each resulting color as next input.
- returns: vector of count colors, being a sequence after end-color, using the color difference between start-color and end-color to generate colors. All generated colors are in RGBA format
[R G B A]
.
hsla-to-rgba
(hsla-to-rgba [hue saturation lightness alpha :as hlsa])
Converts an HSLA vector [H S L A]
where H 0 to 360, S and L percentages to RGBA vector [R G B A]
all 255 and A remains unchanged (0 to 255)
- hlsa: HSLA version of rgba input.
[H S L A]
- returns: vector of RGBA color.
[R G B A]
int-rgb
(int-rgb normalised-value)
Converts a normalized value from 0 to 1 to an RGB int from 0 to 255
- value: float RGB value from 0 to 1.0
- returns: int RGB value from 0 to 255
make-color-increment-fn
(make-color-increment-fn start-color end-color)
Uses the difference between the two colors to create a color increment function.
- start-color: Color value as starting point for difference. either
[R G B]
or[R G B A]
, or a packed int of form<alpha><R><G><B>
. - end-color: Color value as starting point for difference. either
[R G B]
or[R G B A]
, or a packed int of form<alpha><R><G><B>
. - returns: function taking a single color argument, and modifying it by the difference between start-color and end-color. Argument can be either
[R G B]
or[R G B A]
, or a packed int of form<alpha><R><G><B>
. Result will be[R G B A]
model-from-to
(model-from-to from-model to-model color)
Keywords used to define different color model types. Allows conversion form one model to another. Only currently supports :rgba
and :hsla
- from-model: color model used by the input color value. (either
:rgba
or:hsla
) - to-model: color model to convert input color to. (either
:rgba
or:hsla
) - color: Color to be converted. Will be represented according to from-model. So either
[R G B A]
or[H S L A]
- returns: input color converted to the to-model. So either
[R G B A]
or[H S L A]
model-ranges
(model-ranges color-model)
Returns a vector defining the ranges of the values in the color model. eg :rgba
would give [255 255 255 255]
:hsla
would give [359 1 1 255]
- color-model: Color model for which ranges required. (either
:rgba
or:hsla
) - returns: vector of ranges for each dimension, appropriate to color model. (either
[255 255 255 255]
or[359 1 1 255]
) Throws environment specific illegal argument exception for invalid color model.
normalise-rgb
(normalise-rgb value)
Converts a 0 to 255 RGB value to 0 to 1.0
- value: int RGB value from 0 to 255
- returns: float RGB value from 0 to 1.0
nudge-color
(nudge-color [red green blue alpha :as color])
Modifies the color in a small way to make a slightly different color. Currently does it by moving blue up or down by 1.
- color: vector of RGBA color to be nudged.
[R G B A]
- returns: slightly modified color in blue component. Either increment or decrement.
ratio-rgba-to-int-rgba
(ratio-rgba-to-int-rgba rgba-ratios)
Useful when RGB int values have been computed. Will convert any ratios to floats, round and then express as int
- rgba-ratios: Collection of RGB values from 0 to 255. May be expressed as Clojure ratios due to calculations.
- returns: vector of RGB values from 0 to 255 all as ints (may be rounded from original ratio value)
react-color-as-rgba
(react-color-as-rgba {rgb "rgb", :as react-color})
Converts a react-col color value map. This has a number of forms within, but one is { 'rgb' {'r' 255 'g' 255 'b' 255 'a' 1}}
. We convert this to [R G B A]
, all values 0 to 255
- react-color: color as
{ 'rgb' {'r' <red:0-255> 'g' <green:0-255> 'b' <blue:0-255> 'a' <alpha:0-1>}}
- returns: color as RGB vector
[R G B A]
, all 0 to 255.
rgba-as-css-hex
(rgba-as-css-hex [red green blue alpha :as color-value])
Transform a color vector of [R G B A]
into a CSS hex string such as #FFFFFF
, ignoring the alpha.
- color-value: vector of 0 to 255 values.
[R G B A]
- returns: CSS hex RGB string
#FFFFFF
rgba-to-hsla
(rgba-to-hsla [_ _ _ alpha :as rgba])
Converts an RGBA vector [R G B A]
all 0 to 255, to HSLA vector [H S L A]
where H 0 to 360, and S and L are percentages and A remains unchanged (0 to 255)
- rgba: vector of RGBA color.
[R G B A]
- returns: HSLA version of rgba input.
[H S L A]
unique-color
(unique-color col unique-set)
Keep nudging the given color until it is unique given the existing set of unique colors.
- col: vector of RGBA color to be nudged.
[R G B A]
- unique-set: Set of RGBA colors, that we do not want to overlap with.
- returns: slightly modified color in blue component. Either increment or decrement. Will be different from all other colors in unique-set.