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

CSS color-mix()

CSS Color 5 color-mix(in space hue, a p1, b p2) of 8-bit sRGB colors with alpha (α = alpha ÷ 255), as 8-bit sRGB with alpha. A given percentage outside 0–100 (or NaN) is an error; both omitted are 50 and 50; one omitted is 100 minus the other; S = p1 + p2, and S = 0 is an error; t = p2 ÷ S; m = S ÷ 100 if S < 100, else 1. Each color goes into the space from c = channel ÷ 255 by CSS Color 4's sample code: srgb c; srgb-linear lin_sRGB(c); xyz-d65 lin_sRGB_to_XYZ of that; xyz-d50 then D65_to_D50; lab then XYZ_to_Lab; oklab XYZ_to_OKLab of xyz-d65; lch and oklch from lab and oklab with C = √(a × a + b × b), H = atan2(b, a) × 180 ÷ π, + 360 if negative; hsl rgbToHsl and hwb rgbToHwb (hue 60 × ((g − b) ÷ d + (6 if g < b)), 60 × ((b − r) ÷ d + 2) or 60 × ((r − g) ÷ d + 4) by the first maximum of r, g, b; s, l, w, b × 100). When r = g = b the hue is missing; a missing hue takes the other color's. Hues h1, h2: shorter: if h2 − h1 > 180, h1 += 360, else if h2 − h1 < −180, h2 += 360; longer: if 0 < h2 − h1 < 180, h1 += 360, else if −180 < h2 − h1 ≤ 0, h2 += 360; increasing: if h2 < h1, h2 += 360; decreasing: if h1 < h2, h1 += 360. Then α = α1 × (1 − t) + α2 × t; hue = h1 × (1 − t) + h2 × t; each other component (v1 × α1) × (1 − t) + (v2 × α2) × t, ÷ α unless α = 0. That color with alpha α × m (a hue missing in both is 0) converts to 8-bit as color-to-srgb does. round is half away from zero. pow, cbrt, sin, cos, atan2 and exp are the platform's own: results are integers, so any libm accurate to a few ulps agrees except within a hair of a rounding boundary.

From your terminal

