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

WCAG contrast ratio

WCAG 2.x relative luminance and contrast ratio of 8-bit sRGB colors a and b. Each channel s = c ÷ 255 is linearized as s ÷ 12.92 when s ≤ 0.04045, else ((s + 0.055) ÷ 1.055)^2.4. WCAG 2.0 and 2.1 print the threshold as 0.03928, WCAG 2.2 and IEC 61966-2-1 as 0.04045; no 8-bit value lies between them (10 ÷ 255 < 0.03928 and 11 ÷ 255 > 0.04045), so both readings give the same answer. Relative luminance L = 0.2126 × R + 0.7152 × G + 0.0722 × B (WCAG's coefficients, summed left to right); ratio = (max(La, Lb) + 0.05) ÷ (min(La, Lb) + 0.05). The result: luminance-a and luminance-b = round(L × 1,000,000); ratio = round(ratio × 10,000) (210,000 is 21:1); normal-aa: ratio ≥ 4.5; normal-aaa: ratio ≥ 7; large-aa: ratio ≥ 3; large-aaa: ratio ≥ 4.5, each compared on the unrounded ratio (WCAG does not round: 4.499:1 fails 4.5:1). Arithmetic is IEEE 754 binary64, each operation rounded to nearest-even, in the order written (no fused multiply-add). 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-contrast --lang rust
cd color-contrast && 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)32 lines›
color-contrast.wit
package root:component;

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

  export liberas:color/contrast@0.1.0;
}
package liberas:color@0.1.0 {
  interface types {
    record rgb8 {
      r: u8,
      g: u8,
      b: u8,
    }

    record wcag-contrast {
      luminance-a: u32,
      luminance-b: u32,
      ratio: u32,
      normal-aa: bool,
      normal-aaa: bool,
      large-aa: bool,
      large-aaa: bool,
    }
  }
  interface contrast {
    use types.{rgb8, wcag-contrast};

    ratio: func(a: rgb8, b: rgb8) -> wcag-contrast;
  }
}
Test vectors11›
color-contrast.cases.json
[
  {
    "args": {
      "a": {
        "b": 0,
        "g": 0,
        "r": 0
      },
      "b": {
        "b": 255,
        "g": 255,
        "r": 255
      }
    }
  },
  {
    "args": {
      "a": {
        "b": 255,
        "g": 255,
        "r": 255
      },
      "b": {
        "b": 0,
        "g": 0,
        "r": 0
      }
    }
  },
  {
    "args": {
      "a": {
        "b": 118,
        "g": 118,
        "r": 118
      },
      "b": {
        "b": 255,
        "g": 255,
        "r": 255
      }
    }
  },
  {
    "args": {
      "a": {
        "b": 119,
        "g": 119,
        "r": 119
      },
      "b": {
        "b": 255,
        "g": 255,
        "r": 255
      }
    }
  },
  {
    "args": {
      "a": {
        "b": 89,
        "g": 89,
        "r": 89
      },
      "b": {
        "b": 255,
        "g": 255,
        "r": 255
      }
    }
  },
  {
    "args": {
      "a": {
        "b": 0,
        "g": 0,
        "r": 255
      },
      "b": {
        "b": 255,
        "g": 255,
        "r": 255
      }
    }
  },
  {
    "args": {
      "a": {
        "b": 255,
        "g": 0,
        "r": 0
      },
      "b": {
        "b": 0,
        "g": 255,
        "r": 255
      }
    }
  },
  {
    "args": {
      "a": {
        "b": 10,
        "g": 10,
        "r": 10
      },
      "b": {
        "b": 11,
        "g": 11,
        "r": 11
      }
    }
  },
  {
    "args": {
      "a": {
        "b": 128,
        "g": 128,
        "r": 128
      },
      "b": {
        "b": 128,
        "g": 128,
        "r": 128
      }
    }
  },
  {
    "args": {
      "a": {
        "b": 204,
        "g": 102,
        "r": 0
      },
      "b": {
        "b": 240,
        "g": 240,
        "r": 240
      }
    }
  },
  {
    "args": {
      "a": {
        "b": 148,
        "g": 148,
        "r": 148
      },
      "b": {
        "b": 0,
        "g": 0,
        "r": 0
      }
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 11 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%.