Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
color / color-parseOpenRuns on Liberas

Parse a CSS color

A CSS Color 4 <color> as declared: {space, c1, c2, c3, alpha}. text is at most 256 bytes, ASCII case-insensitive; leading and trailing whitespace (space, tab, LF, CR, FF) is ignored. No calc(), var(), comments, relative or system colors, color-mix() or currentcolor. The 148 named colors of CSS Color 4 §6.1 and #rgb, #rgba, #rrggbb, #rrggbbaa (a short digit doubled; alpha = n ÷ 255) give space rgb, channels 0–255 as integers; transparent is rgb 0 0 0 / 0. Functions, the name then ( at once: rgb/rgba, hsl/hsla, hwb, lab, lch, oklab, oklch with three components and an optional / alpha, separated by whitespace, each a number, percentage or none; or rgb/rgba, hsl/hsla in the legacy form: 3 or 4 comma-separated values, no none, rgb channels all numbers or all percentages, hsl s and l percentages. color(sp c1 c2 c3 [/ alpha]) with sp srgb, srgb-linear, display-p3, a98-rgb, prophoto-rgb, rec2020, xyz (= xyz-d65), xyz-d50 or xyz-d65. A number is [+-]?(D(.D)?|.D)([eE][+-]?D)?, D digits, the binary64 nearest its decimal; a percentage p of reference range R is p if R = 100, p ÷ 100 if R = 1, else p × R ÷ 100. R: rgb 255; hsl s, l and hwb w, b 100; lab L 100, a, b 125; lch L 100, C 150; oklab L 1, a, b 0.4; oklch L 1, C 0.4; color() 1; alpha 1. A hue (hsl, hwb first; lch, oklch third) is a number or an angle (deg; grad × 360 ÷ 400; rad × 180 ÷ π; turn × 360), then h fmod 360, + 360 if negative, and 360 becomes 0. Clamps: rgb channels [0, 255]; hsl s, l and hwb w, b [0, 100]; lab, lch L [0, 100]; oklab, oklch L [0, 1]; lch, oklch C ≥ 0; alpha [0, 1]. Omitted alpha is 1; none is absent; −0 becomes +0. Anything else, or a non-finite value, is an error.

From your terminal

terminal
npm i -g https://liberas.dev/cli/liberas.tgz
export LIBERAS_URL=https://liberas.dev
liberas init color-parse --lang rust
cd color-parse && liberas build && liberas submit

Save a token once with liberas login (make it on your page, /me, after signing in). init scaffolds the interface, the vectors and a stub in Rust (--lang js for light specs).

Interface (WIT)42 lines›
color-parse.wit
package root:component;

