Skip to content

Instantly share code, notes, and snippets.

@hagb4rd
Last active Aug 1, 2016
Embed
What would you like to do?

colorzilla-utils.js

  • copyrights reserved by ColorZilla *
//
// This file is part of ColorZilla
//
// Written by Alex Sirota (alex @ iosart.com)
//
// Copyright (c) iosart labs llc 2011, All Rights Reserved
//
var cz = exports;

cz.HSVTriple = function(h, s, v) {
	this.h = h;
	this.s = s;
	this.v = v;

	this.toString = function () {
		return "(" + this.h + ", " + this.s + ", " + this.v + ")";
	}
}

cz.HSLTriple = function(h, s, l) {
	this.h = h;
	this.s = s;
	this.l = l;

	this.toString = function () {
		return "(" + this.h + ", " + this.s + ", " + this.l + ")";
	}
}

cz.RGBTriple = function(r, g, b) {
	this.r = r;
	this.g = g;
	this.b = b;

	this.toString = function () {
		return "(" + this.r + ", " + this.g + ", " + this.b + ")";
	}
}

cz.LABTriple = function(l, a, b) {
	this.l = l;
	this.a = a;
	this.b = b;

	this.toString = function () {
		return "(" + this.l + ", " + this.a + ", " + this.b + ")";
	}
}

cz.XYZTriple = function(x, y, z) {
	this.x = x;
	this.y = y;
	this.z = z;

	this.toString = function () {
		return "(" + this.x + ", " + this.y + ", " + this.z + ")";
	}
}

cz.CMYKQuadruple = function(c, m, y, k) {
	this.c = c;
	this.m = m;
	this.y = y;
	this.k = k;

	this.toString = function () {
		return "(" + this.c + ", " + this.m + ", " + this.y + ", " + this.k + ")";
	}
}

// RGB -> COLORREF
cz.RGBToColor = function(r, g, b) {
	return r | (g << 8) | (b << 16);
}

// COLORREF -> R/G/B:
cz.GetRValue = function(color) {
	return color & 0xff;
}

cz.GetGValue = function(color) {
	return (color >> 8) & 0xff;
}

cz.GetBValue = function(color) {
	return (color >> 16) & 0xff;
}

// 0..255 -> 0% - 100%
cz.IntToPercent = function(val) {
	return Math.floor(((val * 100) / 255) + 0.5);
}

// 0..255 -> 0-360
cz.IntToDegrees = function(val) {
	return Math.floor(((val * 360) / 255) + 0.5);
}

// 0..255 -> 00 - ff 
cz.DecimalToHexa = function(val) {
	var hexStr = val.toString(16);
	if (hexStr.length < 2) {
		hexStr = "0" + hexStr;
	}
	return hexStr;
}

// COLORREF -> rgb(100%, 100%, 100%)
cz.ColToRGBPercentageAttribute = function(col) {
 	var colStr = cz.IntToPercent(cz.GetRValue(col)) + "%, " +
			cz.IntToPercent(cz.GetGValue(col)) + "%, " +
			cz.IntToPercent(cz.GetBValue(col)) + "%";
	return "rgb(" + colStr + ")";
}

// COLORREF -> hsl(240, 100%, 100%)
cz.ColToHSLAttribute = function(col) {
        var hsl = cz.RGBToHSL(cz.GetRValue(col), cz.GetGValue(col), cz.GetBValue(col));

 	var colStr =     hsl.h + ", " +
                     cz.IntToPercent(hsl.s) + "%, " +
                     cz.IntToPercent(hsl.l) + "%";
	return "hsl(" + colStr + ")";
}

// COLORREF -> rgb(255, 255, 255)
cz.ColToRGBAttribute = function(col) {
 	var colStr = cz.GetRValue(col) + ", " + cz.GetGValue(col) + ", " + cz.GetBValue(col);
	return "rgb(" + colStr + ")";
}

// rgb(255, 255, 255) -> COLORREF
cz.RGBAttributeToCol = function(colAttribute) {
	var firstParen = colAttribute.split("(");
	colAttribute = firstParen[1];
	var secondParen = colAttribute.split(")");
	colAttribute = secondParen[0];
	var rgbArr = colAttribute.split(",");
	return cz.RGBToColor(rgbArr[0], rgbArr[1], rgbArr[2]);
}


// COLORREF -> #ffffff
cz.ColToRGBHexaAttribute = function(col) {
 	var colStr = cz.DecimalToHexa(cz.GetRValue(col)) +
			cz.DecimalToHexa(cz.GetGValue(col)) +
			cz.DecimalToHexa(cz.GetBValue(col));
	if (!cz.gbCZLowerCaseHexa) {
		colStr = colStr.toUpperCase();
	}
	return "#" + colStr;
}

// #ffffff -> COLORREF
// assumev valid string
cz.RGBHexaAttributeToCol = function(col) {
	var red   = col.substr(1, 2);
	var green = col.substr(3, 2);
	var blue  = col.substr(5, 2);

	red   = parseInt(red, 16);
	green = parseInt(green, 16);
	blue  = parseInt(blue, 16);

	return cz.RGBToColor(red, green, blue);
}

cz.RGBToGrayscale = function(r, g, b) {
	return (r * 0.30) + (g * 0.59) + (b * 0.11);
}

// COLORREF -> rgb/rgb-perc/hsl/hex-no-hash/hex
cz.ColToSpecificColorFormat = function(colorRef, colorFormat) {
   var colorStr;
   switch (colorFormat) {
       case 'rgb':
           colorStr = cz.ColToRGBAttribute(colorRef);
           break;
       case 'rgb-perc':
           colorStr = cz.ColToRGBPercentageAttribute(colorRef);
           break;
       case 'hsl':
           colorStr = cz.ColToHSLAttribute(colorRef);
           break;
      case 'hex-no-hash':
           colorStr = cz.ColToRGBHexaAttribute(colorRef);
           colorStr = colorStr.substring(1);
           break;
       case 'hex':
           colorStr = cz.ColToRGBHexaAttribute(colorRef);
           break;
       default:
           colorStr = cz.ColToRGBHexaAttribute(colorRef);
   }
   return colorStr;
}

cz.RGBToHSV = function(r, g, b) {
	var hue, sat, val;
	var max, dif;

	max = Math.max(r, g, b);
	dif = max - Math.min(r, g, b);
	sat = (max == 0) ? 0 : (255 * dif/max);

	if (sat == 0) {
		hue=0;
	} else if (r == max) {
		hue = 60.0 * (g - b) / dif;
	} else if (g == max) {
		hue = 120.0 + 60.0*(b - r) / dif;
	} else if (b == max) {
		hue = 240.0 + 60.0*(r - g) / dif;
	}

	if (hue < 0.0) {
		hue += 360.0;
	}
	hue = Math.round((hue * 255.0) / 360.0);
	sat = Math.round(sat);

	val = max;
	return new cz.HSVTriple(hue, sat, val)
}


cz.RGBToHSL = function(r, g, b) {
    r /= 255;
    g /= 255; 
    b /= 255;
    
    var max = Math.max(r, g, b);
    var min = Math.min(r, g, b);

    var l = (max + min) / 2;

    var h, s;
 
    if (max == min) {
         h = s = 0; 
    } else{
         var delta = max - min;
         s = delta / ((l > 0.5) ? (2 - max - min) : (max + min));
         switch (max) {
             case r: h = (g - b) / delta + (g < b ? 6 : 0); break;
             case g: h = (b - r) / delta + 2; break;
             case b: h = (r - g) / delta + 4; break;
         }
         h /= 6;
    }

    h = Math.round(h * 360);
	s = Math.round(s * 255);
	l = Math.round(l * 255);
 
    return new cz.HSLTriple(h, s, l);
}

// http://en.wikipedia.org/wiki/SRGB_color_space
cz.RGBToXYZ = function(r, g, b) {
    r /= 255;
    g /= 255; 
    b /= 255;

    function norm(x) {
        return (x > 0.04045) ? Math.pow( ((x + 0.055) / 1.055), 2.4) : (x / 12.92);
    }

    r = norm(r);
    g = norm(g);
    b = norm(b);
    
    var x = 0.4124*r  + 0.3576*g  + 0.1805*b;
    var y = 0.2126*r  + 0.7152*g  + 0.0722*b;
    var z = 0.0193*r  + 0.1192*g  + 0.9505*b;

    x *= 100;
	y *= 100;
	z *= 100;
 
    return new cz.XYZTriple(x, y, z);
}

cz.XYZToRGB = function(x, y, z) {
    x /= 100;
    y /= 100; 
    z /= 100;

    function norm(x) {
        return (x > 0.0031308) ? 1.055*Math.pow(x, 1/2.4) - 0.055 : 12.92*x;
    }

    
    var r =  3.2406*x - 1.5372*y - 0.4986*z;
    var g = -0.9689*x + 1.8758*y + 0.0415*z;
    var b =  0.0557*x - 0.2040*y + 1.0570*z;

    r = norm(r);
    g = norm(g);
    b = norm(b);
 

    function bounds(x) {
        if (x < 0) return 0;
        if (x > 255) return 255;
        return x;
    }

    r = bounds(Math.round(r*255));
	g = bounds(Math.round(g*255));
	b = bounds(Math.round(b*255));
 
    return new cz.RGBTriple(r, g, b);
}

cz.XYZToLAB = function(x, y, z) {
    function func(t) {
        const thresh = 0.00885645; // (6/29)^3
        return (t > thresh) ? Math.pow(t, 0.3333333333) : 7.787037037037*t + 0.13793103448; // (1/3)*(29/6)^2*t + 4/29
    }

    // observer = 2A? 
    // illuminant = D50
    var xn = 96.422;
    var yn = 100;
    var zn = 82.521;

    var fx = func(x/xn);
    var fy = func(y/yn);
    var fz = func(z/zn);

    var l = 116*fy - 16;
    var a = 500*(fx - fy);
    var b = 200*(fy - fz);

    return new cz.LABTriple(l, a, b);
}

cz.LABToXYZ = function(l, a, b) {
    function func(t) {
        const thresh = 6/29;
        return (t > thresh) ? Math.pow(t, 3) : (t - 0.137931)*0.128418549;
    }

    var fy = (l + 16) / 116;
    var fx = fy + a/500;
    var fz = fy - b/200;

    var x = func(fx);
    var y = func(fy);
    var z = func(fz);

    // observer = 2A? 
    // illuminant = D50
    var xn = 96.422;
    var yn = 100;
    var zn = 82.521;

    x *= xn; 
    y *= yn; 
    z *= zn; 

    return new cz.XYZTriple(x, y, z);
}

cz.RGBToLAB = function(r, g, b) {
    var col = cz.RGBToXYZ(r, g, b);
    return cz.XYZToLAB(col.x, col.y, col.z);
}

cz.LABToRGB = function(l, a, b) {
    var col = cz.LABToXYZ(parseFloat(l), parseFloat(a), parseFloat(b));
    return cz.XYZToRGB(col.x, col.y, col.z);
}

cz.HSVToRGB = function(hue, sat, val) {
	var r, g, b;
	var i, f, p, q, t;

	if (sat == 0) {
		r = g = b = val;
	} else {
		hue = ((hue * 359) / (255*60));
		sat /= 255;
		val /= 255;

		i = Math.floor(hue);
		f = hue - i;
		p = val * (1 - sat);
		q = val * (1 - (sat * f));
		t = val * (1 - sat * (1 - f));

		switch (i) {
			case 0 : r = val; g = t;   b = p;   break;
			case 1 : r = q;   g = val; b = p;   break;
			case 2 : r = p;   g = val; b = t;   break;
			case 3 : r = p;   g = q;   b = val; break;
			case 4 : r = t;   g = p;   b = val; break;
			default: r = val; g = p;   b = q;
		}
		r = Math.round(r * 255);
		g = Math.round(g * 255);
		b = Math.round(b * 255);
	}
	return new cz.RGBTriple(r, g, b);
}

cz.RGBToCMYK = function(r, g, b) {
    var c = 255 - r;
    var m = 255 - g;
    var y = 255 - b;
    var k = Math.min(c, Math.min(m, y));

    if (k == 255) { 
        c = m = y = 0;
    } else {
        c = Math.round(255*((c - k) / (255 - k)));
        m = Math.round(255*((m - k) / (255 - k)));
        y = Math.round(255*((y - k) / (255 - k)));
    } 
    return new cz.CMYKQuadruple(c, m, y, k);
}

cz.CMYKToRGB = function(c, m, y, k) {
    c /= 255; m /= 255; y /= 255; k /= 255;

    c = c*(1-k) + k;
    m = m*(1-k) + k;
    y = y*(1-k) + k;

    r = Math.round((1-c)*255);
    g = Math.round((1-m)*255);
    b = Math.round((1-y)*255);

    return new cz.RGBTriple(r, g, b);
}


// this is actually a "value" component of HSV:
cz.GetColorLightness = function(color) {
	var r = cz.GetRValue(color);
	var g = cz.GetGValue(color);
	var b = cz.GetBValue(color);
	return Math.max(r, g, b);
}

// #ff0000 -> red
cz.HexaAttributeToPredefinedColor = function(hexCol) {
	hexCol = hexCol.toLowerCase();
  	if (hexCol == "#800000") { return "maroon"; }
  	if (hexCol == "#ff0000") { return "red"; }
  	if (hexCol == "#ffa500") { return "orange"; }
  	if (hexCol == "#ffff00") { return "yellow"; }
  	if (hexCol == "#808000") { return "olive"; }
  	if (hexCol == "#800080") { return "purple"; }
  	if (hexCol == "#ff00ff") { return "fuchsia"; }
  	if (hexCol == "#ffffff") { return "white"; }
  	if (hexCol == "#00ff00") { return "lime"; }
  	if (hexCol == "#008000") { return "green"; }
  	if (hexCol == "#000080") { return "navy"; }
  	if (hexCol == "#0000ff") { return "blue"; }
  	if (hexCol == "#00ffff") { return "aqua"; }
  	if (hexCol == "#008080") { return "teal"; }
  	if (hexCol == "#000000") { return "black"; }
  	if (hexCol == "#c0c0c0") { return "silver"; }
  	if (hexCol == "#808080") { return "gray"; }
	return null;
}

cz.FixHexValue = function(val) {
	if ((val.length != 7) || (val.substr(0, 1) != "#")) {
		return "#000000";
	}
	return val;
}

cz.FixByteValue = function(val) {
	if (val > 255) {
		val = 255;
	} else {
		if (val < 0) {
			val = 0;
		}
	}

	return val;
}

cz.Fix100Value = function(val) {
	if (val > 100) {
		val = 100;
	} else {
		if (val < 0) {
			val = 0;
		}
	}

	return val;
}

cz.FixLabABValue = function(val) {
  	if (val > 127) {
		val = 127;
	} else {
		if (val < -128) {
			val = -128;
		}
	}

	return val;
}

cz.ValidateByteValue = function(val) {
	if ((val < 0) || (val > 255)) {
		return false;
	}
	return true;
}

cz.CompareTwoStrings = function(a, b) {
	if ( a < b ) return -1;
        if ( a > b ) return 1;
        return 0;
}

cz.ClipString = function(text, length) {
    if (typeof length == 'undefined') {
        length = 15;
    }

    if (!text) return text;

    var clippedText; 
    if (text.length > length) {
        clippedText = text.substr(0,length) + "...";
    } else {
        clippedText = text;
    }
    return clippedText;
}

cz.GetColorPalettePermalink = function(colors, originUrl, name)  {
    // we are will try to keep the URL length under 2000 chars
    var colorsHex = [];
    
    for (var i=0; i < colors.length; i++) {
        var color = colors[i];
        var colorHex = cz.ColToRGBHexaAttribute(color).substring(1);
        colorsHex.push(colorHex);
    }

    if (colorsHex.length > 256) {
        // no more than 256 colors (URL length limitations)
        colorsHex = colorsHex.slice(0, 255);       
    }
 
    colors = colorsHex.join('+');

    var prefix = 'http://colorzilla.com/colors';
    var colorzillaPaletteURL = prefix + '/' + colorsHex.join('+');
    if (name) { 
        if (name.length > 64) {
            // clip the name if too long
            name = name.substr(0, 64);
        }
        colorzillaPaletteURL += '/' + encodeURIComponent(name);
    }

    var url = null;
    if (originUrl) {
        url = originUrl;
        url = url.replace(/^https?:\/\//, '');
        url = url.replace(/\?.*$/, '');
        url = url.replace(/#.*$/, '');
        url = encodeURIComponent(url);
    }

    if (url) {
          if ((colorzillaPaletteURL.length + url.length) < 1900) {
              colorzillaPaletteURL += '?source-url=' + url;
          }
    }

    return colorzillaPaletteURL;
}

cz.gbCZLowerCaseHexa = false;
//
// This file is part of ColorZilla
//
// Written by Alex Sirota (alex @ iosart.com)
//
// Copyright (c) iosart labs llc 2011, All Rights Reserved
//
var cz = exports||{};
cz.HSVTriple = function(h, s, v) {
this.h = h;
this.s = s;
this.v = v;
this.toString = function () {
return "(" + this.h + ", " + this.s + ", " + this.v + ")";
}
}
cz.HSLTriple = function(h, s, l) {
this.h = h;
this.s = s;
this.l = l;
this.toString = function () {
return "(" + this.h + ", " + this.s + ", " + this.l + ")";
}
}
cz.RGBTriple = function(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
this.toString = function () {
return "(" + this.r + ", " + this.g + ", " + this.b + ")";
}
}
cz.LABTriple = function(l, a, b) {
this.l = l;
this.a = a;
this.b = b;
this.toString = function () {
return "(" + this.l + ", " + this.a + ", " + this.b + ")";
}
}
cz.XYZTriple = function(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
this.toString = function () {
return "(" + this.x + ", " + this.y + ", " + this.z + ")";
}
}
cz.CMYKQuadruple = function(c, m, y, k) {
this.c = c;
this.m = m;
this.y = y;
this.k = k;
this.toString = function () {
return "(" + this.c + ", " + this.m + ", " + this.y + ", " + this.k + ")";
}
}
// RGB -> COLORREF
cz.RGBToColor = function(r, g, b) {
return r | (g << 8) | (b << 16);
}
// COLORREF -> R/G/B:
cz.GetRValue = function(color) {
return color & 0xff;
}
cz.GetGValue = function(color) {
return (color >> 8) & 0xff;
}
cz.GetBValue = function(color) {
return (color >> 16) & 0xff;
}
// 0..255 -> 0% - 100%
cz.IntToPercent = function(val) {
return Math.floor(((val * 100) / 255) + 0.5);
}
// 0..255 -> 0-360
cz.IntToDegrees = function(val) {
return Math.floor(((val * 360) / 255) + 0.5);
}
// 0..255 -> 00 - ff
cz.DecimalToHexa = function(val) {
var hexStr = val.toString(16);
if (hexStr.length < 2) {
hexStr = "0" + hexStr;
}
return hexStr;
}
// COLORREF -> rgb(100%, 100%, 100%)
cz.ColToRGBPercentageAttribute = function(col) {
var colStr = cz.IntToPercent(cz.GetRValue(col)) + "%, " +
cz.IntToPercent(cz.GetGValue(col)) + "%, " +
cz.IntToPercent(cz.GetBValue(col)) + "%";
return "rgb(" + colStr + ")";
}
// COLORREF -> hsl(240, 100%, 100%)
cz.ColToHSLAttribute = function(col) {
var hsl = cz.RGBToHSL(cz.GetRValue(col), cz.GetGValue(col), cz.GetBValue(col));
var colStr = hsl.h + ", " +
cz.IntToPercent(hsl.s) + "%, " +
cz.IntToPercent(hsl.l) + "%";
return "hsl(" + colStr + ")";
}
// COLORREF -> rgb(255, 255, 255)
cz.ColToRGBAttribute = function(col) {
var colStr = cz.GetRValue(col) + ", " + cz.GetGValue(col) + ", " + cz.GetBValue(col);
return "rgb(" + colStr + ")";
}
// rgb(255, 255, 255) -> COLORREF
cz.RGBAttributeToCol = function(colAttribute) {
var firstParen = colAttribute.split("(");
colAttribute = firstParen[1];
var secondParen = colAttribute.split(")");
colAttribute = secondParen[0];
var rgbArr = colAttribute.split(",");
return cz.RGBToColor(rgbArr[0], rgbArr[1], rgbArr[2]);
}
// COLORREF -> #ffffff
cz.ColToRGBHexaAttribute = function(col) {
var colStr = cz.DecimalToHexa(cz.GetRValue(col)) +
cz.DecimalToHexa(cz.GetGValue(col)) +
cz.DecimalToHexa(cz.GetBValue(col));
if (!cz.gbCZLowerCaseHexa) {
colStr = colStr.toUpperCase();
}
return "#" + colStr;
}
// #ffffff -> COLORREF
// assumev valid string
cz.RGBHexaAttributeToCol = function(col) {
var red = col.substr(1, 2);
var green = col.substr(3, 2);
var blue = col.substr(5, 2);
red = parseInt(red, 16);
green = parseInt(green, 16);
blue = parseInt(blue, 16);
return cz.RGBToColor(red, green, blue);
}
cz.RGBToGrayscale = function(r, g, b) {
return (r * 0.30) + (g * 0.59) + (b * 0.11);
}
// COLORREF -> rgb/rgb-perc/hsl/hex-no-hash/hex
cz.ColToSpecificColorFormat = function(colorRef, colorFormat) {
var colorStr;
switch (colorFormat) {
case 'rgb':
colorStr = cz.ColToRGBAttribute(colorRef);
break;
case 'rgb-perc':
colorStr = cz.ColToRGBPercentageAttribute(colorRef);
break;
case 'hsl':
colorStr = cz.ColToHSLAttribute(colorRef);
break;
case 'hex-no-hash':
colorStr = cz.ColToRGBHexaAttribute(colorRef);
colorStr = colorStr.substring(1);
break;
case 'hex':
colorStr = cz.ColToRGBHexaAttribute(colorRef);
break;
default:
colorStr = cz.ColToRGBHexaAttribute(colorRef);
}
return colorStr;
}
cz.RGBToHSV = function(r, g, b) {
var hue, sat, val;
var max, dif;
max = Math.max(r, g, b);
dif = max - Math.min(r, g, b);
sat = (max == 0) ? 0 : (255 * dif/max);
if (sat == 0) {
hue=0;
} else if (r == max) {
hue = 60.0 * (g - b) / dif;
} else if (g == max) {
hue = 120.0 + 60.0*(b - r) / dif;
} else if (b == max) {
hue = 240.0 + 60.0*(r - g) / dif;
}
if (hue < 0.0) {
hue += 360.0;
}
hue = Math.round((hue * 255.0) / 360.0);
sat = Math.round(sat);
val = max;
return new cz.HSVTriple(hue, sat, val)
}
cz.RGBToHSL = function(r, g, b) {
r /= 255;
g /= 255;
b /= 255;
var max = Math.max(r, g, b);
var min = Math.min(r, g, b);
var l = (max + min) / 2;
var h, s;
if (max == min) {
h = s = 0;
} else{
var delta = max - min;
s = delta / ((l > 0.5) ? (2 - max - min) : (max + min));
switch (max) {
case r: h = (g - b) / delta + (g < b ? 6 : 0); break;
case g: h = (b - r) / delta + 2; break;
case b: h = (r - g) / delta + 4; break;
}
h /= 6;
}
h = Math.round(h * 360);
s = Math.round(s * 255);
l = Math.round(l * 255);
return new cz.HSLTriple(h, s, l);
}
// http://en.wikipedia.org/wiki/SRGB_color_space
cz.RGBToXYZ = function(r, g, b) {
r /= 255;
g /= 255;
b /= 255;
function norm(x) {
return (x > 0.04045) ? Math.pow( ((x + 0.055) / 1.055), 2.4) : (x / 12.92);
}
r = norm(r);
g = norm(g);
b = norm(b);
var x = 0.4124*r + 0.3576*g + 0.1805*b;
var y = 0.2126*r + 0.7152*g + 0.0722*b;
var z = 0.0193*r + 0.1192*g + 0.9505*b;
x *= 100;
y *= 100;
z *= 100;
return new cz.XYZTriple(x, y, z);
}
cz.XYZToRGB = function(x, y, z) {
x /= 100;
y /= 100;
z /= 100;
function norm(x) {
return (x > 0.0031308) ? 1.055*Math.pow(x, 1/2.4) - 0.055 : 12.92*x;
}
var r = 3.2406*x - 1.5372*y - 0.4986*z;
var g = -0.9689*x + 1.8758*y + 0.0415*z;
var b = 0.0557*x - 0.2040*y + 1.0570*z;
r = norm(r);
g = norm(g);
b = norm(b);
function bounds(x) {
if (x < 0) return 0;
if (x > 255) return 255;
return x;
}
r = bounds(Math.round(r*255));
g = bounds(Math.round(g*255));
b = bounds(Math.round(b*255));
return new cz.RGBTriple(r, g, b);
}
cz.XYZToLAB = function(x, y, z) {
function func(t) {
const thresh = 0.00885645; // (6/29)^3
return (t > thresh) ? Math.pow(t, 0.3333333333) : 7.787037037037*t + 0.13793103448; // (1/3)*(29/6)^2*t + 4/29
}
// observer = 2A�
// illuminant = D50
var xn = 96.422;
var yn = 100;
var zn = 82.521;
var fx = func(x/xn);
var fy = func(y/yn);
var fz = func(z/zn);
var l = 116*fy - 16;
var a = 500*(fx - fy);
var b = 200*(fy - fz);
return new cz.LABTriple(l, a, b);
}
cz.LABToXYZ = function(l, a, b) {
function func(t) {
const thresh = 6/29;
return (t > thresh) ? Math.pow(t, 3) : (t - 0.137931)*0.128418549;
}
var fy = (l + 16) / 116;
var fx = fy + a/500;
var fz = fy - b/200;
var x = func(fx);
var y = func(fy);
var z = func(fz);
// observer = 2A�
// illuminant = D50
var xn = 96.422;
var yn = 100;
var zn = 82.521;
x *= xn;
y *= yn;
z *= zn;
return new cz.XYZTriple(x, y, z);
}
cz.RGBToLAB = function(r, g, b) {
var col = cz.RGBToXYZ(r, g, b);
return cz.XYZToLAB(col.x, col.y, col.z);
}
cz.LABToRGB = function(l, a, b) {
var col = cz.LABToXYZ(parseFloat(l), parseFloat(a), parseFloat(b));
return cz.XYZToRGB(col.x, col.y, col.z);
}
cz.HSVToRGB = function(hue, sat, val) {
var r, g, b;
var i, f, p, q, t;
if (sat == 0) {
r = g = b = val;
} else {
hue = ((hue * 359) / (255*60));
sat /= 255;
val /= 255;
i = Math.floor(hue);
f = hue - i;
p = val * (1 - sat);
q = val * (1 - (sat * f));
t = val * (1 - sat * (1 - f));
switch (i) {
case 0 : r = val; g = t; b = p; break;
case 1 : r = q; g = val; b = p; break;
case 2 : r = p; g = val; b = t; break;
case 3 : r = p; g = q; b = val; break;
case 4 : r = t; g = p; b = val; break;
default: r = val; g = p; b = q;
}
r = Math.round(r * 255);
g = Math.round(g * 255);
b = Math.round(b * 255);
}
return new cz.RGBTriple(r, g, b);
}
cz.RGBToCMYK = function(r, g, b) {
var c = 255 - r;
var m = 255 - g;
var y = 255 - b;
var k = Math.min(c, Math.min(m, y));
if (k == 255) {
c = m = y = 0;
} else {
c = Math.round(255*((c - k) / (255 - k)));
m = Math.round(255*((m - k) / (255 - k)));
y = Math.round(255*((y - k) / (255 - k)));
}
return new cz.CMYKQuadruple(c, m, y, k);
}
cz.CMYKToRGB = function(c, m, y, k) {
c /= 255; m /= 255; y /= 255; k /= 255;
c = c*(1-k) + k;
m = m*(1-k) + k;
y = y*(1-k) + k;
r = Math.round((1-c)*255);
g = Math.round((1-m)*255);
b = Math.round((1-y)*255);
return new cz.RGBTriple(r, g, b);
}
// this is actually a "value" component of HSV:
cz.GetColorLightness = function(color) {
var r = cz.GetRValue(color);
var g = cz.GetGValue(color);
var b = cz.GetBValue(color);
return Math.max(r, g, b);
}
// #ff0000 -> red
cz.HexaAttributeToPredefinedColor = function(hexCol) {
hexCol = hexCol.toLowerCase();
if (hexCol == "#800000") { return "maroon"; }
if (hexCol == "#ff0000") { return "red"; }
if (hexCol == "#ffa500") { return "orange"; }
if (hexCol == "#ffff00") { return "yellow"; }
if (hexCol == "#808000") { return "olive"; }
if (hexCol == "#800080") { return "purple"; }
if (hexCol == "#ff00ff") { return "fuchsia"; }
if (hexCol == "#ffffff") { return "white"; }
if (hexCol == "#00ff00") { return "lime"; }
if (hexCol == "#008000") { return "green"; }
if (hexCol == "#000080") { return "navy"; }
if (hexCol == "#0000ff") { return "blue"; }
if (hexCol == "#00ffff") { return "aqua"; }
if (hexCol == "#008080") { return "teal"; }
if (hexCol == "#000000") { return "black"; }
if (hexCol == "#c0c0c0") { return "silver"; }
if (hexCol == "#808080") { return "gray"; }
return null;
}
cz.FixHexValue = function(val) {
if ((val.length != 7) || (val.substr(0, 1) != "#")) {
return "#000000";
}
return val;
}
cz.FixByteValue = function(val) {
if (val > 255) {
val = 255;
} else {
if (val < 0) {
val = 0;
}
}
return val;
}
cz.Fix100Value = function(val) {
if (val > 100) {
val = 100;
} else {
if (val < 0) {
val = 0;
}
}
return val;
}
cz.FixLabABValue = function(val) {
if (val > 127) {
val = 127;
} else {
if (val < -128) {
val = -128;
}
}
return val;
}
cz.ValidateByteValue = function(val) {
if ((val < 0) || (val > 255)) {
return false;
}
return true;
}
cz.CompareTwoStrings = function(a, b) {
if ( a < b ) return -1;
if ( a > b ) return 1;
return 0;
}
cz.ClipString = function(text, length) {
if (typeof length == 'undefined') {
length = 15;
}
if (!text) return text;
var clippedText;
if (text.length > length) {
clippedText = text.substr(0,length) + "...";
} else {
clippedText = text;
}
return clippedText;
}
cz.GetColorPalettePermalink = function(colors, originUrl, name) {
// we are will try to keep the URL length under 2000 chars
var colorsHex = [];
for (var i=0; i < colors.length; i++) {
var color = colors[i];
var colorHex = cz.ColToRGBHexaAttribute(color).substring(1);
colorsHex.push(colorHex);
}
if (colorsHex.length > 256) {
// no more than 256 colors (URL length limitations)
colorsHex = colorsHex.slice(0, 255);
}
colors = colorsHex.join('+');
var prefix = 'http://colorzilla.com/colors';
var colorzillaPaletteURL = prefix + '/' + colorsHex.join('+');
if (name) {
if (name.length > 64) {
// clip the name if too long
name = name.substr(0, 64);
}
colorzillaPaletteURL += '/' + encodeURIComponent(name);
}
var url = null;
if (originUrl) {
url = originUrl;
url = url.replace(/^https?:\/\//, '');
url = url.replace(/\?.*$/, '');
url = url.replace(/#.*$/, '');
url = encodeURIComponent(url);
}
if (url) {
if ((colorzillaPaletteURL.length + url.length) < 1900) {
colorzillaPaletteURL += '?source-url=' + url;
}
}
return colorzillaPaletteURL;
}
cz.gbCZLowerCaseHexa = false;
0 info it worked if it ends with ok
1 verbose cli [ 'C:\\nodejs\\node.exe',
1 verbose cli 'C:\\Users\\hagb4rd\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli 'publish' ]
2 info using npm@3.8.6
3 info using node@v6.1.0
4 verbose publish [ '.' ]
5 silly cache add args [ '.', null ]
6 verbose cache add spec .
7 silly cache add parsed spec Result {
7 silly cache add raw: '.',
7 silly cache add scope: null,
7 silly cache add name: null,
7 silly cache add rawSpec: '.',
7 silly cache add spec: 'D:\\node.js\\deepblu\\colorzilla-utils',
7 silly cache add type: 'directory' }
8 verbose addLocalDirectory C:\Users\hagb4rd\AppData\Roaming\npm-cache\colorzilla-utils\0.0.1\package.tgz not in flight; packing
9 verbose correctMkdir C:\Users\hagb4rd\AppData\Roaming\npm-cache correctMkdir not in flight; initializing
10 info lifecycle colorzilla-utils@0.0.1~prepublish: colorzilla-utils@0.0.1
11 silly lifecycle colorzilla-utils@0.0.1~prepublish: no script for prepublish, continuing
12 verbose tar pack [ 'C:\\Users\\hagb4rd\\AppData\\Roaming\\npm-cache\\colorzilla-utils\\0.0.1\\package.tgz',
12 verbose tar pack 'D:\\node.js\\deepblu\\colorzilla-utils' ]
13 verbose tarball C:\Users\hagb4rd\AppData\Roaming\npm-cache\colorzilla-utils\0.0.1\package.tgz
14 verbose folder D:\node.js\deepblu\colorzilla-utils
15 verbose addLocalTarball adding from inside cache C:\Users\hagb4rd\AppData\Roaming\npm-cache\colorzilla-utils\0.0.1\package.tgz
16 verbose correctMkdir C:\Users\hagb4rd\AppData\Roaming\npm-cache correctMkdir not in flight; initializing
17 silly cache afterAdd colorzilla-utils@0.0.1
18 verbose afterAdd C:\Users\hagb4rd\AppData\Roaming\npm-cache\colorzilla-utils\0.0.1\package\package.json not in flight; writing
19 verbose correctMkdir C:\Users\hagb4rd\AppData\Roaming\npm-cache correctMkdir not in flight; initializing
20 verbose afterAdd C:\Users\hagb4rd\AppData\Roaming\npm-cache\colorzilla-utils\0.0.1\package\package.json written
21 silly publish { name: 'colorzilla-utils',
21 silly publish version: '0.0.1',
21 silly publish description: 'color related helper tools',
21 silly publish main: 'index.js',
21 silly publish scripts: { test: 'echo "Error: no test specified" && exit 1' },
21 silly publish repository:
21 silly publish { type: 'git',
21 silly publish url: 'git+ssh://git@gist.github.com/b8a330fa5323a98244b7.git' },
21 silly publish keywords: [ 'colors', 'conversion' ],
21 silly publish author: { name: 'ColorZilla' },
21 silly publish license: 'ISC',
21 silly publish bugs: { url: 'https://gist.github.com/b8a330fa5323a98244b7' },
21 silly publish homepage: 'https://gist.github.com/b8a330fa5323a98244b7',
21 silly publish readme: '# colorzilla-utils.js\r\n\r\n* copyrights reserved by ColorZilla *\r\n\r\n```js\r\n\r\n//\r\n// This file is part of ColorZilla\r\n//\r\n// Written by Alex Sirota (alex @ iosart.com)\r\n//\r\n// Copyright (c) iosart labs llc 2011, All Rights Reserved\r\n//\r\nvar cz = exports;\r\n\r\ncz.HSVTriple = function(h, s, v) {\r\n\tthis.h = h;\r\n\tthis.s = s;\r\n\tthis.v = v;\r\n\r\n\tthis.toString = function () {\r\n\t\treturn "(" + this.h + ", " + this.s + ", " + this.v + ")";\r\n\t}\r\n}\r\n\r\ncz.HSLTriple = function(h, s, l) {\r\n\tthis.h = h;\r\n\tthis.s = s;\r\n\tthis.l = l;\r\n\r\n\tthis.toString = function () {\r\n\t\treturn "(" + this.h + ", " + this.s + ", " + this.l + ")";\r\n\t}\r\n}\r\n\r\ncz.RGBTriple = function(r, g, b) {\r\n\tthis.r = r;\r\n\tthis.g = g;\r\n\tthis.b = b;\r\n\r\n\tthis.toString = function () {\r\n\t\treturn "(" + this.r + ", " + this.g + ", " + this.b + ")";\r\n\t}\r\n}\r\n\r\ncz.LABTriple = function(l, a, b) {\r\n\tthis.l = l;\r\n\tthis.a = a;\r\n\tthis.b = b;\r\n\r\n\tthis.toString = function () {\r\n\t\treturn "(" + this.l + ", " + this.a + ", " + this.b + ")";\r\n\t}\r\n}\r\n\r\ncz.XYZTriple = function(x, y, z) {\r\n\tthis.x = x;\r\n\tthis.y = y;\r\n\tthis.z = z;\r\n\r\n\tthis.toString = function () {\r\n\t\treturn "(" + this.x + ", " + this.y + ", " + this.z + ")";\r\n\t}\r\n}\r\n\r\ncz.CMYKQuadruple = function(c, m, y, k) {\r\n\tthis.c = c;\r\n\tthis.m = m;\r\n\tthis.y = y;\r\n\tthis.k = k;\r\n\r\n\tthis.toString = function () {\r\n\t\treturn "(" + this.c + ", " + this.m + ", " + this.y + ", " + this.k + ")";\r\n\t}\r\n}\r\n\r\n// RGB -> COLORREF\r\ncz.RGBToColor = function(r, g, b) {\r\n\treturn r | (g << 8) | (b << 16);\r\n}\r\n\r\n// COLORREF -> R/G/B:\r\ncz.GetRValue = function(color) {\r\n\treturn color & 0xff;\r\n}\r\n\r\ncz.GetGValue = function(color) {\r\n\treturn (color >> 8) & 0xff;\r\n}\r\n\r\ncz.GetBValue = function(color) {\r\n\treturn (color >> 16) & 0xff;\r\n}\r\n\r\n// 0..255 -> 0% - 100%\r\ncz.IntToPercent = function(val) {\r\n\treturn Math.floor(((val * 100) / 255) + 0.5);\r\n}\r\n\r\n// 0..255 -> 0-360\r\ncz.IntToDegrees = function(val) {\r\n\treturn Math.floor(((val * 360) / 255) + 0.5);\r\n}\r\n\r\n// 0..255 -> 00 - ff \r\ncz.DecimalToHexa = function(val) {\r\n\tvar hexStr = val.toString(16);\r\n\tif (hexStr.length < 2) {\r\n\t\thexStr = "0" + hexStr;\r\n\t}\r\n\treturn hexStr;\r\n}\r\n\r\n// COLORREF -> rgb(100%, 100%, 100%)\r\ncz.ColToRGBPercentageAttribute = function(col) {\r\n \tvar colStr = cz.IntToPercent(cz.GetRValue(col)) + "%, " +\r\n\t\t\tcz.IntToPercent(cz.GetGValue(col)) + "%, " +\r\n\t\t\tcz.IntToPercent(cz.GetBValue(col)) + "%";\r\n\treturn "rgb(" + colStr + ")";\r\n}\r\n\r\n// COLORREF -> hsl(240, 100%, 100%)\r\ncz.ColToHSLAttribute = function(col) {\r\n var hsl = cz.RGBToHSL(cz.GetRValue(col), cz.GetGValue(col), cz.GetBValue(col));\r\n\r\n \tvar colStr = hsl.h + ", " +\r\n cz.IntToPercent(hsl.s) + "%, " +\r\n cz.IntToPercent(hsl.l) + "%";\r\n\treturn "hsl(" + colStr + ")";\r\n}\r\n\r\n// COLORREF -> rgb(255, 255, 255)\r\ncz.ColToRGBAttribute = function(col) {\r\n \tvar colStr = cz.GetRValue(col) + ", " + cz.GetGValue(col) + ", " + cz.GetBValue(col);\r\n\treturn "rgb(" + colStr + ")";\r\n}\r\n\r\n// rgb(255, 255, 255) -> COLORREF\r\ncz.RGBAttributeToCol = function(colAttribute) {\r\n\tvar firstParen = colAttribute.split("(");\r\n\tcolAttribute = firstParen[1];\r\n\tvar secondParen = colAttribute.split(")");\r\n\tcolAttribute = secondParen[0];\r\n\tvar rgbArr = colAttribute.split(",");\r\n\treturn cz.RGBToColor(rgbArr[0], rgbArr[1], rgbArr[2]);\r\n}\r\n\r\n\r\n// COLORREF -> #ffffff\r\ncz.ColToRGBHexaAttribute = function(col) {\r\n \tvar colStr = cz.DecimalToHexa(cz.GetRValue(col)) +\r\n\t\t\tcz.DecimalToHexa(cz.GetGValue(col)) +\r\n\t\t\tcz.DecimalToHexa(cz.GetBValue(col));\r\n\tif (!cz.gbCZLowerCaseHexa) {\r\n\t\tcolStr = colStr.toUpperCase();\r\n\t}\r\n\treturn "#" + colStr;\r\n}\r\n\r\n// #ffffff -> COLORREF\r\n// assumev valid string\r\ncz.RGBHexaAttributeToCol = function(col) {\r\n\tvar red = col.substr(1, 2);\r\n\tvar green = col.substr(3, 2);\r\n\tvar blue = col.substr(5, 2);\r\n\r\n\tred = parseInt(red, 16);\r\n\tgreen = parseInt(green, 16);\r\n\tblue = parseInt(blue, 16);\r\n\r\n\treturn cz.RGBToColor(red, green, blue);\r\n}\r\n\r\ncz.RGBToGrayscale = function(r, g, b) {\r\n\treturn (r * 0.30) + (g * 0.59) + (b * 0.11);\r\n}\r\n\r\n// COLORREF -> rgb/rgb-perc/hsl/hex-no-hash/hex\r\ncz.ColToSpecificColorFormat = function(colorRef, colorFormat) {\r\n var colorStr;\r\n switch (colorFormat) {\r\n case \'rgb\':\r\n colorStr = cz.ColToRGBAttribute(colorRef);\r\n break;\r\n case \'rgb-perc\':\r\n colorStr = cz.ColToRGBPercentageAttribute(colorRef);\r\n break;\r\n case \'hsl\':\r\n colorStr = cz.ColToHSLAttribute(colorRef);\r\n break;\r\n case \'hex-no-hash\':\r\n colorStr = cz.ColToRGBHexaAttribute(colorRef);\r\n colorStr = colorStr.substring(1);\r\n break;\r\n case \'hex\':\r\n colorStr = cz.ColToRGBHexaAttribute(colorRef);\r\n break;\r\n default:\r\n colorStr = cz.ColToRGBHexaAttribute(colorRef);\r\n }\r\n return colorStr;\r\n}\r\n\r\ncz.RGBToHSV = function(r, g, b) {\r\n\tvar hue, sat, val;\r\n\tvar max, dif;\r\n\r\n\tmax = Math.max(r, g, b);\r\n\tdif = max - Math.min(r, g, b);\r\n\tsat = (max == 0) ? 0 : (255 * dif/max);\r\n\r\n\tif (sat == 0) {\r\n\t\thue=0;\r\n\t} else if (r == max) {\r\n\t\thue = 60.0 * (g - b) / dif;\r\n\t} else if (g == max) {\r\n\t\thue = 120.0 + 60.0*(b - r) / dif;\r\n\t} else if (b == max) {\r\n\t\thue = 240.0 + 60.0*(r - g) / dif;\r\n\t}\r\n\r\n\tif (hue < 0.0) {\r\n\t\thue += 360.0;\r\n\t}\r\n\thue = Math.round((hue * 255.0) / 360.0);\r\n\tsat = Math.round(sat);\r\n\r\n\tval = max;\r\n\treturn new cz.HSVTriple(hue, sat, val)\r\n}\r\n\r\n\r\ncz.RGBToHSL = function(r, g, b) {\r\n r /= 255;\r\n g /= 255; \r\n b /= 255;\r\n \r\n var max = Math.max(r, g, b);\r\n var min = Math.min(r, g, b);\r\n\r\n var l = (max + min) / 2;\r\n\r\n var h, s;\r\n \r\n if (max == min) {\r\n h = s = 0; \r\n } else{\r\n var delta = max - min;\r\n s = delta / ((l > 0.5) ? (2 - max - min) : (max + min));\r\n switch (max) {\r\n case r: h = (g - b) / delta + (g < b ? 6 : 0); break;\r\n case g: h = (b - r) / delta + 2; break;\r\n case b: h = (r - g) / delta + 4; break;\r\n }\r\n h /= 6;\r\n }\r\n\r\n h = Math.round(h * 360);\r\n\ts = Math.round(s * 255);\r\n\tl = Math.round(l * 255);\r\n \r\n return new cz.HSLTriple(h, s, l);\r\n}\r\n\r\n// http://en.wikipedia.org/wiki/SRGB_color_space\r\ncz.RGBToXYZ = function(r, g, b) {\r\n r /= 255;\r\n g /= 255; \r\n b /= 255;\r\n\r\n function norm(x) {\r\n return (x > 0.04045) ? Math.pow( ((x + 0.055) / 1.055), 2.4) : (x / 12.92);\r\n }\r\n\r\n r = norm(r);\r\n g = norm(g);\r\n b = norm(b);\r\n \r\n var x = 0.4124*r + 0.3576*g + 0.1805*b;\r\n var y = 0.2126*r + 0.7152*g + 0.0722*b;\r\n var z = 0.0193*r + 0.1192*g + 0.9505*b;\r\n\r\n x *= 100;\r\n\ty *= 100;\r\n\tz *= 100;\r\n \r\n return new cz.XYZTriple(x, y, z);\r\n}\r\n\r\ncz.XYZToRGB = function(x, y, z) {\r\n x /= 100;\r\n y /= 100; \r\n z /= 100;\r\n\r\n function norm(x) {\r\n return (x > 0.0031308) ? 1.055*Math.pow(x, 1/2.4) - 0.055 : 12.92*x;\r\n }\r\n\r\n \r\n var r = 3.2406*x - 1.5372*y - 0.4986*z;\r\n var g = -0.9689*x + 1.8758*y + 0.0415*z;\r\n var b = 0.0557*x - 0.2040*y + 1.0570*z;\r\n\r\n r = norm(r);\r\n g = norm(g);\r\n b = norm(b);\r\n \r\n\r\n function bounds(x) {\r\n if (x < 0) return 0;\r\n if (x > 255) return 255;\r\n return x;\r\n }\r\n\r\n r = bounds(Math.round(r*255));\r\n\tg = bounds(Math.round(g*255));\r\n\tb = bounds(Math.round(b*255));\r\n \r\n return new cz.RGBTriple(r, g, b);\r\n}\r\n\r\ncz.XYZToLAB = function(x, y, z) {\r\n function func(t) {\r\n const thresh = 0.00885645; // (6/29)^3\r\n return (t > thresh) ? Math.pow(t, 0.3333333333) : 7.787037037037*t + 0.13793103448; // (1/3)*(29/6)^2*t + 4/29\r\n }\r\n\r\n // observer = 2A? \r\n // illuminant = D50\r\n var xn = 96.422;\r\n var yn = 100;\r\n var zn = 82.521;\r\n\r\n var fx = func(x/xn);\r\n var fy = func(y/yn);\r\n var fz = func(z/zn);\r\n\r\n var l = 116*fy - 16;\r\n var a = 500*(fx - fy);\r\n var b = 200*(fy - fz);\r\n\r\n return new cz.LABTriple(l, a, b);\r\n}\r\n\r\ncz.LABToXYZ = function(l, a, b) {\r\n function func(t) {\r\n const thresh = 6/29;\r\n return (t > thresh) ? Math.pow(t, 3) : (t - 0.137931)*0.128418549;\r\n }\r\n\r\n var fy = (l + 16) / 116;\r\n var fx = fy + a/500;\r\n var fz = fy - b/200;\r\n\r\n var x = func(fx);\r\n var y = func(fy);\r\n var z = func(fz);\r\n\r\n // observer = 2A? \r\n // illuminant = D50\r\n var xn = 96.422;\r\n var yn = 100;\r\n var zn = 82.521;\r\n\r\n x *= xn; \r\n y *= yn; \r\n z *= zn; \r\n\r\n return new cz.XYZTriple(x, y, z);\r\n}\r\n\r\ncz.RGBToLAB = function(r, g, b) {\r\n var col = cz.RGBToXYZ(r, g, b);\r\n return cz.XYZToLAB(col.x, col.y, col.z);\r\n}\r\n\r\ncz.LABToRGB = function(l, a, b) {\r\n var col = cz.LABToXYZ(parseFloat(l), parseFloat(a), parseFloat(b));\r\n return cz.XYZToRGB(col.x, col.y, col.z);\r\n}\r\n\r\ncz.HSVToRGB = function(hue, sat, val) {\r\n\tvar r, g, b;\r\n\tvar i, f, p, q, t;\r\n\r\n\tif (sat == 0) {\r\n\t\tr = g = b = val;\r\n\t} else {\r\n\t\thue = ((hue * 359) / (255*60));\r\n\t\tsat /= 255;\r\n\t\tval /= 255;\r\n\r\n\t\ti = Math.floor(hue);\r\n\t\tf = hue - i;\r\n\t\tp = val * (1 - sat);\r\n\t\tq = val * (1 - (sat * f));\r\n\t\tt = val * (1 - sat * (1 - f));\r\n\r\n\t\tswitch (i) {\r\n\t\t\tcase 0 : r = val; g = t; b = p; break;\r\n\t\t\tcase 1 : r = q; g = val; b = p; break;\r\n\t\t\tcase 2 : r = p; g = val; b = t; break;\r\n\t\t\tcase 3 : r = p; g = q; b = val; break;\r\n\t\t\tcase 4 : r = t; g = p; b = val; break;\r\n\t\t\tdefault: r = val; g = p; b = q;\r\n\t\t}\r\n\t\tr = Math.round(r * 255);\r\n\t\tg = Math.round(g * 255);\r\n\t\tb = Math.round(b * 255);\r\n\t}\r\n\treturn new cz.RGBTriple(r, g, b);\r\n}\r\n\r\ncz.RGBToCMYK = function(r, g, b) {\r\n var c = 255 - r;\r\n var m = 255 - g;\r\n var y = 255 - b;\r\n var k = Math.min(c, Math.min(m, y));\r\n\r\n if (k == 255) { \r\n c = m = y = 0;\r\n } else {\r\n c = Math.round(255*((c - k) / (255 - k)));\r\n m = Math.round(255*((m - k) / (255 - k)));\r\n y = Math.round(255*((y - k) / (255 - k)));\r\n } \r\n return new cz.CMYKQuadruple(c, m, y, k);\r\n}\r\n\r\ncz.CMYKToRGB = function(c, m, y, k) {\r\n c /= 255; m /= 255; y /= 255; k /= 255;\r\n\r\n c = c*(1-k) + k;\r\n m = m*(1-k) + k;\r\n y = y*(1-k) + k;\r\n\r\n r = Math.round((1-c)*255);\r\n g = Math.round((1-m)*255);\r\n b = Math.round((1-y)*255);\r\n\r\n return new cz.RGBTriple(r, g, b);\r\n}\r\n\r\n\r\n// this is actually a "value" component of HSV:\r\ncz.GetColorLightness = function(color) {\r\n\tvar r = cz.GetRValue(color);\r\n\tvar g = cz.GetGValue(color);\r\n\tvar b = cz.GetBValue(color);\r\n\treturn Math.max(r, g, b);\r\n}\r\n\r\n// #ff0000 -> red\r\ncz.HexaAttributeToPredefinedColor = function(hexCol) {\r\n\thexCol = hexCol.toLowerCase();\r\n \tif (hexCol == "#800000") { return "maroon"; }\r\n \tif (hexCol == "#ff0000") { return "red"; }\r\n \tif (hexCol == "#ffa500") { return "orange"; }\r\n \tif (hexCol == "#ffff00") { return "yellow"; }\r\n \tif (hexCol == "#808000") { return "olive"; }\r\n \tif (hexCol == "#800080") { return "purple"; }\r\n \tif (hexCol == "#ff00ff") { return "fuchsia"; }\r\n \tif (hexCol == "#ffffff") { return "white"; }\r\n \tif (hexCol == "#00ff00") { return "lime"; }\r\n \tif (hexCol == "#008000") { return "green"; }\r\n \tif (hexCol == "#000080") { return "navy"; }\r\n \tif (hexCol == "#0000ff") { return "blue"; }\r\n \tif (hexCol == "#00ffff") { return "aqua"; }\r\n \tif (hexCol == "#008080") { return "teal"; }\r\n \tif (hexCol == "#000000") { return "black"; }\r\n \tif (hexCol == "#c0c0c0") { return "silver"; }\r\n \tif (hexCol == "#808080") { return "gray"; }\r\n\treturn null;\r\n}\r\n\r\ncz.FixHexValue = function(val) {\r\n\tif ((val.length != 7) || (val.substr(0, 1) != "#")) {\r\n\t\treturn "#000000";\r\n\t}\r\n\treturn val;\r\n}\r\n\r\ncz.FixByteValue = function(val) {\r\n\tif (val > 255) {\r\n\t\tval = 255;\r\n\t} else {\r\n\t\tif (val < 0) {\r\n\t\t\tval = 0;\r\n\t\t}\r\n\t}\r\n\r\n\treturn val;\r\n}\r\n\r\ncz.Fix100Value = function(val) {\r\n\tif (val > 100) {\r\n\t\tval = 100;\r\n\t} else {\r\n\t\tif (val < 0) {\r\n\t\t\tval = 0;\r\n\t\t}\r\n\t}\r\n\r\n\treturn val;\r\n}\r\n\r\ncz.FixLabABValue = function(val) {\r\n \tif (val > 127) {\r\n\t\tval = 127;\r\n\t} else {\r\n\t\tif (val < -128) {\r\n\t\t\tval = -128;\r\n\t\t}\r\n\t}\r\n\r\n\treturn val;\r\n}\r\n\r\ncz.ValidateByteValue = function(val) {\r\n\tif ((val < 0) || (val > 255)) {\r\n\t\treturn false;\r\n\t}\r\n\treturn true;\r\n}\r\n\r\ncz.CompareTwoStrings = function(a, b) {\r\n\tif ( a < b ) return -1;\r\n if ( a > b ) return 1;\r\n return 0;\r\n}\r\n\r\ncz.ClipString = function(text, length) {\r\n if (typeof length == \'undefined\') {\r\n length = 15;\r\n }\r\n\r\n if (!text) return text;\r\n\r\n var clippedText; \r\n if (text.length > length) {\r\n clippedText = text.substr(0,length) + "...";\r\n } else {\r\n clippedText = text;\r\n }\r\n return clippedText;\r\n}\r\n\r\ncz.GetColorPalettePermalink = function(colors, originUrl, name) {\r\n // we are will try to keep the URL length under 2000 chars\r\n var colorsHex = [];\r\n \r\n for (var i=0; i < colors.length; i++) {\r\n var color = colors[i];\r\n var colorHex = cz.ColToRGBHexaAttribute(color).substring(1);\r\n colorsHex.push(colorHex);\r\n }\r\n\r\n if (colorsHex.length > 256) {\r\n // no more than 256 colors (URL length limitations)\r\n colorsHex = colorsHex.slice(0, 255); \r\n }\r\n \r\n colors = colorsHex.join(\'+\');\r\n\r\n var prefix = \'http://colorzilla.com/colors\';\r\n var colorzillaPaletteURL = prefix + \'/\' + colorsHex.join(\'+\');\r\n if (name) { \r\n if (name.length > 64) {\r\n // clip the name if too long\r\n name = name.substr(0, 64);\r\n }\r\n colorzillaPaletteURL += \'/\' + encodeURIComponent(name);\r\n }\r\n\r\n var url = null;\r\n if (originUrl) {\r\n url = originUrl;\r\n url = url.replace(/^https?:\\/\\//, \'\');\r\n url = url.replace(/\\?.*$/, \'\');\r\n url = url.replace(/#.*$/, \'\');\r\n url = encodeURIComponent(url);\r\n }\r\n\r\n if (url) {\r\n if ((colorzillaPaletteURL.length + url.length) < 1900) {\r\n colorzillaPaletteURL += \'?source-url=\' + url;\r\n }\r\n }\r\n\r\n return colorzillaPaletteURL;\r\n}\r\n\r\ncz.gbCZLowerCaseHexa = false;\r\n\r\n```',
21 silly publish readmeFilename: 'readme.md',
21 silly publish gitHead: 'fe4f530f81f81fa92e915d315646484f30b8e793',
21 silly publish _id: 'colorzilla-utils@0.0.1',
21 silly publish _shasum: '9fbca8bbcd117df92bf0532a463852ac697a7b05',
21 silly publish _from: '.' }
22 verbose getPublishConfig undefined
23 silly mapToRegistry name colorzilla-utils
24 silly mapToRegistry using default registry
25 silly mapToRegistry registry https://registry.npmjs.org/
26 silly mapToRegistry data Result {
26 silly mapToRegistry raw: 'colorzilla-utils',
26 silly mapToRegistry scope: null,
26 silly mapToRegistry name: 'colorzilla-utils',
26 silly mapToRegistry rawSpec: '',
26 silly mapToRegistry spec: 'latest',
26 silly mapToRegistry type: 'tag' }
27 silly mapToRegistry uri https://registry.npmjs.org/colorzilla-utils
28 verbose publish registryBase https://registry.npmjs.org/
29 silly publish uploading C:\Users\hagb4rd\AppData\Roaming\npm-cache\colorzilla-utils\0.0.1\package.tgz
30 verbose request uri https://registry.npmjs.org/colorzilla-utils
31 verbose request sending authorization for write operation
32 info attempt registry request try #1 at 7:28:14 PM
33 verbose request using bearer token for auth
34 verbose request id 82a51ecf17afd953
35 http request PUT https://registry.npmjs.org/colorzilla-utils
36 http 403 https://registry.npmjs.org/colorzilla-utils
37 verbose headers { 'content-type': 'application/json',
37 verbose headers 'cache-control': 'max-age=300',
37 verbose headers 'content-length': '95',
37 verbose headers 'accept-ranges': 'bytes',
37 verbose headers date: 'Sat, 23 Jul 2016 17:28:14 GMT',
37 verbose headers via: '1.1 varnish',
37 verbose headers connection: 'keep-alive',
37 verbose headers 'x-served-by': 'cache-fra1237-FRA',
37 verbose headers 'x-cache': 'MISS',
37 verbose headers 'x-cache-hits': '0',
37 verbose headers 'x-timer': 'S1469294893.892320,VS0,VE1062',
37 verbose headers vary: 'Accept-Encoding' }
38 verbose request invalidating C:\Users\hagb4rd\AppData\Roaming\npm-cache\registry.npmjs.org\colorzilla-utils on PUT
39 error publish Failed PUT 403
40 verbose stack Error: "You cannot publish over the previously published version 0.0.1." : colorzilla-utils
40 verbose stack at makeError (C:\Users\hagb4rd\AppData\Roaming\npm\node_modules\npm\node_modules\npm-registry-client\lib\request.js:264:12)
40 verbose stack at CachingRegistryClient.<anonymous> (C:\Users\hagb4rd\AppData\Roaming\npm\node_modules\npm\node_modules\npm-registry-client\lib\request.js:252:14)
40 verbose stack at Request._callback (C:\Users\hagb4rd\AppData\Roaming\npm\node_modules\npm\node_modules\npm-registry-client\lib\request.js:172:14)
40 verbose stack at Request.self.callback (C:\Users\hagb4rd\AppData\Roaming\npm\node_modules\npm\node_modules\request\request.js:199:22)
40 verbose stack at emitTwo (events.js:106:13)
40 verbose stack at Request.emit (events.js:191:7)
40 verbose stack at Request.<anonymous> (C:\Users\hagb4rd\AppData\Roaming\npm\node_modules\npm\node_modules\request\request.js:1036:10)
40 verbose stack at emitOne (events.js:101:20)
40 verbose stack at Request.emit (events.js:188:7)
40 verbose stack at IncomingMessage.<anonymous> (C:\Users\hagb4rd\AppData\Roaming\npm\node_modules\npm\node_modules\request\request.js:963:12)
41 verbose statusCode 403
42 verbose pkgid colorzilla-utils
43 verbose cwd D:\node.js\deepblu\colorzilla-utils
44 error Windows_NT 6.3.9600
45 error argv "C:\\nodejs\\node.exe" "C:\\Users\\hagb4rd\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "publish"
46 error node v6.1.0
47 error npm v3.8.6
48 error code E403
49 error "You cannot publish over the previously published version 0.0.1." : colorzilla-utils
50 error If you need help, you may report this error at:
50 error <https://github.com/npm/npm/issues>
51 verbose exit [ 1, true ]
{
"name": "colorzilla-utils",
"version": "0.0.2",
"description": "color related helper tools",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+ssh://git@gist.github.com/b8a330fa5323a98244b7.git"
},
"keywords": [
"colors",
"conversion"
],
"author": "ColorZilla",
"license": "ISC",
"bugs": {
"url": "https://gist.github.com/b8a330fa5323a98244b7"
},
"homepage": "https://gist.github.com/b8a330fa5323a98244b7"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment