Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
color / color-hsv-to-srgbOpenRuns on Liberas

HSV to sRGB

8-bit sRGB of HSV h (degrees), s and v (percent). Every present number must be finite with magnitude at most 1,000,000, else an error. h becomes h fmod 360, + 360 if negative, and 360 becomes 0; S = clamp(s, 0, 100) ÷ 100; V = clamp(v, 0, 100) ÷ 100; W = (1 − S) × V; B = 1 − V. Then CSS Color 4's hwbToRgb on fractions: if W + B ≥ 1 every channel is W ÷ (W + B); else each channel x of hslToRgb(h, 1, 0.5) — f(n) = l − a × max(−1, min(k − 3, 9 − k, 1)) with k = (n + h ÷ 30) fmod 12, a = s × min(l, 1 − l), for n = 0, 8, 4 (red, green, blue) — becomes x × (1 − W − B) + W, (1 − W − B) left to right. Each channel is round(clamp(x, 0, 1) × 255), half away from zero. Arithmetic is IEEE 754 binary64, each operation rounded to nearest-even, in the order written (no fused multiply-add). No libm function is involved, so results match bit for bit.

From your terminal

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

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

  export liberas:color/from-hsv@0.1.0;
}
package liberas:color@0.1.0 {
  interface types {
    record rgb8 {
      r: u8,
      g: u8,
      b: u8,
    }
  }
  interface from-hsv {
    use types.{rgb8};

    from-hsv: func(h: f64, s: f64, v: f64) -> result<rgb8, string>;
  }
}
Test vectors14›
color-hsv-to-srgb.cases.json
[
  {
    "args": {
      "h": 0,
      "s": 100,
      "v": 100
    }
  },
  {
    "args": {
      "h": 120,
      "s": 100,
      "v": 100
    }
  },
  {
    "args": {
      "h": 240,
      "s": 100,
      "v": 50
    }
  },
  {
    "args": {
      "h": 60,
      "s": 50,
      "v": 50
    }
  },
  {
    "args": {
      "h": 210,
      "s": 61.11,
      "v": 70.59
    }
  },
  {
    "args": {
      "h": 300,
      "s": 0,
      "v": 75
    }
  },
  {
    "args": {
      "h": -30,
      "s": 100,
      "v": 100
    }
  },
  {
    "args": {
      "h": 725.5,
      "s": 33.3,
      "v": 66.6
    }
  },
  {
    "args": {
      "h": 359.999,
      "s": 100,
      "v": 100
    }
  },
  {
    "args": {
      "h": 90,
      "s": 150,
      "v": -10
    }
  },
  {
    "args": {
      "h": 180,
      "s": 100,
      "v": 0
    }
  },
  {
    "args": {
      "h": "NaN",
      "s": 50,
      "v": 50
    }
  },
  {
    "args": {
      "h": 0,
      "s": "Infinity",
      "v": 50
    }
  },
  {
    "args": {
      "h": 1000001,
      "s": 0,
      "v": 0
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 14 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%.