<!doctype html>
<meta charset="utf-8">
<title>CSSOKLCH tests</title>
<link rel="help" href="https://drafts.css-houdini.org/css-typed-om-1/#csshwb">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/testhelper.js"></script>
<script>
'use strict';

const gValidLightnessInputs = [
  {lightness: CSS.percent(50), desc: 'percent', expected: CSS.percent(50)},
  {lightness: 0.5, desc: 'number', expected: CSS.percent(50)},
];

const gInvalidLightnessInputs = [
  {lightness: CSS.px(1), desc: "css pixels"},
  {lightness: CSS.deg(180), desc: "degrees"},
]

for (const {lightness, desc, expected} of gValidLightnessInputs) {
  test(() => {
    const result = new CSSOKLCH(lightness, 0.5, 0.5);
    assert_color_channel_approx_equals(result.l, expected);
  }, `Constructing a CSSOKLCH with ${desc} for the lightness works as intended.`);

  test(() => {
    const result = new CSSOKLCH(CSS.percent(0), 0.5, 0.5);
    result.l = lightness;
    assert_color_channel_approx_equals(result.l, expected);
  }, `CSSOKLCH.l can be updated with a ${desc}.`);
}

for (const {lightness, desc} of gInvalidLightnessInputs) {
  test(() => {
    assert_throws_dom("SyntaxError", () => new CSSOKLCH(lightness, 0, 0));
  }, `Constructing a CSSOKLCH with ${desc} for lightness throws a SyntaxError.`);
}

test(() => {
  const result = new CSSOKLCH(CSS.percent(27), 0.7, 8);
  assert_color_channel_approx_equals(result.l, CSS.percent(27));
  assert_color_channel_approx_equals(result.c, CSS.percent(70));
  assert_color_channel_approx_equals(result.h, CSS.deg(8));
  assert_color_channel_approx_equals(result.alpha, CSS.percent(100));
}, 'CSSOKLCH can be constructed from three numbers and will get an alpha of 100%.');

test(() => {
  const result = new CSSOKLCH(0.2, 0.7, CSS.rad(8), CSS.percent(0.4));
  assert_color_channel_approx_equals(result.l, CSS.percent(20));
  assert_color_channel_approx_equals(result.c, CSS.percent(70));
  assert_color_channel_approx_equals(result.h, CSS.rad(8));
  assert_color_channel_approx_equals(result.alpha, CSS.percent(0.4));
}, 'CSSOKLCH can be constructed from four numbers.');

test(() => {
  assert_throws_dom("SyntaxError", () => new CSSOKLCH(CSS.number(1), 0, 0, 0));
  assert_throws_dom("SyntaxError", () => new CSSOKLCH(0, CSS.number(1), 0, 0));
  assert_throws_dom("SyntaxError", () => new CSSOKLCH(0, 0, CSS.number(1), 0));
  assert_throws_dom("SyntaxError", () => new CSSOKLCH(0, 0, 0, CSS.number(1)));
}, `Constructing a CSSOKLCH with CSS.number for l, c, h or alpha throws a SyntaxError`);

for (const attr of ['l', 'c', 'alpha']) {
  test(() => {
    const result = new CSSOKLCH(CSS.percent(0), 0, 0);
    assert_throws_dom("SyntaxError", () => result[attr] = CSS.number(1));
  }, `Updating a CSSOKLCH with CSS.number for ${attr} throws a SyntaxError`);

  test(() => {
    const result = new CSSOKLCH(CSS.percent(0), 0, 0);
    result[attr] = 0.5;
    assert_color_channel_approx_equals(result[attr], CSS.percent(50));
  }, 'CSSOKLCH.' + attr + ' can be updated to a number.');

  test(() => {
    const result = new CSSOKLCH(CSS.percent(0), 0, 0);
    result[attr] = CSS.percent(50);
    assert_color_channel_approx_equals(result[attr], CSS.percent(50));
  }, 'CSSOKLCH.' + attr + ' can be updated with a CSS percent.');
}
</script>
