bobzhang/p5/color does not have a README file

    ColorMaxes

    type ColorMaxes = FixedArray[(Double, Double)]

    Ranges used by the user-facing API for each of the 4 channels (3 color channels + alpha), as (min, max) pairs.

    Color

    pub struct Color {
    // private fields
    }

    A color, stored in the space it was created in.
    impl Show for Color

    Color::alpha

    fn Color::alpha(self : Color, max? : Double) -> Double

    Alpha mapped to [0, max].

    Color::blue

    fn Color::blue(self : Color, max? : Double) -> Double

    Blue channel mapped to [0, max] (sRGB).

    Color::brightness

    fn Color::brightness(self : Color, max? : Double) -> Double

    HSB brightness in [0, max].

    Color::contrast

    fn Color::contrast(self : Color, other : Color, measure? : ContrastMethod) -> Bool

    Whether text in other on this color is readable by the given measure (p5's color.contrast(other, method)).

    Color::contrast_apca

    fn Color::contrast_apca(self : Color, other : Color) -> Double

    APCA lightness contrast (Lc) of text in other on a background of this color, as colorjs.io's contrastAPCA computes it: positive for dark text on a light background, negative for light text on a dark one.

    Color::contrast_ratio

    fn Color::contrast_ratio(self : Color, other : Color) -> Double

    WCAG 2.1 contrast ratio between two colors (1 to 21).

    Color::contrast_report

    fn Color::contrast_report(self : Color, other : Color) -> ContrastReport

    Every contrast measure between this color (background) and other.

    Color::coords

    fn Color::coords(self : Color) -> (Double, Double, Double)

    Raw coordinates in the native range of the color's space.

    Color::from_native

    fn Color::from_native(mode : ColorMode, c0 : Double, c1 : Double, c2 : Double, alpha? : Double) -> Color

    Creates a color from native coordinates of mode (no range mapping). For RGB the coordinates are in [0, 1].

    Color::green

    fn Color::green(self : Color, max? : Double) -> Double

    Green channel mapped to [0, max] (sRGB).

    Color::hue

    fn Color::hue(self : Color, max? : Double) -> Double

    Hue in [0, max]. Uses the color's own space when it has a hue channel, otherwise HSL.

    Color::lerp

    fn Color::lerp(self : Color, other : Color, amt : Double, lerp_mode? : ColorMode, output_mode? : ColorMode) -> Color

    Interpolates between two colors, like p5's lerpColor().

    Interpolation happens in lerp_mode (the space of self by default); hues take the shorter way around the color wheel. The result is expressed in output_mode (the space of self by default).

    test {
    let a = @color.Color::rgb(255, 0, 0)
    let b = @color.Color::rgb(0, 0, 255)
    inspect(a.lerp(b, 0.5).to_css(), content="#7f007f")
    }

    Color::lightness

    fn Color::lightness(self : Color, max? : Double) -> Double

    HSL lightness in [0, max].

    Color::mode

    fn Color::mode(self : Color) -> ColorMode

    The color mode (space) the color is stored in.

    Color::new

    fn Color::new(values : ArrayView[Double], mode : ColorMode, maxes : FixedArray[(Double, Double)], clamp? : Bool) -> Color

    Creates a color from user values the way p5's color(...) does.

    values holds 1 (gray), 2 (gray, alpha), 3 (channels) or 4 (channels, alpha) numbers in the ranges given by maxes. Values are clamped to the ranges unless clamp=false.

    Panics

    Panics if values is empty or holds more than 4 numbers.

    Color::parse

    fn Color::parse(input : StringView) -> Color?

    Parses a CSS color string: hex (#rgb, #rgba, #rrggbb, #rrggbbaa), rgb()/rgba(), hsl()/hsla(), hwb(), named colors and transparent. Returns None for unsupported input.

    test {
    inspect(@color.Color::parse("tomato").unwrap().to_css(), content="#ff6347")
    inspect(@color.Color::parse("#0f08").unwrap().to_css(), content="#00ff0088")
    inspect(
    @color.Color::parse("rgb(255 0 0 / 50%)").unwrap().to_css(),
    content="#ff00007f",
    )
    inspect(
    @color.Color::parse("hsl(120, 100%, 50%)").unwrap().mode(),
    content="hsl",
    )
    }

    Color::red

    fn Color::red(self : Color, max? : Double) -> Double

    Red channel mapped to [0, max] (sRGB).

    Color::rgb

    fn Color::rgb(r : Double, g : Double, b : Double, alpha? : Double) -> Color

    Creates an sRGB color from components in [0, 255].

    test {
    inspect(@color.Color::rgb(255, 128, 0).to_css(), content="#ff8000")
    inspect(@color.Color::rgb(255, 0, 0, alpha=128).to_css(), content="#ff000080")
    }

    Color::rgba

    fn Color::rgba(self : Color, maxes? : FixedArray[(Double, Double)]) -> (Double, Double, Double, Double)

    sRGB components and alpha mapped to maxes (like p5's _getRGBA).

    Color::same_as

    fn Color::same_as(self : Color, other : Color) -> Bool

    Returns true when two colors have the same sRGB components and alpha (within 1e-9).

    Color::saturation

    fn Color::saturation(self : Color, max? : Double) -> Double

    Saturation in [0, max] (HSB saturation for HSB colors, HSL otherwise).

    Color::set_alpha

    fn Color::set_alpha(self : Color, v : Double, max? : Double) -> Unit

    Sets the alpha from a value in [0, max].

    Color::set_blue

    fn Color::set_blue(self : Color, v : Double, max? : Double) -> Unit

    Sets the sRGB blue channel from a value in [0, max].

    Color::set_green

    fn Color::set_green(self : Color, v : Double, max? : Double) -> Unit

    Sets the sRGB green channel from a value in [0, max].

    Color::set_red

    fn Color::set_red(self : Color, v : Double, max? : Double) -> Unit

    Sets the sRGB red channel from a value in [0, max].

    Color::to_css

    fn Color::to_css(self : Color) -> String

    A CSS color string for the color, as used for fillStyle.

    RGB colors created from channel values serialize as #rrggbb or #rrggbbaa like p5.js; other colors serialize as CSS rgb() in percent.

    Color::to_mode

    fn Color::to_mode(self : Color, mode : ColorMode) -> Color

    Converts the color to another space.

    Color::to_srgb

    fn Color::to_srgb(self : Color) -> (Double, Double, Double, Double)

    Gamma-encoded sRGB components and alpha, all in [0, 1] (not clamped for out-of-gamut colors).

    ColorMode

    pub(all) enum ColorMode {
    RGB
    HSB
    HSL
    HWB
    LAB
    LCH
    OKLAB
    OKLCH
    RGBP3
    } derive(Eq, Hash,
    Debug
    )

    The color spaces understood by color_mode().
    impl Show for ColorMode

    ContrastMethod

    pub(all) enum ContrastMethod {
    Wcag21
    Apca
    } derive(Eq,
    Debug
    )

    A contrast measure for Color::contrast.

    ContrastReport

    pub(all) struct ContrastReport {
    wcag21 : Double
    wcag21_passed_minimum : Bool
    wcag21_passed_aaa : Bool
    apca : Double
    apca_passed_minimum : Bool
    } derive(
    Debug
    )

    Both contrast measures and whether they pass (p5's color.contrast(other, 'all')).

    default_maxes

    fn default_maxes(mode : ColorMode) -> FixedArray[(Double, Double)]

    p5.js default maxes for a color mode, e.g. [255, 255, 255, 255] for RGB and [360, 100, 100, 1] for HSB.

    maxes

    fn maxes(c0 : Double, c1 : Double, c2 : Double, alpha : Double) -> FixedArray[(Double, Double)]

    Builds maxes from plain upper bounds (lower bounds are 0).

    palette_lerp

    fn palette_lerp(stops : ArrayView[(Color, Double)], amt : Double) -> Color

    Picks a color along a list of (color, stop) pairs, like p5's paletteLerp(). Stops must be increasing.

    Panics

    Panics if stops is empty.