From 4ee0dfae75fda3a8b6347d55c728b50ce5c210d9 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 26 Jul 2020 18:16:59 +0200 Subject: Allow to customize icons --- src/Color.ml | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/Color.ml (limited to 'src/Color.ml') diff --git a/src/Color.ml b/src/Color.ml new file mode 100644 index 0000000..b3d2f91 --- /dev/null +++ b/src/Color.ml @@ -0,0 +1,38 @@ +let from_sRGB sRGB = + if sRGB <= 0.03928 then + sRGB /. 12.92 + else + ((sRGB +. 0.055) /. 1.055) ** 2.4 + +type rgb = + { r: float + ; g: float + ; b: float + } + +(* https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef *) +let relativeLuminance (c: rgb) = + 0.2126 *. from_sRGB (c.r /. 255.) +. 0.7152 *. from_sRGB (c.g /. 255.) +. 0.0722 *. from_sRGB (c.b /. 255.) + +(* https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrastratio *) +let contrast_ratio (c1: rgb) (c2: rgb) = + let rl1 = relativeLuminance c1 in + let rl2 = relativeLuminance c2 in + + if (rl1 > rl2) then + (rl1 +. 0.05) /. (rl2 +. 0.05) + else + (rl2 +. 0.05) /. (rl1 +. 0.05) + +let from_raw color = + let get_opt = function | Some x -> x | None -> raise (Invalid_argument "Option.get") in + let div = H.div [| HA.style ("color: " ^ color) |] [| |] in + let body = Document.querySelectorUnsafe "body" in + let () = Element.appendChild body div in + let rgb = [%raw {| window.getComputedStyle(div).color |}] in + let () = Element.removeChild body div in + let xs = Js.String.split ", " (get_opt (Js.String.splitByRe [%re "/[()]/"] rgb).(1)) in + { r = Js.Float.fromString xs.(0) + ; g = Js.Float.fromString xs.(1) + ; b = Js.Float.fromString xs.(2) + } -- cgit v1.2.3