terminal
npm i -g https://liberas.dev/cli/liberas.tgz
export LIBERAS_URL=https://liberas.dev
liberas init color-mix --lang rust
cd color-mix && 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)43 lines›
color-mix.wit
package root:component;

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

  export liberas:color/mix@0.1.0;
}
package liberas:color@0.1.0 {
  interface types {
    record rgba8 {
      r: u8,
      g: u8,
      b: u8,
      alpha: u8,
    }

    enum mix-space {
      srgb,
      srgb-linear,
      lab,
      oklab,
      xyz-d50,
      xyz-d65,
      hsl,
      hwb,
      lch,
      oklch,
    }

    enum hue-method {
      shorter,
      longer,
      increasing,
      decreasing,
    }
  }
  interface mix {
    use types.{rgba8, mix-space, hue-method};

    mix: func(space: mix-space, hue: hue-method, a: rgba8, p1: option<f64>, b: rgba8, p2: option<f64>) -> result<rgba8, string>;
  }
}
Test vectors20›
color-mix.cases.json
[
  {
    "args": {
      "a": {
        "b": 0,
        "g": 0,
        "r": 255,
        "alpha": 255
      },
      "b": {
        "b": 255,
        "g": 0,
        "r": 0,
        "alpha": 255
      },
      "p1": null,
      "p2": null,
      "hue": "shorter",
      "space": "srgb"
    }
  },
  {
    "args": {
      "a": {
        "b": 0,
        "g": 0,
        "r": 255,
        "alpha": 255
      },
      "b": {
        "b": 255,
        "g": 0,
        "r": 0,
        "alpha": 255
      },
      "p1": null,
      "p2": null,
      "hue": "shorter",
      "space": "oklab"
    }
  },
  {
    "args": {
      "a": {
        "b": 0,
        "g": 0,
        "r": 255,
        "alpha": 255
      },
      "b": {
        "b": 255,
        "g": 0,
        "r": 0,
        "alpha": 255
      },
      "p1": null,
      "p2": null,
      "hue": "shorter",
      "space": "oklch"
    }
  },
  {
    "args": {
      "a": {
        "b": 0,
        "g": 0,
        "r": 255,
        "alpha": 255
      },
      "b": {
        "b": 255,
        "g": 0,
        "r": 0,
        "alpha": 255
      },
      "p1": null,
      "p2": null,
      "hue": "longer",
      "space": "oklch"
    }
  },
  {
    "args": {
      "a": {
        "b": 255,
        "g": 0,
        "r": 0,
        "alpha": 255
      },
      "b": {
        "b": 0,
        "g": 0,
        "r": 255,
        "alpha": 255
      },
      "p1": 30,
      "p2": null,
      "hue": "increasing",
      "space": "oklch"
    }
  },
  {
    "args": {
      "a": {
        "b": 255,
        "g": 0,
        "r": 0,
        "alpha": 255
      },
      "b": {
        "b": 0,
        "g": 0,
        "r": 255,
        "alpha": 255
      },
      "p1": 30,
      "p2": null,
      "hue": "decreasing",
      "space": "oklch"
    }
  },
  {
    "args": {
      "a": {
        "b": 153,
        "g": 51,
        "r": 102,
        "alpha": 255
      },
      "b": {
        "b": 255,
        "g": 255,
        "r": 255,
        "alpha": 255
      },
      "p1": 25,
      "p2": 25,
      "hue": "shorter",
      "space": "lch"
    }
  },
  {
    "args": {
      "a": {
        "b": 114,
        "g": 128,
        "r": 250,
        "alpha": 255
      },
      "b": {
        "b": 180,
        "g": 130,
        "r": 70,
        "alpha": 128
      },
      "p1": 80,
      "p2": 80,
      "hue": "shorter",
      "space": "lab"
    }
  },
  {
    "args": {
      "a": {
        "b": 0,
        "g": 0,
        "r": 255,
        "alpha": 255
      },
      "b": {
        "b": 255,
        "g": 255,
        "r": 0,
        "alpha": 255
      },
      "p1": null,
      "p2": null,
      "hue": "shorter",
      "space": "hsl"
    }
  },
  {
    "args": {
      "a": {
        "b": 0,
        "g": 255,
        "r": 255,
        "alpha": 255
      },
      "b": {
        "b": 255,
        "g": 0,
        "r": 0,
        "alpha": 255
      },
      "p1": 50,
      "p2": null,
      "hue": "longer",
      "space": "hsl"
    }
  },
  {
    "args": {
      "a": {
        "b": 0,
        "g": 0,
        "r": 255,
        "alpha": 255
      },
      "b": {
        "b": 128,
        "g": 128,
        "r": 128,
        "alpha": 255
      },
      "p1": null,
      "p2": null,
      "hue": "shorter",
      "space": "hwb"
    }
  },
  {
    "args": {
      "a": {
        "b": 255,
        "g": 255,
        "r": 255,
        "alpha": 255
      },
      "b": {
        "b": 0,
        "g": 0,
        "r": 0,
        "alpha": 255
      },
      "p1": null,
      "p2": null,
      "hue": "shorter",
      "space": "oklch"
    }
  },
  {
    "args": {
      "a": {
        "b": 0,
        "g": 0,
        "r": 255,
        "alpha": 0
      },
      "b": {
        "b": 0,
        "g": 255,
        "r": 0,
        "alpha": 255
      },
      "p1": null,
      "p2": 25,
      "hue": "shorter",
      "space": "srgb-linear"
    }
  },
  {
    "args": {
      "a": {
        "b": 99,
        "g": 200,
        "r": 12,
        "alpha": 51
      },
      "b": {
        "b": 200,
        "g": 12,
        "r": 200,
        "alpha": 204
      },
      "p1": 10,
      "p2": null,
      "hue": "shorter",
      "space": "xyz-d65"
    }
  },
  {
    "args": {
      "a": {
        "b": 0,
        "g": 0,
        "r": 0,
        "alpha": 0
      },
      "b": {
        "b": 255,
        "g": 255,
        "r": 255,
        "alpha": 0
      },
      "p1": null,
      "p2": null,
      "hue": "shorter",
      "space": "xyz-d50"
    }
  },
  {
    "args": {
      "a": {
        "b": 3,
        "g": 2,
        "r": 1,
        "alpha": 255
      },
      "b": {
        "b": 6,
        "g": 5,
        "r": 4,
        "alpha": 255
      },
      "p1": 0,
      "p2": 100,
      "hue": "shorter",
      "space": "srgb"
    }
  },
  {
    "args": {
      "a": {
        "b": 3,
        "g": 2,
        "r": 1,
        "alpha": 255
      },
      "b": {
        "b": 6,
        "g": 5,
        "r": 4,
        "alpha": 255
      },
      "p1": 0,
      "p2": 0,
      "hue": "shorter",
      "space": "srgb"
    }
  },
  {
    "args": {
      "a": {
        "b": 3,
        "g": 2,
        "r": 1,
        "alpha": 255
      },
      "b": {
        "b": 6,
        "g": 5,
        "r": 4,
        "alpha": 255
      },
      "p1": 101,
      "p2": null,
      "hue": "shorter",
      "space": "srgb"
    }
  },
  {
    "args": {
      "a": {
        "b": 3,
        "g": 2,
        "r": 1,
        "alpha": 255
      },
      "b": {
        "b": 6,
        "g": 5,
        "r": 4,
        "alpha": 255
      },
      "p1": -1,
      "p2": null,
      "hue": "shorter",
      "space": "srgb"
    }
  },
  {
    "args": {
      "a": {
        "b": 3,
        "g": 2,
        "r": 1,
        "alpha": 255
      },
      "b": {
        "b": 6,
        "g": 5,
        "r": 4,
        "alpha": 255
      },
      "p1": "NaN",
      "p2": null,
      "hue": "shorter",
      "space": "srgb"
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 20 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%.