Documentation
    Preparing search index...

    Function parseColor

    • Parses a CSS colour string into sRGB-encoded components in 0…1 (§60a).

      parseColor("#a0a0a0");                 // [0.627…, 0.627…, 0.627…, 1]
      parseColor("rgb(255, 128, 0)"); // [1, 0.501…, 0, 1]
      parseColor("rgba(255 128 0 / 50%)"); // [1, 0.501…, 0, 0.5]
      parseColor("teal"); // [0, 0.501…, 0.501…, 1]

      §60a: "CSS-style color strings used throughout the API (§50, §59, §68) denote sRGB values", and the GPU pipeline is linear-light. The result of this function is therefore not ready to hand to a material or a light — run it through srgbToLinearRGBA first. The two steps are separate so that a loader reading #rrggbb out of a document, a test asserting the byte value, and a debug overlay printing what the author wrote all get the encoded value they are actually talking about.

      #rgb, #rgba, #rrggbb, #rrggbbaa (case-insensitive); rgb(…) and rgba(…) with three components and an optional alpha, comma-separated or space-separated with / before the alpha, each component a number 0…255 or a percentage and the alpha a number 0…1 or a percentage; transparent; and the sixteen CSS Level 1 keywords (black, silver, gray, white, maroon, red, purple, fuchsia, green, lime, olive, yellow, navy, blue, teal, aqua). Leading and trailing whitespace is ignored, and keywords are case-insensitive.

      Everything else — hsl(), lab(), color(), currentColor, the other ~132 keywords — throws a TypeError naming what is accepted (§85). A full CSS colour parser is not this tier: refusing loudly is what keeps an unsupported notation from silently becoming black.

      Components are not clamped: rgb(300 0 0) parses to [1.176…, 0, 0, 1], because §60a's pipeline is extended-range and this function's job is to report what the string said (module header).

      Parameters

      • css: string

        The colour string.

      • Optionalout: ColorRGBA

        Optional destination, returned when given. Parsing is setup-time work, so this is optional where the transfer functions' out-parameters are required; it exists for a loader filling an array it already owns.

      Returns ColorRGBA