Last active
December 20, 2015 10:49
-
-
Save Buildstarted/6118928 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class Color { | |
| public A: number; | |
| public B: number; | |
| public R: number; | |
| public G: number; | |
| public static AliceBlue: Color = new Color("#F0F8FF"); | |
| public static AntiqueWhite: Color = new Color("#FAEBD7"); | |
| public static Aqua: Color = new Color("#00FFFF"); | |
| public static Aquamarine: Color = new Color("#7FFFD4"); | |
| public static Azure: Color = new Color("#F0FFFF"); | |
| public static Beige: Color = new Color("#F5F5DC"); | |
| public static Bisque: Color = new Color("#FFE4C4"); | |
| public static Black: Color = new Color("#000000"); | |
| public static BlanchedAlmond: Color = new Color("#FFEBCD"); | |
| public static Blue: Color = new Color("#0000FF"); | |
| public static BlueViolet: Color = new Color("#8A2BE2"); | |
| public static Brown: Color = new Color("#A52A2A"); | |
| public static BurlyWood: Color = new Color("#DEB887"); | |
| public static CadetBlue: Color = new Color("#5F9EA0"); | |
| public static Chartreuse: Color = new Color("#7FFF00"); | |
| public static Chocolate: Color = new Color("#D2691E"); | |
| public static Coral: Color = new Color("#FF7F50"); | |
| public static CornflowerBlue: Color = new Color("#6495ED"); | |
| public static Cornsilk: Color = new Color("#FFF8DC"); | |
| public static Crimson: Color = new Color("#DC143C"); | |
| public static Cyan: Color = new Color("#00FFFF"); | |
| public static DarkBlue: Color = new Color("#00008B"); | |
| public static DarkCyan: Color = new Color("#008B8B"); | |
| public static DarkGoldenRod: Color = new Color("#B8860B"); | |
| public static DarkGray: Color = new Color("#A9A9A9"); | |
| public static DarkGreen: Color = new Color("#006400"); | |
| public static DarkKhaki: Color = new Color("#BDB76B"); | |
| public static DarkMagenta: Color = new Color("#8B008B"); | |
| public static DarkOliveGreen: Color = new Color("#556B2F"); | |
| public static DarkOrange: Color = new Color("#FF8C00"); | |
| public static DarkOrchid: Color = new Color("#9932CC"); | |
| public static DarkRed: Color = new Color("#8B0000"); | |
| public static DarkSalmon: Color = new Color("#E9967A"); | |
| public static DarkSeaGreen: Color = new Color("#8FBC8F"); | |
| public static DarkSlateBlue: Color = new Color("#483D8B"); | |
| public static DarkSlateGray: Color = new Color("#2F4F4F"); | |
| public static DarkTurquoise: Color = new Color("#00CED1"); | |
| public static DarkViolet: Color = new Color("#9400D3"); | |
| public static DeepPink: Color = new Color("#FF1493"); | |
| public static DeepSkyBlue: Color = new Color("#00BFFF"); | |
| public static DimGray: Color = new Color("#696969"); | |
| public static DodgerBlue: Color = new Color("#1E90FF"); | |
| public static FireBrick: Color = new Color("#B22222"); | |
| public static FloralWhite: Color = new Color("#FFFAF0"); | |
| public static ForestGreen: Color = new Color("#228B22"); | |
| public static Fuchsia: Color = new Color("#FF00FF"); | |
| public static Gainsboro: Color = new Color("#DCDCDC"); | |
| public static GhostWhite: Color = new Color("#F8F8FF"); | |
| public static Gold: Color = new Color("#FFD700"); | |
| public static GoldenRod: Color = new Color("#DAA520"); | |
| public static Gray: Color = new Color("#808080"); | |
| public static Green: Color = new Color("#008000"); | |
| public static GreenYellow: Color = new Color("#ADFF2F"); | |
| public static HoneyDew: Color = new Color("#F0FFF0"); | |
| public static HotPink: Color = new Color("#FF69B4"); | |
| public static IndianRed: Color = new Color("#CD5C5C"); | |
| public static Indigo: Color = new Color("#4B0082"); | |
| public static Ivory: Color = new Color("#FFFFF0"); | |
| public static Khaki: Color = new Color("#F0E68C"); | |
| public static Lavender: Color = new Color("#E6E6FA"); | |
| public static LavenderBlush: Color = new Color("#FFF0F5"); | |
| public static LawnGreen: Color = new Color("#7CFC00"); | |
| public static LemonChiffon: Color = new Color("#FFFACD"); | |
| public static LightBlue: Color = new Color("#ADD8E6"); | |
| public static LightCoral: Color = new Color("#F08080"); | |
| public static LightCyan: Color = new Color("#E0FFFF"); | |
| public static LightGoldenRodYellow: Color = new Color("#FAFAD2"); | |
| public static LightGray: Color = new Color("#D3D3D3"); | |
| public static LightGreen: Color = new Color("#90EE90"); | |
| public static LightPink: Color = new Color("#FFB6C1"); | |
| public static LightSalmon: Color = new Color("#FFA07A"); | |
| public static LightSeaGreen: Color = new Color("#20B2AA"); | |
| public static LightSkyBlue: Color = new Color("#87CEFA"); | |
| public static LightSlateGray: Color = new Color("#778899"); | |
| public static LightSteelBlue: Color = new Color("#B0C4DE"); | |
| public static LightYellow: Color = new Color("#FFFFE0"); | |
| public static Lime: Color = new Color("#00FF00"); | |
| public static LimeGreen: Color = new Color("#32CD32"); | |
| public static Linen: Color = new Color("#FAF0E6"); | |
| public static Magenta: Color = new Color("#FF00FF"); | |
| public static Maroon: Color = new Color("#800000"); | |
| public static MediumAquaMarine: Color = new Color("#66CDAA"); | |
| public static MediumBlue: Color = new Color("#0000CD"); | |
| public static MediumOrchid: Color = new Color("#BA55D3"); | |
| public static MediumPurple: Color = new Color("#9370DB"); | |
| public static MediumSeaGreen: Color = new Color("#3CB371"); | |
| public static MediumSlateBlue: Color = new Color("#7B68EE"); | |
| public static MediumSpringGreen: Color = new Color("#00FA9A"); | |
| public static MediumTurquoise: Color = new Color("#48D1CC"); | |
| public static MediumVioletRed: Color = new Color("#C71585"); | |
| public static MidnightBlue: Color = new Color("#191970"); | |
| public static MintCream: Color = new Color("#F5FFFA"); | |
| public static MistyRose: Color = new Color("#FFE4E1"); | |
| public static Moccasin: Color = new Color("#FFE4B5"); | |
| public static NavajoWhite: Color = new Color("#FFDEAD"); | |
| public static Navy: Color = new Color("#000080"); | |
| public static OldLace: Color = new Color("#FDF5E6"); | |
| public static Olive: Color = new Color("#808000"); | |
| public static OliveDrab: Color = new Color("#6B8E23"); | |
| public static Orange: Color = new Color("#FFA500"); | |
| public static OrangeRed: Color = new Color("#FF4500"); | |
| public static Orchid: Color = new Color("#DA70D6"); | |
| public static PaleGoldenRod: Color = new Color("#EEE8AA"); | |
| public static PaleGreen: Color = new Color("#98FB98"); | |
| public static PaleTurquoise: Color = new Color("#AFEEEE"); | |
| public static PaleVioletRed: Color = new Color("#DB7093"); | |
| public static PapayaWhip: Color = new Color("#FFEFD5"); | |
| public static PeachPuff: Color = new Color("#FFDAB9"); | |
| public static Peru: Color = new Color("#CD853F"); | |
| public static Pink: Color = new Color("#FFC0CB"); | |
| public static Plum: Color = new Color("#DDA0DD"); | |
| public static PowderBlue: Color = new Color("#B0E0E6"); | |
| public static Purple: Color = new Color("#800080"); | |
| public static Red: Color = new Color("#FF0000"); | |
| public static RosyBrown: Color = new Color("#BC8F8F"); | |
| public static RoyalBlue: Color = new Color("#4169E1"); | |
| public static SaddleBrown: Color = new Color("#8B4513"); | |
| public static Salmon: Color = new Color("#FA8072"); | |
| public static SandyBrown: Color = new Color("#F4A460"); | |
| public static SeaGreen: Color = new Color("#2E8B57"); | |
| public static SeaShell: Color = new Color("#FFF5EE"); | |
| public static Sienna: Color = new Color("#A0522D"); | |
| public static Silver: Color = new Color("#C0C0C0"); | |
| public static SkyBlue: Color = new Color("#87CEEB"); | |
| public static SlateBlue: Color = new Color("#6A5ACD"); | |
| public static SlateGray: Color = new Color("#708090"); | |
| public static Snow: Color = new Color("#FFFAFA"); | |
| public static SpringGreen: Color = new Color("#00FF7F"); | |
| public static SteelBlue: Color = new Color("#4682B4"); | |
| public static Tan: Color = new Color("#D2B48C"); | |
| public static Teal: Color = new Color("#008080"); | |
| public static Thistle: Color = new Color("#D8BFD8"); | |
| public static Tomato: Color = new Color("#FF6347"); | |
| public static Turquoise: Color = new Color("#40E0D0"); | |
| public static Violet: Color = new Color("#EE82EE"); | |
| public static Wheat: Color = new Color("#F5DEB3"); | |
| public static White: Color = new Color("#FFFFFF"); | |
| public static WhiteSmoke: Color = new Color("#F5F5F5"); | |
| public static Yellow: Color = new Color("#FFFF00"); | |
| public static YellowGreen: Color = new Color("#9ACD32"); | |
| constructor(); | |
| constructor(color: string); | |
| constructor(r: number, g: number, b: number); | |
| constructor(a: number, r: number, g: number, b: number); | |
| constructor(a?: any, r?: number, g?: number, b?: number) { | |
| //get all params | |
| if (a && typeof (a) === 'string') { | |
| this.initializeColor(a); | |
| } else { | |
| //a is a number? | |
| if (b !== undefined) { | |
| // we have all 4 | |
| this.A = a; | |
| this.R = r; | |
| this.G = g; | |
| this.B = b; | |
| } else { | |
| // we have only 3 colors assume rgb | |
| this.A = 1; | |
| this.R = a; | |
| this.G = r; | |
| this.B = g; | |
| } | |
| } | |
| } | |
| public ToString(): string { | |
| return 'argb(' | |
| + this.A | |
| + ',' | |
| + this.R | |
| + ',' | |
| + this.G | |
| + ',' | |
| + this.B | |
| + ')'; | |
| } | |
| private initializeColor(color: string): void { | |
| //rgb, hex, rgba, argb | |
| color = this.namedColorToHex(color); | |
| var result: Color = this.convertStringToColor(color); | |
| this.A = result.A; | |
| this.B = result.B; | |
| this.R = result.R; | |
| this.G = result.G; | |
| } | |
| private convertStringToColor(hex: string): Color { | |
| if (hex.charAt(0) == '#') { | |
| hex = hex.substr(1); | |
| } | |
| //check the length to determine type | |
| if (hex.length == 3) { | |
| var shortHex = /^([a-f\d])([a-f\d])([a-f\d])$/i; | |
| hex = hex.replace(shortHex, function(m, r, g, b) { | |
| return r + r + g + g + b + b; | |
| }); | |
| } | |
| if (hex.length == 4) { | |
| //short alpha | |
| var shortHex: RegExp = /^([a-f\d])([a-f\d])([a-f\d])([a-f\d])$/i; | |
| hex = hex.replace(shortHex, function(m, a, r, g, b) { | |
| return a+a+r + r + g + g + b + b; | |
| }); | |
| } | |
| if (hex.length == 6) { | |
| return this.parseHex(hex); | |
| } | |
| if (hex.length == 8) { | |
| return this.parseAlphaHex(hex); | |
| } | |
| return this.parseRGB(hex); | |
| } | |
| private parseRGB(rgb: string): Color { | |
| var regex: RegExp = /^(argb|rgb|rgba)\s*\(\s*([\d]{0,3})\s*,\s*([\d]{0,3})\s*,\s*([\d]{0,3})\s*(?:,\s*(\d{0,3})\s*)?\s*\)$/i; | |
| var result = regex.exec(rgb); | |
| if (result) { | |
| var name = result[1]; | |
| switch(name) { | |
| case 'rgb': | |
| return new Color(parseInt(result[2]), parseInt(result[3]), parseInt(result[4])); | |
| case 'argb': | |
| return new Color(parseInt(result[2]), parseInt(result[3]), parseInt(result[4]), parseInt(result[5])); | |
| case 'rgba': | |
| return new Color(parseInt(result[5]), parseInt(result[2]), parseInt(result[3]), parseInt(result[4])); | |
| } | |
| } | |
| return new Color(0, 0, 0); | |
| } | |
| private parseAlphaHex(hex: string): Color { | |
| var a: number, | |
| r: number, | |
| g: number, | |
| b: number; | |
| a = parseInt(hex.charAt(0) + hex.charAt(1), 16); | |
| r = parseInt(hex.charAt(2) + hex.charAt(3), 16); | |
| g = parseInt(hex.charAt(4) + hex.charAt(5), 16); | |
| b = parseInt(hex.charAt(6) + hex.charAt(7), 16); | |
| return new Color(a, r, g, b); | |
| } | |
| private parseHex(hex: string): Color { | |
| var r: number, | |
| g: number, | |
| b: number; | |
| r = parseInt(hex.charAt(0) + hex.charAt(1), 16); | |
| g = parseInt(hex.charAt(2) + hex.charAt(3), 16); | |
| b = parseInt(hex.charAt(4) + hex.charAt(5), 16); | |
| return new Color(r, g, b); | |
| } | |
| private namedColorToHex(color: string) : string { | |
| var colors = { | |
| "aliceblue": "#f0f8ff", | |
| "antiquewhite": "#faebd7", | |
| "aqua": "#00ffff", | |
| "aquamarine": "#7fffd4", | |
| "azure": "#f0ffff", | |
| "beige": "#f5f5dc", | |
| "bisque": "#ffe4c4", | |
| "black": "#000000", | |
| "blanchedalmond": "#ffebcd", | |
| "blue": "#0000ff", | |
| "blueviolet": "#8a2be2", | |
| "brown": "#a52a2a", | |
| "burlywood": "#deb887", | |
| "cadetblue": "#5f9ea0", | |
| "chartreuse": "#7fff00", | |
| "chocolate": "#d2691e", | |
| "coral": "#ff7f50", | |
| "cornflowerblue": "#6495ed", | |
| "cornsilk": "#fff8dc", | |
| "crimson": "#dc143c", | |
| "cyan": "#00ffff", | |
| "darkblue": "#00008b", | |
| "darkcyan": "#008b8b", | |
| "darkgoldenrod": "#b8860b", | |
| "darkgray": "#a9a9a9", | |
| "darkgreen": "#006400", | |
| "darkkhaki": "#bdb76b", | |
| "darkmagenta": "#8b008b", | |
| "darkolivegreen": "#556b2f", | |
| "darkorange": "#ff8c00", | |
| "darkorchid": "#9932cc", | |
| "darkred": "#8b0000", | |
| "darksalmon": "#e9967a", | |
| "darkseagreen": "#8fbc8f", | |
| "darkslateblue": "#483d8b", | |
| "darkslategray": "#2f4f4f", | |
| "darkturquoise": "#00ced1", | |
| "darkviolet": "#9400d3", | |
| "deeppink": "#ff1493", | |
| "deepskyblue": "#00bfff", | |
| "dimgray": "#696969", | |
| "dodgerblue": "#1e90ff", | |
| "firebrick": "#b22222", | |
| "floralwhite": "#fffaf0", | |
| "forestgreen": "#228b22", | |
| "fuchsia": "#ff00ff", | |
| "gainsboro": "#dcdcdc", | |
| "ghostwhite": "#f8f8ff", | |
| "gold": "#ffd700", | |
| "goldenrod": "#daa520", | |
| "gray": "#808080", | |
| "green": "#008000", | |
| "greenyellow": "#adff2f", | |
| "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", | |
| "lightgrey": "#d3d3d3", | |
| "lightgreen": "#90ee90", | |
| "lightpink": "#ffb6c1", | |
| "lightsalmon": "#ffa07a", | |
| "lightseagreen": "#20b2aa", | |
| "lightskyblue": "#87cefa", | |
| "lightslategray": "#778899", | |
| "lightsteelblue": "#b0c4de", | |
| "lightyellow": "#ffffe0", | |
| "lime": "#00ff00", | |
| "limegreen": "#32cd32", | |
| "linen": "#faf0e6", | |
| "magenta": "#ff00ff", | |
| "maroon": "#800000", | |
| "mediumaquamarine": "#66cdaa", | |
| "mediumblue": "#0000cd", | |
| "mediumorchid": "#ba55d3", | |
| "mediumpurple": "#9370d8", | |
| "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": "#d87093", | |
| "papayawhip": "#ffefd5", | |
| "peachpuff": "#ffdab9", | |
| "peru": "#cd853f", | |
| "pink": "#ffc0cb", | |
| "plum": "#dda0dd", | |
| "powderblue": "#b0e0e6", | |
| "purple": "#800080", | |
| "red": "#ff0000", | |
| "rosybrown": "#bc8f8f", | |
| "royalblue": "#4169e1", | |
| "saddlebrown": "#8b4513", | |
| "salmon": "#fa8072", | |
| "sandybrown": "#f4a460", | |
| "seagreen": "#2e8b57", | |
| "seashell": "#fff5ee", | |
| "sienna": "#a0522d", | |
| "silver": "#c0c0c0", | |
| "skyblue": "#87ceeb", | |
| "slateblue": "#6a5acd", | |
| "slategray": "#708090", | |
| "snow": "#fffafa", | |
| "springgreen": "#00ff7f", | |
| "steelblue": "#4682b4", | |
| "tan": "#d2b48c", | |
| "teal": "#008080", | |
| "thistle": "#d8bfd8", | |
| "tomato": "#ff6347", | |
| "turquoise": "#40e0d0", | |
| "violet": "#ee82ee", | |
| "wheat": "#f5deb3", | |
| "white": "#ffffff", | |
| "whitesmoke": "#f5f5f5", | |
| "yellow": "#ffff00", | |
| "yellowgreen": "#9acd32" | |
| }; | |
| if (typeof colors[color.toLowerCase()] != 'undefined') return colors[color.toLowerCase()]; | |
| return color; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment