Andrew Meadowsarchive remaining JS scriptsc0b446c10 years agoHistory
1155 lines · 34.1 KB · JavaScript
RawDownload
// TinyColor v1.3.0// https://github.com/bgrins/TinyColor// Brian Grinstead, MIT License   var trimLeft = /^\s+/,    trimRight = /\s+$/,    tinyCounter = 0,    math = Math,    mathRound = math.round,    mathMin = math.min,    mathMax = math.max,    mathRandom = math.random; function tinycolor (color, opts) {     color = (color) ? color : '';    opts = opts || { };     // If input is already a tinycolor, return itself    if (color instanceof tinycolor) {       return color;    }    // If we are called as a function, call using new instead    if (!(this instanceof tinycolor)) {        return new tinycolor(color, opts);    }     var rgb = inputToRGB(color);    this._originalInput = color,    this._r = rgb.r,    this._g = rgb.g,    this._b = rgb.b,    this._a = rgb.a,    this._roundA = mathRound(100*this._a) / 100,    this._format = opts.format || rgb.format;    this._gradientType = opts.gradientType;     // Don't let the range of [0,255] come back in [0,1].    // 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 (this._r < 1) { this._r = mathRound(this._r); }    if (this._g < 1) { this._g = mathRound(this._g); }    if (this._b < 1) { this._b = mathRound(this._b); }     this._ok = rgb.ok;    this._tc_id = tinyCounter++;} tinycolor.prototype = {    isDark: function() {        return this.getBrightness() < 128;    },    isLight: function() {        return !this.isDark();    },    isValid: function() {        return this._ok;    },    getOriginalInput: function() {      return this._originalInput;    },    getFormat: function() {        return this._format;    },    getAlpha: function() {        return this._a;    },    getBrightness: function() {        //http://www.w3.org/TR/AERT#color-contrast        var rgb = this.toRgb();        return (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000;    },    getLuminance: function() {        //http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef        var rgb = this.toRgb();        var RsRGB, GsRGB, BsRGB, R, G, B;        RsRGB = rgb.r/255;        GsRGB = rgb.g/255;        BsRGB = rgb.b/255;         if (RsRGB <= 0.03928) {R = RsRGB / 12.92;} else {R = Math.pow(((RsRGB + 0.055) / 1.055), 2.4);}        if (GsRGB <= 0.03928) {G = GsRGB / 12.92;} else {G = Math.pow(((GsRGB + 0.055) / 1.055), 2.4);}        if (BsRGB <= 0.03928) {B = BsRGB / 12.92;} else {B = Math.pow(((BsRGB + 0.055) / 1.055), 2.4);}        return (0.2126 * R) + (0.7152 * G) + (0.0722 * B);    },    setAlpha: function(value) {        this._a = boundAlpha(value);        this._roundA = mathRound(100*this._a) / 100;        return this;    },    toHsv: function() {        var hsv = rgbToHsv(this._r, this._g, this._b);        return { h: hsv.h * 360, s: hsv.s, v: hsv.v, a: this._a };    },    toHsvString: function() {        var hsv = rgbToHsv(this._r, this._g, this._b);        var h = mathRound(hsv.h * 360), s = mathRound(hsv.s * 100), v = mathRound(hsv.v * 100);        return (this._a == 1) ?          "hsv("  + h + ", " + s + "%, " + v + "%)" :          "hsva(" + h + ", " + s + "%, " + v + "%, "+ this._roundA + ")";    },    toHsl: function() {        var hsl = rgbToHsl(this._r, this._g, this._b);        return { h: hsl.h * 360, s: hsl.s, l: hsl.l, a: this._a };    },    toHslString: function() {        var hsl = rgbToHsl(this._r, this._g, this._b);        var h = mathRound(hsl.h * 360), s = mathRound(hsl.s * 100), l = mathRound(hsl.l * 100);        return (this._a == 1) ?          "hsl("  + h + ", " + s + "%, " + l + "%)" :          "hsla(" + h + ", " + s + "%, " + l + "%, "+ this._roundA + ")";    },    toHex: function(allow3Char) {        return rgbToHex(this._r, this._g, this._b, allow3Char);    },    toHexString: function(allow3Char) {        return '#' + this.toHex(allow3Char);    },    toHex8: function() {        return rgbaToHex(this._r, this._g, this._b, this._a);    },    toHex8String: function() {        return '#' + this.toHex8();    },    toRgb: function() {        return { r: mathRound(this._r), g: mathRound(this._g), b: mathRound(this._b), a: this._a };    },    toRgbString: function() {        return (this._a == 1) ?          "rgb("  + mathRound(this._r) + ", " + mathRound(this._g) + ", " + mathRound(this._b) + ")" :          "rgba(" + mathRound(this._r) + ", " + mathRound(this._g) + ", " + mathRound(this._b) + ", " + this._roundA + ")";    },    toPercentageRgb: function() {        return { r: mathRound(bound01(this._r, 255) * 100) + "%", g: mathRound(bound01(this._g, 255) * 100) + "%", b: mathRound(bound01(this._b, 255) * 100) + "%", a: this._a };    },    toPercentageRgbString: function() {        return (this._a == 1) ?          "rgb("  + mathRound(bound01(this._r, 255) * 100) + "%, " + mathRound(bound01(this._g, 255) * 100) + "%, " + mathRound(bound01(this._b, 255) * 100) + "%)" :          "rgba(" + mathRound(bound01(this._r, 255) * 100) + "%, " + mathRound(bound01(this._g, 255) * 100) + "%, " + mathRound(bound01(this._b, 255) * 100) + "%, " + this._roundA + ")";    },    toName: function() {        if (this._a === 0) {            return "transparent";        }         if (this._a < 1) {            return false;        }         return hexNames[rgbToHex(this._r, this._g, this._b, true)] || false;    },    toFilter: function(secondColor) {        var hex8String = '#' + rgbaToHex(this._r, this._g, this._b, this._a);        var secondHex8String = hex8String;        var gradientType = this._gradientType ? "GradientType = 1, " : "";         if (secondColor) {            var s = tinycolor(secondColor);            secondHex8String = s.toHex8String();        }         return "progid:DXImageTransform.Microsoft.gradient("+gradientType+"startColorstr="+hex8String+",endColorstr="+secondHex8String+")";    },    toString: function(format) {        var formatSet = !!format;        format = format || this._format;         var formattedString = false;        var hasAlpha = this._a < 1 && this._a >= 0;        var needsAlphaFormat = !formatSet && hasAlpha && (format === "hex" || format === "hex6" || format === "hex3" || format === "name");         if (needsAlphaFormat) {            // Special case for "transparent", all other non-alpha formats            // will return rgba when there is transparency.            if (format === "name" && this._a === 0) {                return this.toName();            }            return this.toRgbString();        }        if (format === "rgb") {            formattedString = this.toRgbString();        }        if (format === "prgb") {            formattedString = this.toPercentageRgbString();        }        if (format === "hex" || format === "hex6") {            formattedString = this.toHexString();        }        if (format === "hex3") {            formattedString = this.toHexString(true);        }        if (format === "hex8") {            formattedString = this.toHex8String();        }        if (format === "name") {            formattedString = this.toName();        }        if (format === "hsl") {            formattedString = this.toHslString();        }        if (format === "hsv") {            formattedString = this.toHsvString();        }         return formattedString || this.toHexString();    },    clone: function() {        return tinycolor(this.toString());    },     _applyModification: function(fn, args) {        var color = fn.apply(null, [this].concat([].slice.call(args)));        this._r = color._r;        this._g = color._g;        this._b = color._b;        this.setAlpha(color._a);        return this;    },    lighten: function() {        return this._applyModification(lighten, arguments);    },    brighten: function() {        return this._applyModification(brighten, arguments);    },    darken: function() {        return this._applyModification(darken, arguments);    },    desaturate: function() {        return this._applyModification(desaturate, arguments);    },    saturate: function() {        return this._applyModification(saturate, arguments);    },    greyscale: function() {        return this._applyModification(greyscale, arguments);    },    spin: function() {        return this._applyModification(spin, arguments);    },     _applyCombination: function(fn, args) {        return fn.apply(null, [this].concat([].slice.call(args)));    },    analogous: function() {        return this._applyCombination(analogous, arguments);    },    complement: function() {        return this._applyCombination(complement, arguments);    },    monochromatic: function() {        return this._applyCombination(monochromatic, arguments);    },    splitcomplement: function() {        return this._applyCombination(splitcomplement, arguments);    },    triad: function() {        return this._applyCombination(triad, arguments);    },    tetrad: function() {        return this._applyCombination(tetrad, arguments);    }}; // 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 1tinycolor.fromRatio = function(color, opts) {    if (typeof color == "object") {        var newColor = {};        for (var i in color) {            if (color.hasOwnProperty(i)) {                if (i === "a") {                    newColor[i] = color[i];                }                else {                    newColor[i] = convertToPercentage(color[i]);                }            }        }        color = newColor;    }     return tinycolor(color, opts);}; // Given a string or object, convert that input to RGB// Possible string inputs:////     "red"//     "#f00" or "f00"//     "#ff0000" or "ff0000"//     "#ff000000" or "ff000000"//     "rgb 255 0 0" or "rgb (255, 0, 0)"//     "rgb 1.0 0 0" or "rgb (1, 0, 0)"//     "rgba (255, 0, 0, 1)" or "rgba 255, 0, 0, 1"//     "rgba (1.0, 0, 0, 1)" or "rgba 1.0, 0, 0, 1"//     "hsl(0, 100%, 50%)" or "hsl 0 100% 50%"//     "hsla(0, 100%, 50%, 1)" or "hsla 0 100% 50%, 1"//     "hsv(0, 100%, 100%)" or "hsv 0 100% 100%"//function inputToRGB(color) {     var rgb = { r: 0, g: 0, b: 0 };    var a = 1;    var ok = false;    var format = false;     if (typeof color == "string") {        color = stringInputToObject(color);    }     if (typeof color == "object") {        if (color.hasOwnProperty("r") && color.hasOwnProperty("g") && color.hasOwnProperty("b")) {            rgb = rgbToRgb(color.r, color.g, color.b);            ok = true;            format = String(color.r).substr(-1) === "%" ? "prgb" : "rgb";        }        else if (color.hasOwnProperty("h") && color.hasOwnProperty("s") && color.hasOwnProperty("v")) {            color.s = convertToPercentage(color.s);            color.v = convertToPercentage(color.v);            rgb = hsvToRgb(color.h, color.s, color.v);            ok = true;            format = "hsv";        }        else if (color.hasOwnProperty("h") && color.hasOwnProperty("s") && color.hasOwnProperty("l")) {            color.s = convertToPercentage(color.s);            color.l = convertToPercentage(color.l);            rgb = hslToRgb(color.h, color.s, color.l);            ok = true;            format = "hsl";        }         if (color.hasOwnProperty("a")) {            a = color.a;        }    }     a = boundAlpha(a);     return {        ok: ok,        format: color.format || format,        r: mathMin(255, mathMax(rgb.r, 0)),        g: mathMin(255, mathMax(rgb.g, 0)),        b: mathMin(255, mathMax(rgb.b, 0)),        a: a    };}  // Conversion Functions// -------------------- // `rgbToHsl`, `rgbToHsv`, `hslToRgb`, `hsvToRgb` modified from:// <http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript> // `rgbToRgb`// Handle bounds / percentage checking to conform to CSS color spec// <http://www.w3.org/TR/css3-color/>// *Assumes:* r, g, b in [0, 255] or [0, 1]// *Returns:* { r, g, b } in [0, 255]function rgbToRgb(r, g, b){    return {        r: bound01(r, 255) * 255,        g: bound01(g, 255) * 255,        b: bound01(b, 255) * 255    };} // `rgbToHsl`// Converts an RGB color value to HSL.// *Assumes:* r, g, and b are contained in [0, 255] or [0, 1]// *Returns:* { h, s, l } in [0,1]function rgbToHsl(r, g, b) {     r = bound01(r, 255);    g = bound01(g, 255);    b = bound01(b, 255);     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    }    else {        var d = max - min;        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);        switch(max) {            case r: h = (g - b) / d + (g < b ? 6 : 0); break;            case g: h = (b - r) / d + 2; break;            case b: h = (r - g) / d + 4; break;        }         h /= 6;    }     return { h: h, s: s, l: l };} // `hslToRgb`// Converts an HSL color value to RGB.// *Assumes:* h is contained in [0, 1] or [0, 360] and s and l are contained [0, 1] or [0, 100]// *Returns:* { r, g, b } in the set [0, 255]function hslToRgb(h, s, l) {    var r, g, b;     h = bound01(h, 360);    s = bound01(s, 100);    l = bound01(l, 100);     function hue2rgb(p, q, t) {        if(t < 0) t += 1;        if(t > 1) t -= 1;        if(t < 1/6) return p + (q - p) * 6 * t;        if(t < 1/2) return q;        if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;        return p;    }     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;        r = hue2rgb(p, q, h + 1/3);        g = hue2rgb(p, q, h);        b = hue2rgb(p, q, h - 1/3);    }     return { r: r * 255, g: g * 255, b: b * 255 };} // `rgbToHsv`// Converts an RGB color value to HSV// *Assumes:* r, g, and b are contained in the set [0, 255] or [0, 1]// *Returns:* { h, s, v } in [0,1]function rgbToHsv(r, g, b) {     r = bound01(r, 255);    g = bound01(g, 255);    b = bound01(b, 255);     var max = mathMax(r, g, b), min = mathMin(r, g, b);    var h, s, v = max;     var d = max - min;    s = max === 0 ? 0 : d / max;     if(max == min) {        h = 0; // achromatic    }    else {        switch(max) {            case r: h = (g - b) / d + (g < b ? 6 : 0); break;            case g: h = (b - r) / d + 2; break;            case b: h = (r - g) / d + 4; break;        }        h /= 6;    }    return { h: h, s: s, v: v };} // `hsvToRgb`// Converts an HSV color value to RGB.// *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) {     h = bound01(h, 360) * 6;    s = bound01(s, 100);    v = bound01(v, 100);     var i = math.floor(h),        f = h - i,        p = v * (1 - s),        q = v * (1 - f * s),        t = v * (1 - (1 - f) * s),        mod = i % 6,        r = [v, q, p, p, t, v][mod],        g = [t, v, v, q, p, p][mod],        b = [p, p, t, v, v, q][mod];     return { r: r * 255, g: g * 255, b: b * 255 };} // `rgbToHex`// Converts an RGB color to hex// Assumes r, g, and b are contained in the set [0, 255]// Returns a 3 or 6 character hexfunction rgbToHex(r, g, b, allow3Char) {     var hex = [        pad2(mathRound(r).toString(16)),        pad2(mathRound(g).toString(16)),        pad2(mathRound(b).toString(16))    ];

Lines 1–500 of 1155.