diff --git a/package.json b/package.json index 7faff18bf..b957a7c6a 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "reason-fontkit": "^1.1.0", "reason-gl-matrix": "^0.2.0", "reason-reactify": "^2.1.0", + "@opam/color": "^0.2.0", "@opam/lwt": "^4.0.0", "@opam/lwt_ppx": "^1.1.0", "@opam/js_of_ocaml": "*", diff --git a/src/Core/Color.re b/src/Core/Color.re deleted file mode 100644 index 5517291af..000000000 --- a/src/Core/Color.re +++ /dev/null @@ -1,27 +0,0 @@ -open Reglm; - -type t = { - r: float, - g: float, - b: float, - a: float, -}; - -let rgba = (r: float, g: float, b: float, a: float) => { - let c: t = {r, g, b, a}; - c; -}; - -let rgb = (r: float, g: float, b: float) => { - let c = rgba(r, g, b, 1.0); - c; -}; - -let multiplyAlpha = (opacity: float, color: t) => { - let ret: t = {...color, a: opacity *. color.a}; - ret; -}; - -let toVec3 = (color: t) => Vec3.create(color.r, color.g, color.b); - -let toVec4 = (color: t) => Vec4.create(color.r, color.g, color.b, color.a); diff --git a/src/Core/Color_wrapper.re b/src/Core/Color_wrapper.re new file mode 100644 index 000000000..02a4bcc6f --- /dev/null +++ b/src/Core/Color_wrapper.re @@ -0,0 +1,35 @@ +open Reglm; + +type t = Color.Rgba'.t; + +let rgba = (r, g, b, a) => { + Color.of_rgba'(r, g, b, a) + |> Color.to_rgba'; +}; + +let rgb = (r, g, b) => { + Color.of_rgb'(r, g, b) + |> Color.to_rgba'; +}; + +exception ColorHexParseException(string); + +let parseOrThrow = (str, c) => { + switch (c) { + | Some(v) => v + | None => raise(ColorHexParseException("Unable to parse color: " ++ str)) + }; +}; + +let hex = c => c + |> Color.of_hexstring + |> parseOrThrow(c) + |> Color.to_rgba'; + +let multiplyAlpha = (opacity: float, color: t) => { + let ret: t = {...color, a: opacity *. color.a}; + ret; +}; + +let toVec3 = (color: t) => Vec3.create(color.r, color.g, color.b); +let toVec4 = (color: t) => Vec4.create(color.r, color.g, color.b, color.a); diff --git a/src/Core/Colors.re b/src/Core/Colors.re index d4fcb6e65..6951277d7 100644 --- a/src/Core/Colors.re +++ b/src/Core/Colors.re @@ -1,4 +1,4 @@ -open Color; +open Color_wrapper; let transparentWhite = rgba(1.0, 1.0, 1.0, 0.0); let transparentBlack = rgba(0., 0., 0., 0.); diff --git a/src/Core/Revery_Core.re b/src/Core/Revery_Core.re index 464836747..1e8fdca71 100644 --- a/src/Core/Revery_Core.re +++ b/src/Core/Revery_Core.re @@ -1,4 +1,4 @@ -module Color = Color; +module Color = Color_wrapper; module Colors = Colors; module Key = Key; diff --git a/src/Core/Window.re b/src/Core/Window.re index e6e33cb55..5552519d3 100644 --- a/src/Core/Window.re +++ b/src/Core/Window.re @@ -1,6 +1,7 @@ open Reglfw; module Event = Reactify.Event; +module Color = Color_wrapper; type keyPressEvent = { codepoint: int, diff --git a/src/Core/dune b/src/Core/dune index 571288f76..cba6b6ff9 100644 --- a/src/Core/dune +++ b/src/Core/dune @@ -3,4 +3,4 @@ (public_name Revery_Core) (js_of_ocaml (javascript_files file.js)) (cxx_names file) - (libraries lwt lwt.unix reglfw flex fontkit reactify)) + (libraries color lwt lwt.unix reglfw flex fontkit reactify)) diff --git a/test/Core/ColorTests.re b/test/Core/ColorTests.re new file mode 100644 index 000000000..00b45d4c7 --- /dev/null +++ b/test/Core/ColorTests.re @@ -0,0 +1,41 @@ +open Rejest; + +open Revery_Core; + +let validateColor = (actual: Color.t, expected: Color.t) => { + expect(actual.r).toEqual(expected.r); + expect(actual.g).toEqual(expected.g); + expect(actual.b).toEqual(expected.b); + expect(actual.a).toEqual(expected.a); +}; + +test("Color", () => { + test("hex parsing", () => { + test("16-bit RGB cases", () => { + let color1 = Color.hex("#000"); + let color2 = Color.hex("#F00"); + let color3 = Color.hex("#0F0"); + let color4 = Color.hex("#00F"); + let color5 = Color.hex("#FFF"); + + validateColor(color1, Color.rgb(0., 0., 0.)); + validateColor(color2, Color.rgb(1., 0., 0.)); + validateColor(color3, Color.rgb(0., 1., 0.)); + validateColor(color4, Color.rgb(0., 0., 1.)); + validateColor(color5, Color.rgb(1., 1., 1.)); + }) + test("256-bit RGB cases", () => { + let color1 = Color.hex("#000000"); + let color2 = Color.hex("#FF0000"); + let color3 = Color.hex("#00FF00"); + let color4 = Color.hex("#0000FF"); + let color5 = Color.hex("#FFFFFF"); + + validateColor(color1, Color.rgb(0., 0., 0.)); + validateColor(color2, Color.rgb(1., 0., 0.)); + validateColor(color3, Color.rgb(0., 1., 0.)); + validateColor(color4, Color.rgb(0., 0., 1.)); + validateColor(color5, Color.rgb(1., 1., 1.)); + }) + }); +});