Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

General improvement. #2

Closed
wants to merge 2 commits into from

2 participants

@LeoDutra
  • omited "{" "}" on some blocks;
  • faster operator for equality ("===");
  • fixed some jsHint pointings;
  • optimized a condition;
  • minor optimizations of size and performance.

:D

LeoDutra added some commits
@LeoDutra LeoDutra General improvement.
 - omited "{" "}" on some blocks;
 - faster operator for equality ("===");
 - fixed some jsHint pointings;
 - optimized a condition;
 - minor optimizations of size and performance.
9851f46
@LeoDutra LeoDutra 8.76KB (3.36KB gzipped). Closure compiled. 8b1ec90
@LeoDutra LeoDutra closed this
@LeoDutra LeoDutra reopened this
@LeoDutra

Brian, please review "hasOwnProperty" substituition.
I don't expect negative values on the new version.
Please point me any mistakes and I'll come with another solution.

And nice job till here!

@bgrins
Owner

Thanks for the contribution!

A few notes:

  • The compiler should remove the extra braces, and I keep them in the source based on convention - I think it makes the blocks more explicit and easier to read.
  • All the === blocks are good changes.
  • Good catch on some of the mathRound type fixes - minimizing the size of the compressed source is definitely a priority.
  • hasOwnProperty: I would prefer to keep this in to prevent any ambiguity with the negative values as you mention. I would guess that it actually doesn't save too much space once compressed to use it as it probably gzips well, but it would be an interesting comparison in a different commit (maybe creating a hasOwnProperty function that takes in object and field).

Thanks for the interest and effort put into this commit - if you can revert some of the coding style changes ({} and hasOwnProperty) just to keep everything consistent I will push it into master.

@LeoDutra

I'll do it in almost no time. I'm finishing a "no-lib" slider for iOS and Android.
;)

@bgrins bgrins closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 4, 2012
  1. @LeoDutra

    General improvement.

    LeoDutra authored
     - omited "{" "}" on some blocks;
     - faster operator for equality ("===");
     - fixed some jsHint pointings;
     - optimized a condition;
     - minor optimizations of size and performance.
  2. @LeoDutra
This page is out of date. Refresh to see the latest.
Showing with 80 additions and 102 deletions.
  1. +18 −18 tinycolor-min.js
  2. +62 −84 tinycolor.js
