on master help sign in
Convert Color and Text modules to new format.
76e83e0evancz
libraries/Native/Graphics/Color.js+36−36
@@ -1,29 +1,29 @@
1−/**
2−module Native.Color where
3−**/
4
1
5−(function() {
2+Elm.Native.Color = function(elm) {
6−"use strict";
3+ "use strict";
7
4
5+ elm.Native = elm.Native || {};
6+ if (elm.Native.Color) return elm.Native.Color;
7+
8−function extract(c) {
8+ function extract(c) {
9− if (c._3 === 1) { return 'rgb(' + c._0 + ',' + c._1 + ',' + c._2 + ')'; }
9+ if (c._3 === 1) { return 'rgb(' + c._0 + ',' + c._1 + ',' + c._2 + ')'; }
10− return 'rgba(' + c._0 + ',' + c._1 + ',' + c._2 + ',' + c._3 + ')';
10+ return 'rgba(' + c._0 + ',' + c._1 + ',' + c._2 + ',' + c._3 + ')';
11−}
11+ }
12−function complement(rgb) {
12+ function complement(rgb) {
13 var hsv = toHSV(rgb);
13 var hsv = toHSV(rgb);
14 hsv.hue = (hsv.hue + 180) % 360;
14 hsv.hue = (hsv.hue + 180) % 360;
15 return toRGB(hsv);
15 return toRGB(hsv);
16−}
16+ }
17
17
18−function hsva(h,s,v,a) {
18+ function hsva(h,s,v,a) {
19 var clr = toRGB({hue:h, saturation:s, value:v});
19 var clr = toRGB({hue:h, saturation:s, value:v});
20 clr._3 = a;
20 clr._3 = a;
21 return clr;
21 return clr;
22−}
22+ }
23
23
24−function hsv(h,s,v) { return toRGB({hue:h, saturation:s, value:v}); }
24+ function hsv(h,s,v) { return toRGB({hue:h, saturation:s, value:v}); }
25
25
26−function toHSV(rgb) {
26+ function toHSV(rgb) {
27 var hsv = {};
27 var hsv = {};
28 var r = rgb._0 / 255.0, g = rgb._1 / 255.0, b = rgb._2 / 255.0;
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);
29 var M = Math.max(r,g,b);
@@ -31,39 +31,39 @@ function toHSV(rgb) {
31 var c = M - m;
31 var c = M - m;
32
32
33 var h = 0;
33 var h = 0;
34− if (c === 0) { h = 0; }
34+ if (c === 0) { h = 0; }
35 else if (M === r) { h = ((g - b) / c) % 6; }
35 else if (M === r) { h = ((g - b) / c) % 6; }
36 else if (M === g) { h = ((b - r) / c) + 2; }
36 else if (M === g) { h = ((b - r) / c) + 2; }
37 else if (M === b) { h = ((r - g) / c) + 4; }
37 else if (M === b) { h = ((r - g) / c) + 4; }
38 h *= 60;
38 h *= 60;
39
39
40 return { value : M, hue : h, saturation : (M === 0 ? 0 : c / M) };
40 return { value : M, hue : h, saturation : (M === 0 ? 0 : c / M) };
41−}
41+ }
42
42
43−function between(lo,hi,x) { return lo <= x && x < hi; }
43+ function between(lo,hi,x) { return lo <= x && x < hi; }
44−function norm(n) { return Math.round(n*255); }
44+ function norm(n) { return Math.round(n*255); }
45
45
46−function toRGB(hsv) {
46+ function toRGB(hsv) {
47− var c = hsv.value * hsv.saturation;
47+ var c = hsv.value * hsv.saturation;
48− var hue = hsv.hue / 60;
48+ var hue = hsv.hue / 60;
49− var x = c * (1 - Math.abs((hue % 2) - 1));
49+ var x = c * (1 - Math.abs((hue % 2) - 1));
50− var r = 0, g = 0, b = 0;
50+ var r = 0, g = 0, b = 0;
51− if (between(0,1,hue)) { r = c; g = x; 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; }
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; }
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; }
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; }
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; }
56+ else if (between(5,6,hue)) { r = c; g = 0; b = x; }
57
57
58− var m = hsv.value - c;
58+ var m = hsv.value - c;
59− return { ctor:"Color", _0:norm(r+m), _1:norm(g+m), _2:norm(b+m), _3:1 };
59+ return { ctor:"Color", _0:norm(r+m), _1:norm(g+m), _2:norm(b+m), _3:1 };
60−}
60+ }
61
61
62−Elm.Native.Color = {
62+ return elm.Native.Color = {
63 hsva:F4(hsva),
63 hsva:F4(hsva),
64 hsv:F3(hsv),
64 hsv:F3(hsv),
65 complement:complement,
65 complement:complement,
66 extract:extract
66 extract:extract
67−};
67+ };
68
68
69−}());
69+};
libraries/Native/Graphics/Text.js+14−15
@@ -1,14 +1,14 @@
1
1
2−/**
3−module Native.Graphics.Text where
2+Elm.Native.Graphics.Text = function(elm) {
3+ 'use strict';
4
4
5−import JavaScript
5+ elm.Native = elm.Native || {};
6−import Native.Graphics.Color
6+ elm.Native.Graphics = elm.Native.Graphics || {};
7−import Graphics.Element
7+ if (elm.Native.Graphics.Text) return elm.Native.Graphics.Text;
8−**/
9
8
10−(function() {
9+ var JS = Elm.JavaScript(elm);
11− 'use strict';
10+ var Color = Elm.Native.Graphics.Color(elm);
11+ var Element = Elm.Graphics.Element(elm);
12
12
13 function makeSpaces(s) {
13 function makeSpaces(s) {
14 if (s.length == 0) { return s; }
14 if (s.length == 0) { return s; }
@@ -51,7 +51,7 @@ import Graphics.Element
51 return arr.join('<br/>');
51 return arr.join('<br/>');
52 }
52 }
53
53
54− function toText(str) { return properEscape(Elm.JavaScript.fromString(str)); }
54+ function toText(str) { return properEscape(JS.fromString(str)); }
55
55
56 function addTag(tag) { return function(text) {
56 function addTag(tag) { return function(text) {
57 return '<' + tag + ' style="padding:0;margin:0">' + text + '</' + tag + '>';
57 return '<' + tag + ' style="padding:0;margin:0">' + text + '</' + tag + '>';
@@ -63,7 +63,7 @@ import Graphics.Element
63 }
63 }
64
64
65 function typeface(name, text) {
65 function typeface(name, text) {
66− return addStyle('font-family', Elm.JavaScript.fromString(name), text);
66+ return addStyle('font-family', JS.fromString(name), text);
67 }
67 }
68 function size(px, text) { return addStyle('font-size', px + 'px', text) }
68 function size(px, text) { return addStyle('font-size', px + 'px', text) }
69 var header = addTag('h1');
69 var header = addTag('h1');
@@ -71,7 +71,7 @@ import Graphics.Element
71 function italic(text) { return addStyle('font-style', 'italic', text) }
71 function italic(text) { return addStyle('font-style', 'italic', text) }
72 var bold = addTag('b');
72 var bold = addTag('b');
73 function color(c, text) {
73 function color(c, text) {
74− return addStyle('color', Elm.Native.Graphics.Color.extract(c), text);
74+ return addStyle('color', Color.extract(c), text);
75 }
75 }
76 function underline(text) { return addStyle('text-decoration', 'underline', text) }
76 function underline(text) { return addStyle('text-decoration', 'underline', text) }
77 function overline(text) { return addStyle('text-decoration', 'overline', text) }
77 function overline(text) { return addStyle('text-decoration', 'overline', text) }
@@ -82,12 +82,11 @@ import Graphics.Element
82 return "<a href='" + toText(href) + "'>" + text + "</a>";
82 return "<a href='" + toText(href) + "'>" + text + "</a>";
83 }
83 }
84
84
85− var newElem = Elm.Graphics.Element.newElement;
86 function position(pos) { return function(text) {
85 function position(pos) { return function(text) {
87 var e = {ctor:'RawHtml',
86 var e = {ctor:'RawHtml',
88 _0: '<div style="padding:0;margin:0;text-align:'+pos+'">'+text+'</div>'
87 _0: '<div style="padding:0;margin:0;text-align:'+pos+'">'+text+'</div>'
89 };
88 };
90− return A3(newElem, 0, 0, e);
89+ return A3(Element.newElement, 0, 0, e);
91 }
90 }
92 }
91 }
93
92
@@ -95,7 +94,7 @@ import Graphics.Element
95 return position('left')(typeface('monospace', toText(show(v))));
94 return position('left')(typeface('monospace', toText(show(v))));
96 }
95 }
97
96
98− Elm.Native.Graphics.Text = {
97+ return elm.Native.Graphics.Text = {
99 toText: toText,
98 toText: toText,
100
99
101 header : header,
100 header : header,
@@ -118,4 +117,4 @@ import Graphics.Element
118 asText : asText
117 asText : asText
119 };
118 };
120
119
121−}())
120+};