on dcbe2fc help sign in
elk/libraries/Native/Color.js
68 lines 1.9 kB history raw
1Elm.Native.Color = {};
2Elm.Native.Color.make = function(elm) {
3 elm.Native = elm.Native || {};
4 if (elm.Native.Color) return elm.Native.Color;
6 var Utils = Elm.Native.Utils.make(elm);
8 function complement(rgb) {
9 var hsv = toHSV(rgb);
10 hsv.hue = (hsv.hue + 180) % 360;
11 return toRGB(hsv);
12 }
14 function hsva(h,s,v,a) {
15 var degree = A2(Utils.mod, h * 180 / Math.PI, 360);
16 var clr = toRGB({hue:degree, saturation:s, value:v});
17 clr._3 = a;
18 return clr;
19 }
21 function hsv(h,s,v) {
22 var degree = A2(Utils.mod, h * 180 / Math.PI, 360);
23 return toRGB({hue:degree, saturation:s, value:v});
24 }
26 function toHSV(rgb) {
27 var hsv = {};
28 var r = rgb._0 / 255.0, g = rgb._1 / 255.0, b = rgb._2 / 255.0;
29 var M = Math.max(r,g,b);
30 var m = Math.min(r,g,b);
31 var c = M - m;
33 var h = 0;
34 if (c === 0) { h = 0; }
35 else if (M === r) { h = ((g - b) / c) % 6; }
36 else if (M === g) { h = ((b - r) / c) + 2; }
37 else if (M === b) { h = ((r - g) / c) + 4; }
38 h *= 60;
40 return { value : M, hue : h, saturation : (M === 0 ? 0 : c / M) };
41 }
43 function between(lo,hi,x) { return lo <= x && x < hi; }
44 function norm(n) { return Math.round(n*255); }
46 function toRGB(hsv) {
47 var c = hsv.value * hsv.saturation;
48 var hue = hsv.hue / 60;
49 var x = c * (1 - Math.abs((hue % 2) - 1));
50 var r = 0, g = 0, b = 0;
51 if (between(0,1,hue)) { r = c; g = x; b = 0; }
52 else if (between(1,2,hue)) { r = x; g = c; b = 0; }
53 else if (between(2,3,hue)) { r = 0; g = c; b = x; }
54 else if (between(3,4,hue)) { r = 0; g = x; b = c; }
55 else if (between(4,5,hue)) { r = x; g = 0; b = c; }
56 else if (between(5,6,hue)) { r = c; g = 0; b = x; }
58 var m = hsv.value - c;
59 return { ctor:"Color", _0:norm(r+m), _1:norm(g+m), _2:norm(b+m), _3:1 };
60 }
62 return elm.Native.Color = {
63 hsva:F4(hsva),
64 hsv:F3(hsv),
65 complement:complement
66 };
68};