on master help sign in
Convert Color and Text modules to new format.
commit76e83e0e1b0bef22c8f3c80babcd6ec77fe64258 authorevancz <info@elm-lang.org> dateSat 9 Mar 2013, 00:22 (14 years ago) parent0fe301a
2 files changed+50−51
libraries/Native/Graphics/Color.js+36−36 libraries/Native/Graphics/Text.js+14−15
libraries/Native/Graphics/Color.js+36−36
@@ -1,29 +1,29 @@
1−/**
2−module Native.Color where
3−**/
41
5−(function() {
6−"use strict";
2+Elm.Native.Color = function(elm) {
3+ "use strict";
74
8−function extract(c) {
9− if (c._3 === 1) { return 'rgb(' + c._0 + ',' + c._1 + ',' + c._2 + ')'; }
10− return 'rgba(' + c._0 + ',' + c._1 + ',' + c._2 + ',' + c._3 + ')';
11−}
12−function complement(rgb) {
5+ elm.Native = elm.Native || {};
6+ if (elm.Native.Color) return elm.Native.Color;
7+
8+ function extract(c) {
9+ if (c._3 === 1) { return 'rgb(' + c._0 + ',' + c._1 + ',' + c._2 + ')'; }
10+ return 'rgba(' + c._0 + ',' + c._1 + ',' + c._2 + ',' + c._3 + ')';
11+ }
12+ function complement(rgb) {
1313 var hsv = toHSV(rgb);
1414 hsv.hue = (hsv.hue + 180) % 360;
1515 return toRGB(hsv);
16−}
16+ }
1717
18−function hsva(h,s,v,a) {
18+ function hsva(h,s,v,a) {
1919 var clr = toRGB({hue:h, saturation:s, value:v});
2020 clr._3 = a;
2121 return clr;
22−}
22+ }
2323
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}); }
2525
26−function toHSV(rgb) {
26+ function toHSV(rgb) {
2727 var hsv = {};
2828 var r = rgb._0 / 255.0, g = rgb._1 / 255.0, b = rgb._2 / 255.0;
2929 var M = Math.max(r,g,b);
@@ -31,39 +31,39 @@ function toHSV(rgb) {
3131 var c = M - m;
3232
3333 var h = 0;
34− if (c === 0) { h = 0; }
34+ if (c === 0) { h = 0; }
3535 else if (M === r) { h = ((g - b) / c) % 6; }
3636 else if (M === g) { h = ((b - r) / c) + 2; }
3737 else if (M === b) { h = ((r - g) / c) + 4; }
3838 h *= 60;
3939
4040 return { value : M, hue : h, saturation : (M === 0 ? 0 : c / M) };
41−}
41+ }
4242
43−function between(lo,hi,x) { return lo <= x && x < hi; }
44−function norm(n) { return Math.round(n*255); }
43+ function between(lo,hi,x) { return lo <= x && x < hi; }
44+ function norm(n) { return Math.round(n*255); }
4545
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; }
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; }
5757
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−}
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+ }
6161
62−Elm.Native.Color = {
62+ return elm.Native.Color = {
6363 hsva:F4(hsva),
6464 hsv:F3(hsv),
6565 complement:complement,
6666 extract:extract
67−};
67+ };
6868
69−}());
69+};
libraries/Native/Graphics/Text.js+14−15
@@ -1,14 +1,14 @@
11
2−/**
3−module Native.Graphics.Text where
2+Elm.Native.Graphics.Text = function(elm) {
3+ 'use strict';
44
5−import JavaScript
6−import Native.Graphics.Color
7−import Graphics.Element
8−**/
5+ elm.Native = elm.Native || {};
6+ elm.Native.Graphics = elm.Native.Graphics || {};
7+ if (elm.Native.Graphics.Text) return elm.Native.Graphics.Text;
98
10−(function() {
11− 'use strict';
9+ var JS = Elm.JavaScript(elm);
10+ var Color = Elm.Native.Graphics.Color(elm);
11+ var Element = Elm.Graphics.Element(elm);
1212
1313 function makeSpaces(s) {
1414 if (s.length == 0) { return s; }
@@ -51,7 +51,7 @@ import Graphics.Element
5151 return arr.join('<br/>');
5252 }
5353
54− function toText(str) { return properEscape(Elm.JavaScript.fromString(str)); }
54+ function toText(str) { return properEscape(JS.fromString(str)); }
5555
5656 function addTag(tag) { return function(text) {
5757 return '<' + tag + ' style="padding:0;margin:0">' + text + '</' + tag + '>';
@@ -63,7 +63,7 @@ import Graphics.Element
6363 }
6464
6565 function typeface(name, text) {
66− return addStyle('font-family', Elm.JavaScript.fromString(name), text);
66+ return addStyle('font-family', JS.fromString(name), text);
6767 }
6868 function size(px, text) { return addStyle('font-size', px + 'px', text) }
6969 var header = addTag('h1');
@@ -71,7 +71,7 @@ import Graphics.Element
7171 function italic(text) { return addStyle('font-style', 'italic', text) }
7272 var bold = addTag('b');
7373 function color(c, text) {
74− return addStyle('color', Elm.Native.Graphics.Color.extract(c), text);
74+ return addStyle('color', Color.extract(c), text);
7575 }
7676 function underline(text) { return addStyle('text-decoration', 'underline', text) }
7777 function overline(text) { return addStyle('text-decoration', 'overline', text) }
@@ -82,12 +82,11 @@ import Graphics.Element
8282 return "<a href='" + toText(href) + "'>" + text + "</a>";
8383 }
8484
85− var newElem = Elm.Graphics.Element.newElement;
8685 function position(pos) { return function(text) {
8786 var e = {ctor:'RawHtml',
8887 _0: '<div style="padding:0;margin:0;text-align:'+pos+'">'+text+'</div>'
8988 };
90− return A3(newElem, 0, 0, e);
89+ return A3(Element.newElement, 0, 0, e);
9190 }
9291 }
9392
@@ -95,7 +94,7 @@ import Graphics.Element
9594 return position('left')(typeface('monospace', toText(show(v))));
9695 }
9796
98− Elm.Native.Graphics.Text = {
97+ return elm.Native.Graphics.Text = {
9998 toText: toText,
10099
101100 header : header,
@@ -118,4 +117,4 @@ import Graphics.Element
118117 asText : asText
119118 };
120119
121−}())
120+};