Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #102 - Incorporate Colors lib, add simple test case #113

Merged
merged 1 commit into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
27 changes: 0 additions & 27 deletions src/Core/Color.re

This file was deleted.

35 changes: 35 additions & 0 deletions src/Core/Color_wrapper.re
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion src/Core/Colors.re
Original file line number Diff line number Diff line change
@@ -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.);
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Revery_Core.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Color = Color;
module Color = Color_wrapper;
module Colors = Colors;
module Key = Key;

Expand Down
1 change: 1 addition & 0 deletions src/Core/Window.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open Reglfw;

module Event = Reactify.Event;
module Color = Color_wrapper;

type keyPressEvent = {
codepoint: int,
Expand Down
2 changes: 1 addition & 1 deletion src/Core/dune
Original file line number Diff line number Diff line change
Expand Up @@ -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))
41 changes: 41 additions & 0 deletions test/Core/ColorTests.re
Original file line number Diff line number Diff line change
@@ -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.));
})
});
});