View
36 tinycolor-min.js
@@ -1,19 +1,19 @@
// TinyColor.js - <https://github.com/bgrins/TinyColor> - 2011 Brian Grinstead - v0.5
-(function(r){var s,t,u,v,w,x,y;function d(a,b){if("object"==typeof a&&a.hasOwnProperty("_tc_id"))return a;if("object"==typeof a&&(!b||!b.skipRatio))for(var c in a)1===a[c]&&(a[c]="1.0");c=B(a);var j=c.r,e=c.g,d=c.b,h=n(c.a);1>j&&(j=f(j));1>e&&(e=f(e));1>d&&(d=f(d));return{ok:c.ok,_tc_id:C++,alpha:h,toHsv:function(){var a=z(j,e,d);return{h:a.h,s:a.s,v:a.v,a:h}},toHsvString:function(){var a=z(j,e,d),c=f(360*a.h),b=f(100*a.s),a=f(100*a.v);return 1==h?"hsv("+c+", "+b+"%, "+a+"%)":"hsva("+c+", "+b+"%, "+
-a+"%, "+h+")"},toHsl:function(){var a=A(j,e,d);return{h:a.h,s:a.s,l:a.l,a:h}},toHslString:function(){var a=A(j,e,d),c=f(360*a.h),b=f(100*a.s),a=f(100*a.l);return 1==h?"hsl("+c+", "+b+"%, "+a+"%)":"hsla("+c+", "+b+"%, "+a+"%, "+h+")"},toHex:function(){return o(j,e,d)},toHexString:function(){return"#"+o(j,e,d)},toRgb:function(){return{r:f(j),g:f(e),b:f(d),a:h}},toRgbString:function(){return 1==h?"rgb("+f(j)+", "+f(e)+", "+f(d)+")":"rgba("+f(j)+", "+f(e)+", "+f(d)+", "+h+")"},toName:function(){return D[o(j,
-e,d)]||!1},toFilter:function(){var a=o(j,e,d),c=Math.round(255*n(h)).toString(16);return"progid:DXImageTransform.Microsoft.gradient(startColorstr=#"+c+a+",endColorstr=#"+c+a+")"}}}function B(a){var b={r:255,g:255,b:255},c=1,d=!1;if("string"==typeof a)if(a=a.replace(E,"").replace(F,"").toLowerCase(),p[a]&&(a=p[a]),"transparent"==a)a={r:0,g:0,b:0,a:0};else var e,a=(e=s.exec(a))?{r:e[1],g:e[2],b:e[3]}:(e=t.exec(a))?{r:e[1],g:e[2],b:e[3],a:e[4]}:(e=u.exec(a))?{h:e[1],s:e[2],l:e[3]}:(e=v.exec(a))?{h:e[1],
-s:e[2],l:e[3],a:e[4]}:(e=w.exec(a))?{h:e[1],s:e[2],v:e[3]}:(e=x.exec(a))?{r:parseInt(e[1],16),g:parseInt(e[2],16),b:parseInt(e[3],16)}:(e=y.exec(a))?{r:parseInt(e[1]+""+e[1],16),g:parseInt(e[2]+""+e[2],16),b:parseInt(e[3]+""+e[3],16)}:!1;if("object"==typeof a){if(a.hasOwnProperty("r")&&a.hasOwnProperty("g")&&a.hasOwnProperty("b"))b={r:255*i(a.r,255),g:255*i(a.g,255),b:255*i(a.b,255)},d=!0;else if(a.hasOwnProperty("h")&&a.hasOwnProperty("s")&&a.hasOwnProperty("v")){e=a.h;var g=a.s,b=a.v;e=6*i(e,360);
-var g=i(g,100),b=i(b,100),d=m.floor(e),h=e-d;e=b*(1-g);var f=b*(1-h*g),g=b*(1-(1-h)*g),d=d%6,b={r:255*[b,f,e,e,g,b][d],g:255*[g,b,b,f,e,e][d],b:255*[e,e,g,b,b,f][d]},d=!0}else a.hasOwnProperty("h")&&a.hasOwnProperty("s")&&a.hasOwnProperty("l")&&(b=G(a.h,a.s,a.l),d=!0);a.hasOwnProperty("a")&&(c=a.a)}return{ok:d,r:k(255,l(b.r,0)),g:k(255,l(b.g,0)),b:k(255,l(b.b,0)),a:c}}function A(a,b,c){var a=i(a,255),b=i(b,255),c=i(c,255),d=l(a,b,c),e=k(a,b,c),g,h=(d+e)/2;if(d==e)g=e=0;else{var f=d-e,e=0.5<h?f/(2-
-d-e):f/(d+e);switch(d){case a:g=(b-c)/f+(b<c?6:0);break;case b:g=(c-a)/f+2;break;case c:g=(a-b)/f+4}g/=6}return{h:g,s:e,l:h}}function G(a,b,c){function d(a,c,b){0>b&&(b+=1);1<b&&(b-=1);return b<1/6?a+6*(c-a)*b:0.5>b?c:b<2/3?a+6*(c-a)*(2/3-b):a}a=i(a,360);b=i(b,100);c=i(c,100);if(0==b)c=b=a=c;else var e=0.5>c?c*(1+b):c+b-c*b,g=2*c-e,c=d(g,e,a+1/3),b=d(g,e,a),a=d(g,e,a-1/3);return{r:255*c,g:255*b,b:255*a}}function z(a,b,c){var a=i(a,255),b=i(b,255),c=i(c,255),d=l(a,b,c),e=k(a,b,c),g,f=d-e;if(d==e)g=
-0;else{switch(d){case a:g=(b-c)/f+(b<c?6:0);break;case b:g=(c-a)/f+2;break;case c:g=(a-b)/f+4}g/=6}return{h:g,s:0==d?0:f/d,v:d}}function o(a,b,c){function d(a){return 1==a.length?"0"+a:""+a}a=[d(f(a).toString(16)),d(f(b).toString(16)),d(f(c).toString(16))];return a[0][0]==a[0][1]&&a[1][0]==a[1][1]&&a[2][0]==a[2][1]?a[0][0]+a[1][0]+a[2][0]:a.join("")}function i(a,b){"string"==typeof a&&-1!=a.indexOf(".")&&1===n(a)&&(a="100%");var c="string"===typeof a&&-1!=a.indexOf("%"),a=k(b,l(0,n(a)));c&&(a*=b/
-100);return 1.0E-6>m.abs(a-b)?1:1<=a?a%b/n(b):a}var E=/^[\s,#]+/,F=/\s+$/,C=0,m=Math,f=m.round,k=m.min,l=m.max,q=m.random,n=r.parseFloat;d.equals=function(a,b){return d(a).toHex()==d(b).toHex()};d.random=function(){return d({r:q(),g:q(),b:q()})};d.desaturate=function(a,b){var c=d(a).toHsl();c.s-=(b||10)/100;c.s=k(1,l(0,c.s));return d(c)};d.saturate=function(a,b){var c=d(a).toHsl();c.s+=(b||10)/100;c.s=k(1,l(0,c.s));return d(c)};d.greyscale=function(a){return d.desaturate(a,100)};d.lighten=function(a,
-b){var c=d(a).toHsl();c.l+=(b||10)/100;c.l=k(1,l(0,c.l));return d(c)};d.darken=function(a,b){var c=d(a).toHsl();c.l-=(b||10)/100;c.l=k(1,l(0,c.l));return d(c)};d.complement=function(a){a=d(a).toHsl();a.h=(a.h+0.5)%1;return d(a)};d.triad=function(a){var b=d(a).toHsl(),c=360*b.h;return[d(a),d({h:(c+120)%360,s:b.s,l:b.l}),d({h:(c+240)%360,s:b.s,l:b.l})]};d.tetrad=function(a){var b=d(a).toHsl(),c=360*b.h;return[d(a),d({h:(c+90)%360,s:b.s,l:b.l}),d({h:(c+180)%360,s:b.s,l:b.l}),d({h:(c+270)%360,s:b.s,l:b.l})]};
-d.splitcomplement=function(a){var b=d(a).toHsl(),c=360*b.h;return[d(a),d({h:(c+72)%360,s:b.s,l:b.l}),d({h:(c+216)%360,s:b.s,l:b.l})]};d.analogous=function(a,b,c){var b=b||6,c=c||30,f=d(a).toHsl(),c=360/c,a=[d(a)];f.h*=360;for(f.h=(f.h-(c*b>>1)+720)%360;--b;)f.h=(f.h+c)%360,a.push(d(f));return a};d.monochromatic=function(a,b){for(var b=b||6,c=d(a).toHsv(),f=c.h,e=c.s,c=c.v,g=[];b--;)g.push(d({h:f,s:e,v:c})),c=(c+0.2)%1;return g};d.readable=function(a,b){var c=d(a).toRgb(),f=d(b).toRgb();return 10404<
-(f.r-c.r)*(f.r-c.r)+(f.g-c.g)*(f.g-c.g)+(f.b-c.b)*(f.b-c.b)};var p=d.names={aliceblue:"f0f8ff",antiquewhite:"faebd7",aqua:"0ff",aquamarine:"7fffd4",azure:"f0ffff",beige:"f5f5dc",bisque:"ffe4c4",black:"000",blanchedalmond:"ffebcd",blue:"00f",blueviolet:"8a2be2",brown:"a52a2a",burlywood:"deb887",burntsienna:"ea7e5d",cadetblue:"5f9ea0",chartreuse:"7fff00",chocolate:"d2691e",coral:"ff7f50",cornflowerblue:"6495ed",cornsilk:"fff8dc",crimson:"dc143c",cyan:"0ff",darkblue:"00008b",darkcyan:"008b8b",darkgoldenrod:"b8860b",
-darkgray:"a9a9a9",darkgreen:"006400",darkgrey:"a9a9a9",darkkhaki:"bdb76b",darkmagenta:"8b008b",darkolivegreen:"556b2f",darkorange:"ff8c00",darkorchid:"9932cc",darkred:"8b0000",darksalmon:"e9967a",darkseagreen:"8fbc8f",darkslateblue:"483d8b",darkslategray:"2f4f4f",darkslategrey:"2f4f4f",darkturquoise:"00ced1",darkviolet:"9400d3",deeppink:"ff1493",deepskyblue:"00bfff",dimgray:"696969",dimgrey:"696969",dodgerblue:"1e90ff",firebrick:"b22222",floralwhite:"fffaf0",forestgreen:"228b22",fuchsia:"f0f",gainsboro:"dcdcdc",
-ghostwhite:"f8f8ff",gold:"ffd700",goldenrod:"daa520",gray:"808080",green:"008000",greenyellow:"adff2f",grey:"808080",honeydew:"f0fff0",hotpink:"ff69b4",indianred:"cd5c5c",indigo:"4b0082",ivory:"fffff0",khaki:"f0e68c",lavender:"e6e6fa",lavenderblush:"fff0f5",lawngreen:"7cfc00",lemonchiffon:"fffacd",lightblue:"add8e6",lightcoral:"f08080",lightcyan:"e0ffff",lightgoldenrodyellow:"fafad2",lightgray:"d3d3d3",lightgreen:"90ee90",lightgrey:"d3d3d3",lightpink:"ffb6c1",lightsalmon:"ffa07a",lightseagreen:"20b2aa",
-lightskyblue:"87cefa",lightslategray:"789",lightslategrey:"789",lightsteelblue:"b0c4de",lightyellow:"ffffe0",lime:"0f0",limegreen:"32cd32",linen:"faf0e6",magenta:"f0f",maroon:"800000",mediumaquamarine:"66cdaa",mediumblue:"0000cd",mediumorchid:"ba55d3",mediumpurple:"9370db",mediumseagreen:"3cb371",mediumslateblue:"7b68ee",mediumspringgreen:"00fa9a",mediumturquoise:"48d1cc",mediumvioletred:"c71585",midnightblue:"191970",mintcream:"f5fffa",mistyrose:"ffe4e1",moccasin:"ffe4b5",navajowhite:"ffdead",navy:"000080",
-oldlace:"fdf5e6",olive:"808000",olivedrab:"6b8e23",orange:"ffa500",orangered:"ff4500",orchid:"da70d6",palegoldenrod:"eee8aa",palegreen:"98fb98",paleturquoise:"afeeee",palevioletred:"db7093",papayawhip:"ffefd5",peachpuff:"ffdab9",peru:"cd853f",pink:"ffc0cb",plum:"dda0dd",powderblue:"b0e0e6",purple:"800080",red:"f00",rosybrown:"bc8f8f",royalblue:"4169e1",saddlebrown:"8b4513",salmon:"fa8072",sandybrown:"f4a460",seagreen:"2e8b57",seashell:"fff5ee",sienna:"a0522d",silver:"c0c0c0",skyblue:"87ceeb",slateblue:"6a5acd",
-slategray:"708090",slategrey:"708090",snow:"fffafa",springgreen:"00ff7f",steelblue:"4682b4",tan:"d2b48c",teal:"008080",thistle:"d8bfd8",tomato:"ff6347",turquoise:"40e0d0",violet:"ee82ee",wheat:"f5deb3",white:"fff",whitesmoke:"f5f5f5",yellow:"ff0",yellowgreen:"9acd32"},D=d.hexNames=function(a){var b={},c;for(c in a)a.hasOwnProperty(c)&&(b[a[c]]=c);return b}(p);s=RegExp("rgb[\\s|\\(]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))\\s*\\)?");
-t=RegExp("rgba[\\s|\\(]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))\\s*\\)?");u=RegExp("hsl[\\s|\\(]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))\\s*\\)?");v=RegExp("hsla[\\s|\\(]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))\\s*\\)?");
-w=RegExp("hsv[\\s|\\(]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))\\s*\\)?");y=/^([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/;x=/^([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/;r.tinycolor=d})(this);
+(function(r){var s,t,u,v,w,x,y;function d(a,b){if("object"===typeof a){if(-1<a._tc_id)return a;if(!b||!b.skipRatio)for(var c in a)1===a[c]&&(a[c]="1.0")}c=B(a);var j=c.r,e=c.g,d=c.b,g=n(c.a);1>j&&(j=f(j));1>e&&(e=f(e));1>d&&(d=f(d));return{ok:c.ok,_tc_id:C++,alpha:g,toHsv:function(){var a=z(j,e,d);return{h:a.h,s:a.s,v:a.v,a:g}},toHsvString:function(){var a=z(j,e,d),c=f(360*a.h),b=f(100*a.s),a=f(100*a.v);return 1===g?"hsv("+c+", "+b+"%, "+a+"%)":"hsva("+c+", "+b+"%, "+a+"%, "+g+")"},toHsl:function(){var a=
+A(j,e,d);return{h:a.h,s:a.s,l:a.l,a:g}},toHslString:function(){var a=A(j,e,d),c=f(360*a.h),b=f(100*a.s),a=f(100*a.l);return 1===g?"hsl("+c+", "+b+"%, "+a+"%)":"hsla("+c+", "+b+"%, "+a+"%, "+g+")"},toHex:function(){return o(j,e,d)},toHexString:function(){return"#"+o(j,e,d)},toRgb:function(){return{r:f(j),g:f(e),b:f(d),a:g}},toRgbString:function(){return 1===g?"rgb("+f(j)+", "+f(e)+", "+f(d)+")":"rgba("+f(j)+", "+f(e)+", "+f(d)+", "+g+")"},toName:function(){return D[o(j,e,d)]||!1},toFilter:function(){var a=
+o(j,e,d),c=f(255*n(g)).toString(16);return"progid:DXImageTransform.Microsoft.gradient(startColorstr=#"+c+a+",endColorstr=#"+c+a+")"}}}function B(a){var b={r:255,g:255,b:255},c=1,d=!1;if("string"===typeof a)if(a=a.replace(E,"").replace(F,"").toLowerCase(),p[a]&&(a=p[a]),"transparent"===a)a={r:0,g:0,b:0,a:0};else var e,a=(e=s.exec(a))?{r:e[1],g:e[2],b:e[3]}:(e=t.exec(a))?{r:e[1],g:e[2],b:e[3],a:e[4]}:(e=u.exec(a))?{h:e[1],s:e[2],l:e[3]}:(e=v.exec(a))?{h:e[1],s:e[2],l:e[3],a:e[4]}:(e=w.exec(a))?{h:e[1],
+s:e[2],v:e[3]}:(e=x.exec(a))?{r:parseInt(e[1],16),g:parseInt(e[2],16),b:parseInt(e[3],16)}:(e=y.exec(a))?{r:parseInt(e[1]+""+e[1],16),g:parseInt(e[2]+""+e[2],16),b:parseInt(e[3]+""+e[3],16)}:!1;if("object"===typeof a){if(-1<a.r&&-1<a.g&&-1<a.b)b={r:255*i(a.r,255),g:255*i(a.g,255),b:255*i(a.b,255)},d=!0;else if(-1<a.h&&-1<a.s&&-1<a.v){e=a.h;var h=a.s,b=a.v;e=6*i(e,360);var h=i(h,100),b=i(b,100),d=m.floor(e),g=e-d;e=b*(1-h);var f=b*(1-g*h),h=b*(1-(1-g)*h),d=d%6,b={r:255*[b,f,e,e,h,b][d],g:255*[h,b,
+b,f,e,e][d],b:255*[e,e,h,b,b,f][d]},d=!0}else-1<a.h&&-1<a.s&&-1<a.l&&(b=G(a.h,a.s,a.l),d=!0);-1<a.a&&(c=a.a)}return{ok:d,r:k(255,l(b.r,0)),g:k(255,l(b.g,0)),b:k(255,l(b.b,0)),a:c}}function A(a,b,c){var a=i(a,255),b=i(b,255),c=i(c,255),d=l(a,b,c),e=k(a,b,c),h,g=(d+e)/2;if(d===e)h=e=0;else{var f=d-e,e=0.5<g?f/(2-d-e):f/(d+e);switch(d){case a:h=(b-c)/f+(b<c?6:0);break;case b:h=(c-a)/f+2;break;case c:h=(a-b)/f+4}h/=6}return{h:h,s:e,l:g}}function G(a,b,c){function d(a,c,b){0>b&&(b+=1);1<b&&(b-=1);return b<
+1/6?a+6*(c-a)*b:0.5>b?c:b<2/3?a+6*(c-a)*(2/3-b):a}a=i(a,360);b=i(b,100);c=i(c,100);if(0===b)c=b=a=c;else var e=0.5>c?c*(1+b):c+b-c*b,f=2*c-e,c=d(f,e,a+1/3),b=d(f,e,a),a=d(f,e,a-1/3);return{r:255*c,g:255*b,b:255*a}}function z(a,b,c){var a=i(a,255),b=i(b,255),c=i(c,255),d=l(a,b,c),e=k(a,b,c),f,g=d-e;if(d===e)f=0;else{switch(d){case a:f=(b-c)/g+(b<c?6:0);break;case b:f=(c-a)/g+2;break;case c:f=(a-b)/g+4}f/=6}return{h:f,s:0===d?0:g/d,v:d}}function o(a,b,c){function d(a){return 1===a.length?"0"+a:""+a}
+a=[d(f(a).toString(16)),d(f(b).toString(16)),d(f(c).toString(16))];return a[0][0]===a[0][1]&&a[1][0]===a[1][1]&&a[2][0]===a[2][1]?a[0][0]+a[1][0]+a[2][0]:a.join("")}function i(a,b){"string"===typeof a&&-1!==a.indexOf(".")&&1===n(a)&&(a="100%");var c="string"===typeof a&&-1!==a.indexOf("%"),a=k(b,l(0,n(a)));c&&(a*=b/100);return 1.0E-6>m.abs(a-b)?1:1<=a?a%b/n(b):a}var E=/^[\s,#]+/,F=/\s+$/,C=0,m=Math,f=m.round,k=m.min,l=m.max,q=m.random,n=r.parseFloat;d.equals=function(a,b){return d(a).toHex()===d(b).toHex()};
+d.random=function(){return d({r:q(),g:q(),b:q()})};d.desaturate=function(a,b){var c=d(a).toHsl();c.s-=(b||10)/100;c.s=k(1,l(0,c.s));return d(c)};d.saturate=function(a,b){var c=d(a).toHsl();c.s+=(b||10)/100;c.s=k(1,l(0,c.s));return d(c)};d.greyscale=function(a){return d.desaturate(a,100)};d.lighten=function(a,b){var c=d(a).toHsl();c.l+=(b||10)/100;c.l=k(1,l(0,c.l));return d(c)};d.darken=function(a,b){var c=d(a).toHsl();c.l-=(b||10)/100;c.l=k(1,l(0,c.l));return d(c)};d.complement=function(a){a=d(a).toHsl();
+a.h=(a.h+0.5)%1;return d(a)};d.triad=function(a){var b=d(a).toHsl(),c=360*b.h;return[d(a),d({h:(c+120)%360,s:b.s,l:b.l}),d({h:(c+240)%360,s:b.s,l:b.l})]};d.tetrad=function(a){var b=d(a).toHsl(),c=360*b.h;return[d(a),d({h:(c+90)%360,s:b.s,l:b.l}),d({h:(c+180)%360,s:b.s,l:b.l}),d({h:(c+270)%360,s:b.s,l:b.l})]};d.splitcomplement=function(a){var b=d(a).toHsl(),c=360*b.h;return[d(a),d({h:(c+72)%360,s:b.s,l:b.l}),d({h:(c+216)%360,s:b.s,l:b.l})]};d.analogous=function(a,b,c){var b=b||6,c=c||30,f=d(a).toHsl(),
+c=360/c,a=[d(a)];f.h*=360;for(f.h=(f.h-(c*b>>1)+720)%360;--b;)f.h=(f.h+c)%360,a.push(d(f));return a};d.monochromatic=function(a,b){for(var b=b||6,c=d(a).toHsv(),f=c.h,e=c.s,c=c.v,h=[];b--;)h.push(d({h:f,s:e,v:c})),c=(c+0.2)%1;return h};d.readable=function(a,b){var c=d(a).toRgb(),f=d(b).toRgb();return 10404<(f.r-c.r)*(f.r-c.r)+(f.g-c.g)*(f.g-c.g)+(f.b-c.b)*(f.b-c.b)};var p=d.names={aliceblue:"f0f8ff",antiquewhite:"faebd7",aqua:"0ff",aquamarine:"7fffd4",azure:"f0ffff",beige:"f5f5dc",bisque:"ffe4c4",
+black:"000",blanchedalmond:"ffebcd",blue:"00f",blueviolet:"8a2be2",brown:"a52a2a",burlywood:"deb887",burntsienna:"ea7e5d",cadetblue:"5f9ea0",chartreuse:"7fff00",chocolate:"d2691e",coral:"ff7f50",cornflowerblue:"6495ed",cornsilk:"fff8dc",crimson:"dc143c",cyan:"0ff",darkblue:"00008b",darkcyan:"008b8b",darkgoldenrod:"b8860b",darkgray:"a9a9a9",darkgreen:"006400",darkgrey:"a9a9a9",darkkhaki:"bdb76b",darkmagenta:"8b008b",darkolivegreen:"556b2f",darkorange:"ff8c00",darkorchid:"9932cc",darkred:"8b0000",darksalmon:"e9967a",
+darkseagreen:"8fbc8f",darkslateblue:"483d8b",darkslategray:"2f4f4f",darkslategrey:"2f4f4f",darkturquoise:"00ced1",darkviolet:"9400d3",deeppink:"ff1493",deepskyblue:"00bfff",dimgray:"696969",dimgrey:"696969",dodgerblue:"1e90ff",firebrick:"b22222",floralwhite:"fffaf0",forestgreen:"228b22",fuchsia:"f0f",gainsboro:"dcdcdc",ghostwhite:"f8f8ff",gold:"ffd700",goldenrod:"daa520",gray:"808080",green:"008000",greenyellow:"adff2f",grey:"808080",honeydew:"f0fff0",hotpink:"ff69b4",indianred:"cd5c5c",indigo:"4b0082",
+ivory:"fffff0",khaki:"f0e68c",lavender:"e6e6fa",lavenderblush:"fff0f5",lawngreen:"7cfc00",lemonchiffon:"fffacd",lightblue:"add8e6",lightcoral:"f08080",lightcyan:"e0ffff",lightgoldenrodyellow:"fafad2",lightgray:"d3d3d3",lightgreen:"90ee90",lightgrey:"d3d3d3",lightpink:"ffb6c1",lightsalmon:"ffa07a",lightseagreen:"20b2aa",lightskyblue:"87cefa",lightslategray:"789",lightslategrey:"789",lightsteelblue:"b0c4de",lightyellow:"ffffe0",lime:"0f0",limegreen:"32cd32",linen:"faf0e6",magenta:"f0f",maroon:"800000",
+mediumaquamarine:"66cdaa",mediumblue:"0000cd",mediumorchid:"ba55d3",mediumpurple:"9370db",mediumseagreen:"3cb371",mediumslateblue:"7b68ee",mediumspringgreen:"00fa9a",mediumturquoise:"48d1cc",mediumvioletred:"c71585",midnightblue:"191970",mintcream:"f5fffa",mistyrose:"ffe4e1",moccasin:"ffe4b5",navajowhite:"ffdead",navy:"000080",oldlace:"fdf5e6",olive:"808000",olivedrab:"6b8e23",orange:"ffa500",orangered:"ff4500",orchid:"da70d6",palegoldenrod:"eee8aa",palegreen:"98fb98",paleturquoise:"afeeee",palevioletred:"db7093",
+papayawhip:"ffefd5",peachpuff:"ffdab9",peru:"cd853f",pink:"ffc0cb",plum:"dda0dd",powderblue:"b0e0e6",purple:"800080",red:"f00",rosybrown:"bc8f8f",royalblue:"4169e1",saddlebrown:"8b4513",salmon:"fa8072",sandybrown:"f4a460",seagreen:"2e8b57",seashell:"fff5ee",sienna:"a0522d",silver:"c0c0c0",skyblue:"87ceeb",slateblue:"6a5acd",slategray:"708090",slategrey:"708090",snow:"fffafa",springgreen:"00ff7f",steelblue:"4682b4",tan:"d2b48c",teal:"008080",thistle:"d8bfd8",tomato:"ff6347",turquoise:"40e0d0",violet:"ee82ee",
+wheat:"f5deb3",white:"fff",whitesmoke:"f5f5f5",yellow:"ff0",yellowgreen:"9acd32"},D=d.hexNames=function(a){var b={},c;for(c in a)a.hasOwnProperty(c)&&(b[a[c]]=c);return b}(p);s=RegExp("rgb[\\s|\\(]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))\\s*\\)?");t=RegExp("rgba[\\s|\\(]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))\\s*\\)?");
+u=RegExp("hsl[\\s|\\(]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))\\s*\\)?");v=RegExp("hsla[\\s|\\(]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))\\s*\\)?");w=RegExp("hsv[\\s|\\(]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))[,|\\s]+((?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?))\\s*\\)?");
+y=/^([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/;x=/^([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/;r.tinycolor=d})(this);
View
146 tinycolor.js
@@ -14,19 +14,18 @@ var trimLeft = /^[\s,#]+/,
function tinycolor (color, opts) {
- // If input is already a tinycolor, return itself
- if (typeof color == "object" && color.hasOwnProperty("_tc_id")) {
- return color;
- }
- // If input is an object, force 1 into "1.0" to handle ratios properly
- // String input requires "1.0" as input, so 1 will be treated as 1
- if (typeof color == "object" && (!opts || !opts.skipRatio)) {
- for (var i in color) {
- if (color[i] === 1) {
- color[i] = "1.0";
- }
- }
+ if (typeof color === "object") {
+ // If input is already a tinycolor, return itself
+ if (-1 < color._tc_id) // avoids NaN, allows 0, smaller than hasOwnProperty
+ return color;
+
+ // If input is an object, force 1 into "1.0" to handle ratios properly
+ // String input requires "1.0" as input, so 1 will be treated as 1
+ if (!(opts && opts.skipRatio)) //!opts || !opts.skipRatio
+ for (var i in color)
+ if (color[i] === 1)
+ color[i] = "1.0";
}
var rgb = inputToRGB(color);
@@ -36,9 +35,9 @@ function tinycolor (color, opts) {
// Potentially lose a little bit of precision here, but will fix issues where
// .5 gets interpreted as half of the total, instead of half of 1
// If it was supposed to be 128, this was already taken care of by `inputToRgb`
- if (r < 1) { r = mathRound(r); }
- if (g < 1) { g = mathRound(g); }
- if (b < 1) { b = mathRound(b); }
+ if (r < 1) r = mathRound(r);
+ if (g < 1) g = mathRound(g);
+ if (b < 1) b = mathRound(b);
return {
ok: rgb.ok,
@@ -51,7 +50,7 @@ function tinycolor (color, opts) {
toHsvString: function() {
var hsv = rgbToHsv(r, g, b);
var h = mathRound(hsv.h * 360), s = mathRound(hsv.s * 100), v = mathRound(hsv.v * 100);
- return (a == 1) ?
+ return a === 1 ?
"hsv(" + h + ", " + s + "%, " + v + "%)" :
"hsva(" + h + ", " + s + "%, " + v + "%, "+ a + ")";
},
@@ -62,7 +61,7 @@ function tinycolor (color, opts) {
toHslString: function() {
var hsl = rgbToHsl(r, g, b);
var h = mathRound(hsl.h * 360), s = mathRound(hsl.s * 100), l = mathRound(hsl.l * 100);
- return (a == 1) ?
+ return a === 1 ?
"hsl(" + h + ", " + s + "%, " + l + "%)" :
"hsla(" + h + ", " + s + "%, " + l + "%, "+ a + ")";
},
@@ -76,7 +75,7 @@ function tinycolor (color, opts) {
return { r: mathRound(r), g: mathRound(g), b: mathRound(b), a: a };
},
toRgbString: function() {
- return (a == 1) ?
+ return a === 1 ?
"rgb(" + mathRound(r) + ", " + mathRound(g) + ", " + mathRound(b) + ")" :
"rgba(" + mathRound(r) + ", " + mathRound(g) + ", " + mathRound(b) + ", " + a + ")";
},
@@ -85,7 +84,7 @@ function tinycolor (color, opts) {
},
toFilter: function() {
var hex = rgbToHex(r, g, b);
- var alphaHex = Math.round(parseFloat(a) * 255).toString(16);
+ var alphaHex = mathRound(parseFloat(a) * 255).toString(16);
return "progid:DXImageTransform.Microsoft.gradient(startColorstr=#" +
alphaHex + hex + ",endColorstr=#" + alphaHex + hex + ")";
}
@@ -112,26 +111,24 @@ function inputToRGB(color) {
var a = 1;
var ok = false;
- if (typeof color == "string") {
+ if (typeof color === "string")
color = stringInputToObject(color);
- }
- if (typeof color == "object") {
- if (color.hasOwnProperty("r") && color.hasOwnProperty("g") && color.hasOwnProperty("b")) {
+
+ if (typeof color === "object") {
+ if (-1 < color.r && -1 < color.g && -1 < color.b) {
rgb = rgbToRgb(color.r, color.g, color.b);
ok = true;
}
- else if (color.hasOwnProperty("h") && color.hasOwnProperty("s") && color.hasOwnProperty("v")) {
+ else if (-1 < color.h && -1 < color.s && -1 < color.v) {
rgb = hsvToRgb(color.h, color.s, color.v);
ok = true;
}
- else if (color.hasOwnProperty("h") && color.hasOwnProperty("s") && color.hasOwnProperty("l")) {
- var rgb = hslToRgb(color.h, color.s, color.l);
+ else if (-1 < color.h && -1 < color.s && -1 <color.l) {
+ rgb = hslToRgb(color.h, color.s, color.l);
ok = true;
}
- if (color.hasOwnProperty("a")) {
- a = color.a;
- }
+ if (-1 < color.a) a = color.a;
}
return {
@@ -177,9 +174,7 @@ function rgbToHsl(r, g, b) {
var max = mathMax(r, g, b), min = mathMin(r, g, b);
var h, s, l = (max + min) / 2;
- if(max == min) {
- h = s = 0; // achromatic
- }
+ if(max === min) h = s = 0; // achromatic
else {
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
@@ -215,9 +210,7 @@ function hslToRgb(h, s, l) {
return p;
}
- if(s == 0) {
- r = g = b = l; // achromatic
- }
+ if(s === 0) r = g = b = l; // achromatic
else {
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
@@ -243,11 +236,9 @@ function rgbToHsv(r, g, b) {
var h, s, v = max;
var d = max - min;
- s = max == 0 ? 0 : d / max;
+ s = max === 0 ? 0 : d / max;
- if(max == min) {
- h = 0; // achromatic
- }
+ if(max === min) h = 0; // achromatic
else {
switch(max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
@@ -264,8 +255,7 @@ function rgbToHsv(r, g, b) {
// *Assumes:* h is contained in [0, 1] or [0, 360] and s and v are contained in [0, 1] or [0, 100]
// *Returns:* { r, g, b } in the set [0, 255]
function hsvToRgb(h, s, v) {
- var r, g, b;
-
+
h = bound01(h, 360) * 6;
s = bound01(s, 100);
v = bound01(v, 100);
@@ -288,9 +278,8 @@ function rgbToHsv(r, g, b) {
// Assumes r, g, and b are contained in the set [0, 255]
// Returns a 3 or 6 character hex
function rgbToHex(r, g, b) {
- function pad(c) {
- return c.length == 1 ? '0' + c : '' + c;
- }
+ function pad(c) { return c.length === 1 ? '0' + c : '' + c; }
+
var hex = [
pad(mathRound(r).toString(16)),
pad(mathRound(g).toString(16)),
@@ -298,17 +287,16 @@ function rgbToHex(r, g, b) {
];
// Return a 3 character hex if possible
- if (hex[0][0] == hex[0][1] && hex[1][0] == hex[1][1] && hex[2][0] == hex[2][1]) {
+ if (hex[0][0] === hex[0][1] && hex[1][0] === hex[1][1] && hex[2][0] === hex[2][1])
return hex[0][0] + hex[1][0] + hex[2][0];
- }
- return hex.join("");
+ return hex.join('');
}
// `equals`
// Can be called with any tinycolor input
tinycolor.equals = function(color1, color2) {
- return tinycolor(color1).toHex() == tinycolor(color2).toHex();
+ return tinycolor(color1).toHex() === tinycolor(color2).toHex();
};
tinycolor.random = function() {
return tinycolor({
@@ -396,9 +384,9 @@ tinycolor.analogous = function(color, results, slices) {
results = results || 6;
slices = slices || 30;
- var hsl = tinycolor(color).toHsl();
- var part = 360 / slices
- var ret = [tinycolor(color)];
+ var hsl = tinycolor(color).toHsl(),
+ part = 360 / slices,
+ ret = [tinycolor(color)];
hsl.h *= 360;
@@ -594,11 +582,9 @@ var hexNames = tinycolor.hexNames = flip(names);
// `{ 'name1': 'val1' }` becomes `{ 'val1': 'name1' }`
function flip(o) {
var flipped = { };
- for (var i in o) {
- if (o.hasOwnProperty(i)) {
+ for (var i in o)
+ if (o.hasOwnProperty(i))
flipped[o[i]] = i;
- }
- }
return flipped;
}
@@ -610,17 +596,13 @@ function bound01(n, max) {
n = mathMin(max, mathMax(0, parseFloat(n)));
// Automatically convert percentage into number
- if (processPercent) {
+ if (processPercent)
n = n * (max / 100);
- }
// Handle floating point rounding errors
- if ((math.abs(n - max) < 0.000001)) {
- return 1;
- }
- else if (n >= 1) {
+ if ((math.abs(n - max) < 0.000001)) return 1;
+ else if (n >= 1)
return (n % max) / parseFloat(max);
- }
return n;
}
@@ -637,12 +619,12 @@ function parseHex(val) {
// Need to handle 1.0 as 100%, since once it is a number, there is no difference between it and 1
// <http://stackoverflow.com/questions/7422072/javascript-how-to-detect-number-as-a-decimal-including-1-0>
function isOnePointZero(n) {
- return typeof n == "string" && n.indexOf('.') != -1 && parseFloat(n) === 1;
+ return typeof n === "string" && n.indexOf('.') !== -1 && parseFloat(n) === 1;
}
// Check to see if string passed in is a percentage
function isPercentage(n) {
- return typeof n === "string" && n.indexOf('%') != -1;
+ return typeof n === "string" && n.indexOf('%') !== -1;
}
var matchers = (function() {
@@ -679,47 +661,43 @@ var matchers = (function() {
function stringInputToObject(color) {
color = color.replace(trimLeft,'').replace(trimRight, '').toLowerCase();
- if (names[color]) {
- color = names[color];
- }
- if (color == 'transparent') {
+ if (names[color]) color = names[color];
+ if (color === 'transparent')
return { r: 0, g: 0, b: 0, a: 0 };
- }
// Try to match string input using regular expressions.
// Keep most of the number bounding out of this function - don't worry about [0,1] or [0,100] or [0,360]
// Just return an object and let the conversion functions handle that.
// This way the result will be the same whether the tinycolor is initialized with string or object.
var match;
- if ((match = matchers.rgb.exec(color))) {
+ if ((match = matchers.rgb.exec(color)))
return { r: match[1], g: match[2], b: match[3] };
- }
- if ((match = matchers.rgba.exec(color))) {
+
+ if ((match = matchers.rgba.exec(color)))
return { r: match[1], g: match[2], b: match[3], a: match[4] };
- }
- if ((match = matchers.hsl.exec(color))) {
+
+ if ((match = matchers.hsl.exec(color)))
return { h: match[1], s: match[2], l: match[3] };
- }
- if ((match = matchers.hsla.exec(color))) {
+
+ if ((match = matchers.hsla.exec(color)))
return { h: match[1], s: match[2], l: match[3], a: match[4] };
- }
- if ((match = matchers.hsv.exec(color))) {
+
+ if ((match = matchers.hsv.exec(color)))
return { h: match[1], s: match[2], v: match[3] };
- }
- if ((match = matchers.hex6.exec(color))) {
+
+ if ((match = matchers.hex6.exec(color)))
return {
r: parseHex(match[1]),
g: parseHex(match[2]),
b: parseHex(match[3])
};
- }
- if ((match = matchers.hex3.exec(color))) {
+
+ if ((match = matchers.hex3.exec(color)))
return {
r: parseHex(match[1] + '' + match[1]),
g: parseHex(match[2] + '' + match[2]),
b: parseHex(match[3] + '' + match[3])
};
- }
return false;
}
@@ -727,4 +705,4 @@ function stringInputToObject(color) {
// Everything is ready, expose to window
window.tinycolor = tinycolor;
-})(this);
+})(this);
Something went wrong with that request. Please try again.