Color
- Package
- purescript-colors
- Repository
- sharkdp/purescript-colors
This module provides basic types and functions for dealing with colors.
Colors can be constructed from HSL values, RGB values or Hex strings /
integers. In addition, a lot of standardized named colors can be found in
Color.Scheme.X11
.
This module also provides functions to modify colors (e.g. lighten/darken, saturate/desaturate, complementary), to combine colors (mix) and to analyze colors (e.g. brightness, luminance, contrast).
Implementation detail: Colors are represented by their HSL values (hue, saturation, lightness) internally, as this provides more flexibility than storing RGB values. In particular, note that only colors within the sRGB gamut can be represented.
#Color Source
data Color
The representation of a color.
Note:
- The
Eq
instance compares twoColor
s by comparing their (integer) RGB values. This is different from comparing the HSL values (for example, HSL has many different representations of black (arbitrary hue and saturation values). - Colors outside the sRGB gamut which cannot be displayed on a typical
computer screen can not be represented by
Color
.
Instances
#ColorSpace Source
data ColorSpace
Definition of a color space.
RGB
: red, green, blueHSL
: hue, saturation, lightnessLCh
: Lightness, chroma, hueLab
: Lightness, a, b
Constructors
#xyz Source
xyz :: Number -> Number -> Number -> Color
Create a Color
from XYZ coordinates in the CIE 1931 color space. Note
that a Color
always represents a color in the sRGB gamut (colors that
can be represented on a typical computer screen) while the XYZ color space
is bigger. This function will tend to create fully saturated colors at the
edge of the sRGB gamut if the coordinates lie outside the sRGB range.
See:
#lch Source
#fromHexString Source
fromHexString :: String -> Maybe Color
Parse a hexadecimal RGB code of the form #rgb
or #rrggbb
. The #
character is required. Each hexadecimal digit is of the form [0-9a-fA-F]
(case insensitive). Returns Nothing
if the string is in a wrong format.
#toXYZ Source
#toHexString Source
toHexString :: Color -> String
Return a hexadecimal representation of the color in the form #rrggbb
,
where rr
, gg
and bb
refer to hexadecimal digits corresponding to
the RGB channel values between 00
and ff
. The alpha channel is not
represented.
#cssStringHSLA Source
cssStringHSLA :: Color -> String
A CSS representation of the color in the form hsl(..)
or hsla(...)
.
#cssStringRGBA Source
cssStringRGBA :: Color -> String
A CSS representation of the color in the form rgb(..)
or rgba(...)
.
#complementary Source
complementary :: Color -> Color
Get the complementary color (hue rotated by 180°).
#desaturate Source
desaturate :: Number -> Color -> Color
Decrease the saturation of a color by subtracting a certain amount (number between -1.0 and 1.0) from the saturation channel. If the number is negative, the color is saturated.
#Interpolator Source
type Interpolator = Color -> Color -> Number -> Color
A function that interpolates between two colors. It takes a start color, an end color, and a ratio in the interval [0.0, 1.0]. It returns the mixed color.
#mix Source
mix :: ColorSpace -> Interpolator
Mix two colors by linearly interpolating between them in the specified color space. For the HSL colorspace, the shortest path is chosen along the circle of hue values.
#mixCubehelix Source
mixCubehelix :: Number -> Interpolator
Mix two colors via Dave Green's cubehelix by
interpolating between them. Takes a gamma correction value as an argument and
returns an Interpolator
function.
For more details see:
Ported from:
#brightness Source
brightness :: Color -> Number
The percieved brightness of the color (A number between 0.0 and 1.0).
#luminance Source
luminance :: Color -> Number
The relative brightness of a color (normalized to 0.0 for darkest black and 1.0 for lightest white), according to the WCAG definition.
See: https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
#contrast Source
contrast :: Color -> Color -> Number
The contrast ratio of two colors. A minimum contrast ratio of 4.5 is
recommended to ensure that text is readable on a colored background. The
contrast ratio is symmetric on both arguments:
contrast c1 c2 == contrast c2 c1
.
See http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
#isReadable Source
isReadable :: Color -> Color -> Boolean
Determine whether text of one color is readable on a background of a
different color (see contrast
). This function is symmetric in both
arguments.
isReadable c1 c2 = contrast c1 c2 > 4.5