culori

repository·main·Indexed 22 days ago

https://github.com/evercoder/culori

A general-purpose JavaScript color library supporting a wide range of color spaces, including those from the CSS Color Module Level 4 specification. It provides tools for color conversion, interpolation, blending, and calculating color differences, representing colors as plain JavaScript objects rather than classes.

Tokens
15.5K
Snippets
46
Records
101
Agent score
78%

What's inside culori

  1. Understand the difference between color models and color spaces in Culori

    main

    Culori distinguishes between color models and color spaces:

    • Color Model: A method of describing colors along specific dimensions (e.g., RGB describes color as a combination of red, green, and blue).
    • Color Space: A color model combined with a precise description of how those colors are interpreted (e.g., sRGB, Display P3, and ProPhoto RGB all use the RGB model but define different primary colors and color ranges).

    All color spaces in Culori support an optional alpha channel with a range of [0, 1].

  2. Hue and Alpha fixup methods

    main

    Fixup methods adjust channel values before interpolation to ensure better visual results for circular or special channels.

    Hue Fixup

    Used for circular hue channels.

    • fixupHueShorter(values): (Default) Interpolates along the shortest path around the hue circle.
    • fixupHueLonger(values): Interpolates along the longest path.
    • fixupHueIncreasing(values): Ensures every hue is larger than the previous.
    • fixupHueDecreasing(values): Ensures every hue is smaller than the previous.
    • To treat hues as normal numbers, override the fixup with an identity function: h: { fixup: arr => arr }.

    Alpha Fixup

    • fixupAlpha(values): (Default) Converts all undefined values to 1 (full opacity), unless all values are undefined.
  3. Understand the HSL/HSV/HSI color family

    main

    HSL, HSV, and HSI are alternative representations of the RGB model designed to be more intuitive. While they all share the same hue (h), their saturation calculations differ and are not interchangeable.

    hsl (HSL)

    ChannelRangeDescription
    h[0, 360)Hue
    s[0, 1]Saturation in HSL
    l[0, 1]Lightness
    Serialized as hsl(h s% l%).

    hsv (HSV)

    ChannelRangeDescription
    h[0, 360)Hue
    s[0, 1]Saturation in HSV
    v[0, 1]Value
    Serialized as color(--hsv h s v).

    hsi (HSI)

    ChannelRangeDescription
    h[0, 360)Hue
    s[0, 1]Saturation in HSI
    i[0, 1]Intensity
    Serialized as color(--hsi h s i).

    All three models have gamut limits. Achromatic colors (shades of gray) will have an undefined hue.

  4. Understand Culori's function-oriented API

    main
    Culori uses a function-oriented API where colors are represented as plain JavaScript objects. Instead of using class methods, you pass these color objects through various functions to perform conversions, manipulations, or calculations. This design makes the library highly extensible.
  5. Understand the Color object representation

    main

    Culori does not use a Color class. Instead, colors are represented as plain JavaScript objects. Every color object must include a mode property that identifies its color space. It may optionally include an alpha property for transparency. The specific channel names (e.g., r, g, b, h, s, l) and their expected ranges depend on the mode used.

    /* A RGB color */
    {
      mode: 'rgb',
      r: 0.1,
      g: 0.2,
      b: 1,
      alpha: 1
    }
  6. Use XYB color model

    main

    The XYB color model is part of the JPEG XL Image Coding System. It is an LMS-based model that facilitates perceptually uniform quantization.

    ChannelRangeDescription
    x[-0.0154, 0.0281]Cyan-red component
    y[0, 0.8453]Luma
    b[ -0.2778, 0.3880 ]Blue-yellow component

    XYB is defined in relationship to sRGB and has a default 'Chroma from Luma' adjustment applied so that colors with { x: 0, b: 0 } are achromatic. It does not have gamut limits.

  7. How Culori handles the alpha channel

    main

    Culori distinguishes between implicit and explicit opacity. An undefined value on the alpha channel does not necessarily mean the color is opaque; rather, it indicates that the opacity is unknown or not specified.

    For example, the hex string #ff0000 has an implicit alpha, whereas #ff0000ff has an explicit alpha. This allows developers to decide how to interpret undefined alpha values during color processing.

  8. Interpret approximate channel ranges in Culori color spaces

    main
    For certain color spaces like CIELAB or CIELCh, some channels do not have fixed, absolute ranges. In these cases, Culori provides approximate ranges (marked with ). These approximations are calculated by converting all sRGB colors defined by r, g, b ∈ ℕ ⋂ [0, 255] into that specific color space.
  9. Use Cubehelix color scheme

    main

    Cubehelix is a color scheme described by Dave Green and expanded into a cylindrical color space.

    ChannelRangeDescription
    h[0, 360)Hue (Based on start color and rotations)
    s[0, 4.614]Saturation (Called hue in Green's paper)
    l[0, 1]Lightness

    Serialized as color(--cubehelix h s l). Cubehelix does not have gamut limits.

  10. Use ICtCp (ITP) color space

    main

    ICtCp (or ITP) is a color space developed by Dolby Laboratories and included in the CSS HDR Color Module Level 1 specification.

    ChannelRangeDescription
    i[0, 0.581]Intensity
    t[-0.282, 0.278]Blue-yellow component (“tritanopia”)
    p[-0.162, 0.279]Green–red component (“protanopia”)

    Serialized as color(--ictcp i t p). ICtCp is the basis of the $\Delta E_{ITP}$ color difference metric. It does not have gamut limits.

  11. Use Jzazbz color spaces

    main

    Jzazbz is a perceptually uniform color space for high dynamic range and wide gamut signals.

    jab (Cartesian)

    ChannelRangeDescription
    j[0, 0.222]Lightness
    a[-0.109, 0.129]Green–red component
    b[-0.185, 0.134]Blue–yellow component
    Serialized as color(--jzazbz j a b).

    jch (Cylindrical)

    ChannelRangeDescription
    j[0, 0.222]Lightness
    c[0, 0.190]Chroma
    h[0, 360)Hue
    Serialized as color(--jzczhz j c h).

    Jzazbz spaces do not have gamut limits.