Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #1, better semi-transparent background, various bugfixes and re…

…factoring
  • Loading branch information...
commit 2a63abf4437a5da8c66150283e51b3a915a2f6c4 1 parent d66d4e3
@LeaVerou authored
View
3  .gitignore
@@ -0,0 +1,3 @@
+*.png
+*.zip
+list.txt
View
0  README.md 100644 → 100755
File mode changed
View
151 colors.js 100644 → 100755
@@ -11,59 +11,66 @@ var body = document.body,
canvas.width = canvas.height = 16;
body.appendChild(canvas);
-form.onsubmit = function(e) {
- e && e.preventDefault();
-
- var color = input.value.trim(),
- previous = body.style.backgroundColor;
-
- body.style.backgroundColor = '';
- body.style.backgroundColor = color;
-
- if(!body.style.backgroundColor) {
- body.style.backgroundColor = previous;
- input.className = 'invalid';
- return;
+// Simple class for handling sRGB colors
+function Color(rgba) {
+ if (rgba === 'transparent') {
+ rgba = [0,0,0,0];
}
-
- input.removeAttribute('class');
-
- ctx.fillStyle = color;
- ctx.clearRect(0, 0, 16, 16);
- ctx.fillRect(0, 0, 16, 16);
-
- // Get RGB
- var rgb = [].slice.apply(ctx.getImageData(0, 0, 1, 1).data);
+ else if (typeof rgba === 'string') {
+ var rgbaString = rgba;
+ rgba = rgbaString.match(/rgba?\(([\d.]+), ([\d.]+), ([\d.]+)(?:, ([\d.]+))?\)/);
- if(rgb[3] === 255) {
- rgb.length = 3;
- }
-
- var hex = rgb.map(function(a) {
- return (a < 16? '0' : '') + a.toString(16);
- }).join('');
-
- if(rgb.length === 3) {
- if(/(([\da-z])\2){3}/i.test(hex)) {
- hex = hex.charAt(0) + hex.charAt(2) + hex.charAt(4);
+ if (rgba) {
+ rgba.shift();
+ }
+ else {
+ throw new Error('Invalid string: ' + rgbaString);
}
}
- out[1].value = '#' + hex;
-
- if(rgb.length > 3) {
- rgb[3] = ('' + Math.round(rgb[3] / 2.55) / 100).replace('0.', '.');
+ if (rgba[3] === undefined) {
+ rgba[3] = 1;
}
- out[0].value = 'rgb' + (rgb.length > 3? 'a' : '') + '(' + rgb.join(', ') + ')';
+ rgba = rgba.map(function (a) { return Math.round(a * 100)/100 });
+ this.rgba = rgba;
+}
+
+Color.prototype = {
+ get rgb () {
+ return this.rgba.slice(0,3);
+ },
+
+ get alpha () {
+ return this.rgba[3];
+ },
+
+ set alpha (alpha) {
+ this.rgba[3] = alpha;
+ },
+
+ get hex () {
+ var hex = this.rgb.map(function(a) {
+ return (a < 16? '0' : '') + a.toString(16);
+ }).join('')
+
+ if(this.alpha >= 1) {
+ if(/(([\da-z])\2){3}/i.test(hex)) {
+ hex = hex.charAt(0) + hex.charAt(2) + hex.charAt(4);
+ }
+ }
+ else {
+ var alpha255 = Math.round(this.alpha * 255);
+
+ hex += (alpha255 < 16? '0' : '') + alpha255.toString(16);
+ }
+
+ return '#' + hex;
+ },
- // Now it's HSL's turn
- if(color.indexOf('hsl') === 0) {
- out[2].value = color.replace('0.', '.');
- }
- else {
- rgb[0] /= 2.55; rgb[1] /= 2.55; rgb[2] /= 2.55;
+ get hsl () {
+ var rgb = this.rgb.map(function(a) { return a / 2.55 });
var hsl = [],
max = Math.max.apply(Math, rgb),
@@ -83,19 +90,65 @@ form.onsubmit = function(e) {
}
hsl[0] = Math.round(hsl[0]*60);
- hsl[2] += '%';
}
else {
hsl[0] = 0;
hsl[1] = '0%';
}
- if(rgb.length > 3) {
- hsl[3] = rgb[3];
- }
+ hsl[2] += '%';
+
+ return hsl;
+ },
+
+ toString: function() {
+ return 'rgb' + (this.alpha < 1? 'a' : '') + '(' + (this.rgba.slice(0, this.alpha >= 1? 3 : 4).join(', ') + ')').replace(/\b0\./, '.');
+ },
+
+ toHSLString: function() {
+ var hsl = this.hsl;
- out[2].value = 'hsl' + (hsl.length > 3? 'a' : '') + '(' + hsl.join(', ') + ')';
+ return 'hsl' + (this.alpha < 1? 'a' : '') + '(' + hsl.join(', ') + ((this.alpha < 1? ', ' + this.alpha : '') + ')').replace(/\b0\./, '.');
+ },
+
+ clone: function() {
+ return new Color(this.rgba);
}
+}
+
+form.onsubmit = function(e) {
+ e && e.preventDefault();
+
+ var oldColor = getComputedStyle(document.body).backgroundColor;
+
+ document.body.style.background = '';
+ document.body.style.background = input.value;
+
+ var newColor = document.body.style.background;
+
+ if (!newColor) {
+ // Invalid color
+ document.body.style.background = oldColor;
+ input.className = 'invalid';
+
+ return;
+ }
+
+ newColor = getComputedStyle(document.body).backgroundColor;
+
+ input.removeAttribute('class');
+
+ var color = new Color(newColor);
+
+ out[1].value = color.hex;
+
+ out[0].value = color + '';
+
+ out[2].value = color.toHSLString();
+
+ ctx.fillStyle = color;
+ ctx.clearRect(0, 0, 16, 16);
+ ctx.fillRect(0, 0, 16, 16);
$('link[rel="shortcut icon"]').setAttribute('href', canvas.toDataURL());
document.title = color + ' ✿ CSS.coloratum';
View
1  index.html 100644 → 100755
@@ -5,6 +5,7 @@
<meta charset="utf-8" />
<title>CSS.coloratum: Convert and share CSS colors</title>
<link rel="stylesheet" href="style.css" />
+<script src="prefixfree.min.js"></script>
<link rel="shortcut icon" type="image/png" href="about:blank" />
<meta name="keywords" content="RGB, HSL, RGBA, HSLA, hex, color keywords, convert, conversion" />
</head>
View
5 prefixfree.min.js
@@ -0,0 +1,5 @@
+/**
+ * StyleFix 1.0.3 & PrefixFree 1.0.7
+ * @author Lea Verou
+ * MIT license
+ */(function(){function t(e,t){return[].slice.call((t||document).querySelectorAll(e))}if(!window.addEventListener)return;var e=window.StyleFix={link:function(t){try{if(t.rel!=="stylesheet"||t.hasAttribute("data-noprefix"))return}catch(n){return}var r=t.href||t.getAttribute("data-href"),i=r.replace(/[^\/]+$/,""),s=t.parentNode,o=new XMLHttpRequest,u;o.onreadystatechange=function(){o.readyState===4&&u()};u=function(){var n=o.responseText;if(n&&t.parentNode&&(!o.status||o.status<400||o.status>600)){n=e.fix(n,!0,t);if(i){n=n.replace(/url\(\s*?((?:"|')?)(.+?)\1\s*?\)/gi,function(e,t,n){return/^([a-z]{3,10}:|\/|#)/i.test(n)?e:'url("'+i+n+'")'});var r=i.replace(/([\\\^\$*+[\]?{}.=!:(|)])/g,"\\$1");n=n.replace(RegExp("\\b(behavior:\\s*?url\\('?\"?)"+r,"gi"),"$1")}var u=document.createElement("style");u.textContent=n;u.media=t.media;u.disabled=t.disabled;u.setAttribute("data-href",t.getAttribute("href"));s.insertBefore(u,t);s.removeChild(t);u.media=t.media}};try{o.open("GET",r);o.send(null)}catch(n){if(typeof XDomainRequest!="undefined"){o=new XDomainRequest;o.onerror=o.onprogress=function(){};o.onload=u;o.open("GET",r);o.send(null)}}t.setAttribute("data-inprogress","")},styleElement:function(t){if(t.hasAttribute("data-noprefix"))return;var n=t.disabled;t.textContent=e.fix(t.textContent,!0,t);t.disabled=n},styleAttribute:function(t){var n=t.getAttribute("style");n=e.fix(n,!1,t);t.setAttribute("style",n)},process:function(){t('link[rel="stylesheet"]:not([data-inprogress])').forEach(StyleFix.link);t("style").forEach(StyleFix.styleElement);t("[style]").forEach(StyleFix.styleAttribute)},register:function(t,n){(e.fixers=e.fixers||[]).splice(n===undefined?e.fixers.length:n,0,t)},fix:function(t,n,r){for(var i=0;i<e.fixers.length;i++)t=e.fixers[i](t,n,r)||t;return t},camelCase:function(e){return e.replace(/-([a-z])/g,function(e,t){return t.toUpperCase()}).replace("-","")},deCamelCase:function(e){return e.replace(/[A-Z]/g,function(e){return"-"+e.toLowerCase()})}};(function(){setTimeout(function(){t('link[rel="stylesheet"]').forEach(StyleFix.link)},10);document.addEventListener("DOMContentLoaded",StyleFix.process,!1)})()})();(function(e){function t(e,t,r,i,s){e=n[e];if(e.length){var o=RegExp(t+"("+e.join("|")+")"+r,"gi");s=s.replace(o,i)}return s}if(!window.StyleFix||!window.getComputedStyle)return;var n=window.PrefixFree={prefixCSS:function(e,r,i){var s=n.prefix;n.functions.indexOf("linear-gradient")>-1&&(e=e.replace(/(\s|:|,)(repeating-)?linear-gradient\(\s*(-?\d*\.?\d*)deg/ig,function(e,t,n,r){return t+(n||"")+"linear-gradient("+(90-r)+"deg"}));e=t("functions","(\\s|:|,)","\\s*\\(","$1"+s+"$2(",e);e=t("keywords","(\\s|:)","(\\s|;|\\}|$)","$1"+s+"$2$3",e);e=t("properties","(^|\\{|\\s|;)","\\s*:","$1"+s+"$2:",e);if(n.properties.length){var o=RegExp("\\b("+n.properties.join("|")+")(?!:)","gi");e=t("valueProperties","\\b",":(.+?);",function(e){return e.replace(o,s+"$1")},e)}if(r){e=t("selectors","","\\b",n.prefixSelector,e);e=t("atrules","@","\\b","@"+s+"$1",e)}e=e.replace(RegExp("-"+s,"g"),"-");e=e.replace(/-\*-(?=[a-z]+)/gi,n.prefix);return e},property:function(e){return(n.properties.indexOf(e)?n.prefix:"")+e},value:function(e,r){e=t("functions","(^|\\s|,)","\\s*\\(","$1"+n.prefix+"$2(",e);e=t("keywords","(^|\\s)","(\\s|$)","$1"+n.prefix+"$2$3",e);return e},prefixSelector:function(e){return e.replace(/^:{1,2}/,function(e){return e+n.prefix})},prefixProperty:function(e,t){var r=n.prefix+e;return t?StyleFix.camelCase(r):r}};(function(){var e={},t=[],r={},i=getComputedStyle(document.documentElement,null),s=document.createElement("div").style,o=function(n){if(n.charAt(0)==="-"){t.push(n);var r=n.split("-"),i=r[1];e[i]=++e[i]||1;while(r.length>3){r.pop();var s=r.join("-");u(s)&&t.indexOf(s)===-1&&t.push(s)}}},u=function(e){return StyleFix.camelCase(e)in s};if(i.length>0)for(var a=0;a<i.length;a++)o(i[a]);else for(var f in i)o(StyleFix.deCamelCase(f));var l={uses:0};for(var c in e){var h=e[c];l.uses<h&&(l={prefix:c,uses:h})}n.prefix="-"+l.prefix+"-";n.Prefix=StyleFix.camelCase(n.prefix);n.properties=[];for(var a=0;a<t.length;a++){var f=t[a];if(f.indexOf(n.prefix)===0){var p=f.slice(n.prefix.length);u(p)||n.properties.push(p)}}n.Prefix=="Ms"&&!("transform"in s)&&!("MsTransform"in s)&&"msTransform"in s&&n.properties.push("transform","transform-origin");n.properties.sort()})();(function(){function i(e,t){r[t]="";r[t]=e;return!!r[t]}var e={"linear-gradient":{property:"backgroundImage",params:"red, teal"},calc:{property:"width",params:"1px + 5%"},element:{property:"backgroundImage",params:"#foo"},"cross-fade":{property:"backgroundImage",params:"url(a.png), url(b.png), 50%"}};e["repeating-linear-gradient"]=e["repeating-radial-gradient"]=e["radial-gradient"]=e["linear-gradient"];var t={initial:"color","zoom-in":"cursor","zoom-out":"cursor",box:"display",flexbox:"display","inline-flexbox":"display",flex:"display","inline-flex":"display"};n.functions=[];n.keywords=[];var r=document.createElement("div").style;for(var s in e){var o=e[s],u=o.property,a=s+"("+o.params+")";!i(a,u)&&i(n.prefix+a,u)&&n.functions.push(s)}for(var f in t){var u=t[f];!i(f,u)&&i(n.prefix+f,u)&&n.keywords.push(f)}})();(function(){function s(e){i.textContent=e+"{}";return!!i.sheet.cssRules.length}var t={":read-only":null,":read-write":null,":any-link":null,"::selection":null},r={keyframes:"name",viewport:null,document:'regexp(".")'};n.selectors=[];n.atrules=[];var i=e.appendChild(document.createElement("style"));for(var o in t){var u=o+(t[o]?"("+t[o]+")":"");!s(u)&&s(n.prefixSelector(u))&&n.selectors.push(o)}for(var a in r){var u=a+" "+(r[a]||"");!s("@"+u)&&s("@"+n.prefix+u)&&n.atrules.push(a)}e.removeChild(i)})();n.valueProperties=["transition","transition-property"];e.className+=" "+n.prefix;StyleFix.register(n.prefixCSS)})(document.documentElement);
View
14 style.css 100644 → 100755
@@ -4,17 +4,14 @@
html {
background: white;
- background: -moz-repeating-linear-gradient(-45deg, white, white 50px, black 50px, black 100px);
- background: -ms-repeating-linear-gradient(-45deg, white, white 50px, black 50px, black 100px);
- background: -o-repeating-linear-gradient(-45deg, white, white 50px, black 50px, black 100px);
- background: -webkit-repeating-linear-gradient(-45deg, white, white 50px, black 50px, black 100px);
- background: repeating-linear-gradient(-45deg, white, white 50px, black 50px, black 100px);
+ background: linear-gradient(45deg, #bbb 25%, transparent 25%, transparent 75%, #bbb 75%, #bbb) 5px 5px,
+ linear-gradient(45deg, #bbb 25%, #eee 25%, #eee 75%, #bbb 75%, #bbb) 15px 15px;
+ background-size:20px 20px;
height: 100%;
}
body {
min-height: 100%;
- background: slategray;
color: rgba(0,0,0,.6);
font-family: 'Palatino Linotype', Palatino, Georgia, serif;
font-size: 110%;
@@ -26,10 +23,6 @@ form {
margin: auto;
padding: 1em 1.2em 1.2em;
background: rgba(0,0,0,.6);
- background-image: -moz-linear-gradient(transparent, rgba(0,0,0,.5));
- background-image: -ms-linear-gradient(transparent, rgba(0,0,0,.5));
- background-image: -o-linear-gradient(transparent, rgba(0,0,0,.5));
- background-image: -webkit-linear-gradient(transparent, rgba(0,0,0,.5));
background-image: linear-gradient(transparent, rgba(0,0,0,.5));
color: white;
font-size: 90%;
@@ -41,7 +34,6 @@ input {
display: block;
width: 100%;
padding: .4em 0 .2em;
- -moz-box-sizing: border-box;
box-sizing: border-box;
font-family: Consolas, 'Andale Mono', 'Lucida Console', monospace;
font-weight: bold;
Please sign in to comment.
Something went wrong with that request. Please try again.