world root {
  import liberas:color/types@0.1.0;

  export liberas:color/parse@0.1.0;
}
package liberas:color@0.1.0 {
  interface types {
    enum space {
      rgb,
      hsl,
      hwb,
      lab,
      lch,
      oklab,
      oklch,
      srgb,
      srgb-linear,
      display-p3,
      a98-rgb,
      prophoto-rgb,
      rec2020,
      xyz-d50,
      xyz-d65,
    }

    record color {
      space: space,
      c1: option<f64>,
      c2: option<f64>,
      c3: option<f64>,
      alpha: option<f64>,
    }
  }
  interface parse {
    use types.{color};

    parse: func(text: string) -> result<color, string>;
  }
}
Test vectors81›
color-parse.cases.json
[
  {
    "args": {
      "text": "red"
    }
  },
  {
    "args": {
      "text": "RebeccaPurple"
    }
  },
  {
    "args": {
      "text": "  Lime\t"
    }
  },
  {
    "args": {
      "text": "transparent"
    }
  },
  {
    "args": {
      "text": "#f00"
    }
  },
  {
    "args": {
      "text": "#FfF8"
    }
  },
  {
    "args": {
      "text": "#663399"
    }
  },
  {
    "args": {
      "text": "#ff000080"
    }
  },
  {
    "args": {
      "text": "rgb(255 0 0)"
    }
  },
  {
    "args": {
      "text": "rgb(255, 0, 0)"
    }
  },
  {
    "args": {
      "text": "rgba(255,0,0,0.5)"
    }
  },
  {
    "args": {
      "text": "RGB( 10.5 , 20 , 30 , 50% )"
    }
  },
  {
    "args": {
      "text": "rgb(100% 50% 0% / 25%)"
    }
  },
  {
    "args": {
      "text": "rgb(33% 66.6% 1e2%)"
    }
  },
  {
    "args": {
      "text": "rgb(300 -20 none / none)"
    }
  },
  {
    "args": {
      "text": "rgba(12 34 56)"
    }
  },
  {
    "args": {
      "text": "rgb(1 2 3/.5)"
    }
  },
  {
    "args": {
      "text": "rgb(1 2 3 / 2)"
    }
  },
  {
    "args": {
      "text": "hsl(120deg 100% 50%)"
    }
  },
  {
    "args": {
      "text": "hsl(120, 100%, 50%)"
    }
  },
  {
    "args": {
      "text": "hsla(-90, 50%, 150%, -1)"
    }
  },
  {
    "args": {
      "text": "hsl(0.5turn 50 50 / .5)"
    }
  },
  {
    "args": {
      "text": "hsl(none 20% none)"
    }
  },
  {
    "args": {
      "text": "hsl(400grad 10% 20%)"
    }
  },
  {
    "args": {
      "text": "hsl(-0 0% 0%)"
    }
  },
  {
    "args": {
      "text": "hwb(200grad 20% 30%)"
    }
  },
  {
    "args": {
      "text": "hwb(1.5rad 120% -5)"
    }
  },
  {
    "args": {
      "text": "lab(50% 40 -59.5)"
    }
  },
  {
    "args": {
      "text": "lab(150 100% -100%)"
    }
  },
  {
    "args": {
      "text": "lch(52.2345% 72.2 56.2 / 0.8)"
    }
  },
  {
    "args": {
      "text": "lch(50 -10 -45deg)"
    }
  },
  {
    "args": {
      "text": "oklab(0.6 0.1 -0.1)"
    }
  },
  {
    "args": {
      "text": "oklab(60% 25% -25%)"
    }
  },
  {
    "args": {
      "text": "oklab(none none none / none)"
    }
  },
  {
    "args": {
      "text": "oklch(70% 0.15 3.14159rad)"
    }
  },
  {
    "args": {
      "text": "oklch(1.2 50% 725)"
    }
  },
  {
    "args": {
      "text": "color(display-p3 1 0 0)"
    }
  },
  {
    "args": {
      "text": "color(srgb 0.5 50% none)"
    }
  },
  {
    "args": {
      "text": "color(srgb-linear 1.5 -0.25 0)"
    }
  },
  {
    "args": {
      "text": "color(a98-rgb 0.1 0.2 0.3 / 0.4)"
    }
  },
  {
    "args": {
      "text": "color(prophoto-rgb 0.3 0.6 0.9)"
    }
  },
  {
    "args": {
      "text": "color(rec2020 0.25 0.5 0.75)"
    }
  },
  {
    "args": {
      "text": "color(xyz 0.2 0.3 0.4 / 50%)"
    }
  },
  {
    "args": {
      "text": "color(XYZ-D50 0.9642 1 0.8251)"
    }
  },
  {
    "args": {
      "text": "color(xyz-d65 +.5e1 -1E-1 0)"
    }
  },
  {
    "args": {
      "text": "rgb(0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 2 3)"
    }
  },
  {
    "args": {
      "text": ""
    }
  },
  {
    "args": {
      "text": "currentcolor"
    }
  },
  {
    "args": {
      "text": "notacolor"
    }
  },
  {
    "args": {
      "text": "red blue"
    }
  },
  {
    "args": {
      "text": "#ff"
    }
  },
  {
    "args": {
      "text": "#fffff"
    }
  },
  {
    "args": {
      "text": "#ggg"
    }
  },
  {
    "args": {
      "text": "rgb(1 2)"
    }
  },
  {
    "args": {
      "text": "rgb(1 2 3 4)"
    }
  },
  {
    "args": {
      "text": "rgb(1, 2 3)"
    }
  },
  {
    "args": {
      "text": "rgb(1,2,3,)"
    }
  },
  {
    "args": {
      "text": "rgb(10%, 20, 30)"
    }
  },
  {
    "args": {
      "text": "rgb(1, 2, none)"
    }
  },
  {
    "args": {
      "text": "rgb(1 2 3 / 0.5 / 1)"
    }
  },
  {
    "args": {
      "text": "rgb(1 2 3 /)"
    }
  },
  {
    "args": {
      "text": "rgb (1 2 3)"
    }
  },
  {
    "args": {
      "text": "rgb(1 2 3"
    }
  },
  {
    "args": {
      "text": "rgb(1 2 3) x"
    }
  },
  {
    "args": {
      "text": "rgb(1e400 0 0)"
    }
  },
  {
    "args": {
      "text": "rgb(1. 2 3)"
    }
  },
  {
    "args": {
      "text": "rgb(1px 2 3)"
    }
  },
  {
    "args": {
      "text": "hsl(120, 50, 50)"
    }
  },
  {
    "args": {
      "text": "hsl(120% 50% 50%)"
    }
  },
  {
    "args": {
      "text": "hsl(120foo 50% 50%)"
    }
  },
  {
    "args": {
      "text": "hwb(1, 2%, 3%)"
    }
  },
  {
    "args": {
      "text": "lab(50 40)"
    }
  },
  {
    "args": {
      "text": "lab(50 40 30deg)"
    }
  },
  {
    "args": {
      "text": "oklch(0.5 0.1 10%)"
    }
  },
  {
    "args": {
      "text": "color(foo 1 2 3)"
    }
  },
  {
    "args": {
      "text": "color(srgb 1 2)"
    }
  },
  {
    "args": {
      "text": "color(1 2 3)"
    }
  },
  {
    "args": {
      "text": "calc(1)"
    }
  },
  {
    "args": {
      "text": "rgb(calc(1) 2 3)"
    }
  },
  {
    "args": {
      "text": "réd"
    }
  },
  {
    "args": {
      "text": "rgb(00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 2 3)"
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 81 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
  • Components may import nothing but specs: no files, no network, no clock.
  • Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
  • A remote call may execute up to 1,000,000 instructions.
  • Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.