|
| 1 | +// This file was taken from the cytoscape.js style parser |
| 2 | + |
| 3 | +const number = '(?:[-+]?(?:(?:\\d+|\\d*\\.\\d+)(?:[Ee][+-]?\\d+)?))'; |
| 4 | +const rgba = 'rgb[a]?\\((' + number + '[%]?)\\s*,\\s*(' + number + '[%]?)\\s*,\\s*(' + number + '[%]?)(?:\\s*,\\s*(' + number + '))?\\)'; |
| 5 | +const rgbaNoBackRefs = 'rgb[a]?\\((?:' + number + '[%]?)\\s*,\\s*(?:' + number + '[%]?)\\s*,\\s*(?:' + number + '[%]?)(?:\\s*,\\s*(?:' + number + '))?\\)'; |
| 6 | +const hsla = 'hsl[a]?\\((' + number + ')\\s*,\\s*(' + number + '[%])\\s*,\\s*(' + number + '[%])(?:\\s*,\\s*(' + number + '))?\\)'; |
| 7 | +const hslaNoBackRefs = 'hsl[a]?\\((?:' + number + ')\\s*,\\s*(?:' + number + '[%])\\s*,\\s*(?:' + number + '[%])(?:\\s*,\\s*(?:' + number + '))?\\)'; |
| 8 | +const hex3 = '\\#[0-9a-fA-F]{3}'; |
| 9 | +const hex6 = '\\#[0-9a-fA-F]{6}'; |
| 10 | + |
| 11 | + |
| 12 | + // get [r, g, b] from #abc or #aabbcc |
| 13 | +export const hex2tuple = hex => { |
| 14 | + if( !(hex.length === 4 || hex.length === 7) || hex[0] !== '#' ){ return; } |
| 15 | + |
| 16 | + let shortHex = hex.length === 4; |
| 17 | + let r, g, b; |
| 18 | + let base = 16; |
| 19 | + |
| 20 | + if( shortHex ){ |
| 21 | + r = parseInt( hex[1] + hex[1], base ); |
| 22 | + g = parseInt( hex[2] + hex[2], base ); |
| 23 | + b = parseInt( hex[3] + hex[3], base ); |
| 24 | + } else { |
| 25 | + r = parseInt( hex[1] + hex[2], base ); |
| 26 | + g = parseInt( hex[3] + hex[4], base ); |
| 27 | + b = parseInt( hex[5] + hex[6], base ); |
| 28 | + } |
| 29 | + |
| 30 | + return [ r, g, b ]; |
| 31 | +}; |
| 32 | + |
| 33 | + // get [r, g, b, a] from hsl(0, 0, 0) or hsla(0, 0, 0, 0) |
| 34 | +export const hsl2tuple = hsl => { |
| 35 | + let ret; |
| 36 | + let h, s, l, a, r, g, b; |
| 37 | + function hue2rgb( p, q, t ){ |
| 38 | + if( t < 0 ) t += 1; |
| 39 | + if( t > 1 ) t -= 1; |
| 40 | + if( t < 1 / 6 ) return p + (q - p) * 6 * t; |
| 41 | + if( t < 1 / 2 ) return q; |
| 42 | + if( t < 2 / 3 ) return p + (q - p) * (2 / 3 - t) * 6; |
| 43 | + return p; |
| 44 | + } |
| 45 | + |
| 46 | + let m = new RegExp( '^' + hsla + '$' ).exec( hsl ); |
| 47 | + if( m ){ |
| 48 | + |
| 49 | + // get hue |
| 50 | + h = parseInt( m[1] ); |
| 51 | + if( h < 0 ){ |
| 52 | + h = ( 360 - (-1 * h % 360) ) % 360; |
| 53 | + } else if( h > 360 ){ |
| 54 | + h = h % 360; |
| 55 | + } |
| 56 | + h /= 360; // normalise on [0, 1] |
| 57 | + |
| 58 | + s = parseFloat( m[2] ); |
| 59 | + if( s < 0 || s > 100 ){ return; } // saturation is [0, 100] |
| 60 | + s = s / 100; // normalise on [0, 1] |
| 61 | + |
| 62 | + l = parseFloat( m[3] ); |
| 63 | + if( l < 0 || l > 100 ){ return; } // lightness is [0, 100] |
| 64 | + l = l / 100; // normalise on [0, 1] |
| 65 | + |
| 66 | + a = m[4]; |
| 67 | + if( a !== undefined ){ |
| 68 | + a = parseFloat( a ); |
| 69 | + |
| 70 | + if( a < 0 || a > 1 ){ return; } // alpha is [0, 1] |
| 71 | + } |
| 72 | + |
| 73 | + // now, convert to rgb |
| 74 | + // code from http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript |
| 75 | + if( s === 0 ){ |
| 76 | + r = g = b = Math.round( l * 255 ); // achromatic |
| 77 | + } else { |
| 78 | + let q = l < 0.5 ? l * (1 + s) : l + s - l * s; |
| 79 | + let p = 2 * l - q; |
| 80 | + r = Math.round( 255 * hue2rgb( p, q, h + 1 / 3 ) ); |
| 81 | + g = Math.round( 255 * hue2rgb( p, q, h ) ); |
| 82 | + b = Math.round( 255 * hue2rgb( p, q, h - 1 / 3 ) ); |
| 83 | + } |
| 84 | + |
| 85 | + ret = [ r, g, b, a ]; |
| 86 | + } |
| 87 | + |
| 88 | + return ret; |
| 89 | +}; |
| 90 | + |
| 91 | +// get [r, g, b, a] from rgb(0, 0, 0) or rgba(0, 0, 0, 0) |
| 92 | +export const rgb2tuple = rgb => { |
| 93 | + let ret; |
| 94 | + |
| 95 | + let m = new RegExp( '^' + rgba + '$' ).exec( rgb ); |
| 96 | + if( m ){ |
| 97 | + ret = []; |
| 98 | + |
| 99 | + let isPct = []; |
| 100 | + for( let i = 1; i <= 3; i++ ){ |
| 101 | + let channel = m[ i ]; |
| 102 | + |
| 103 | + if( channel[ channel.length - 1 ] === '%' ){ |
| 104 | + isPct[ i ] = true; |
| 105 | + } |
| 106 | + channel = parseFloat( channel ); |
| 107 | + |
| 108 | + if( isPct[ i ] ){ |
| 109 | + channel = channel / 100 * 255; // normalise to [0, 255] |
| 110 | + } |
| 111 | + |
| 112 | + if( channel < 0 || channel > 255 ){ return; } // invalid channel value |
| 113 | + |
| 114 | + ret.push( Math.floor( channel ) ); |
| 115 | + } |
| 116 | + |
| 117 | + let atLeastOneIsPct = isPct[1] || isPct[2] || isPct[3]; |
| 118 | + let allArePct = isPct[1] && isPct[2] && isPct[3]; |
| 119 | + if( atLeastOneIsPct && !allArePct ){ return; } // must all be percent values if one is |
| 120 | + |
| 121 | + let alpha = m[4]; |
| 122 | + if( alpha !== undefined ){ |
| 123 | + alpha = parseFloat( alpha ); |
| 124 | + |
| 125 | + if( alpha < 0 || alpha > 1 ){ return; } // invalid alpha value |
| 126 | + |
| 127 | + ret.push( alpha ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return ret; |
| 132 | +}; |
| 133 | + |
| 134 | +export const colorname2tuple = color => { |
| 135 | + return colors[ color.toLowerCase() ]; |
| 136 | +}; |
| 137 | + |
| 138 | +export const color2tuple = color => { |
| 139 | + return ( Array.isArray( color ) ? color : null ) |
| 140 | + || colorname2tuple( color ) |
| 141 | + || hex2tuple( color ) |
| 142 | + || rgb2tuple( color ) |
| 143 | + || hsl2tuple( color ); |
| 144 | +}; |
| 145 | + |
| 146 | +export const colors = { |
| 147 | + // special colour names |
| 148 | + transparent: [0, 0, 0, 0], // NB alpha === 0 |
| 149 | + |
| 150 | + // regular colours |
| 151 | + aliceblue: [ 240, 248, 255 ], |
| 152 | + antiquewhite: [ 250, 235, 215 ], |
| 153 | + aqua: [0, 255, 255 ], |
| 154 | + aquamarine: [ 127, 255, 212 ], |
| 155 | + azure: [ 240, 255, 255 ], |
| 156 | + beige: [ 245, 245, 220 ], |
| 157 | + bisque: [ 255, 228, 196 ], |
| 158 | + black: [0, 0, 0], |
| 159 | + blanchedalmond: [ 255, 235, 205 ], |
| 160 | + blue: [0, 0, 255 ], |
| 161 | + blueviolet: [ 138, 43, 226 ], |
| 162 | + brown: [ 165, 42, 42 ], |
| 163 | + burlywood: [ 222, 184, 135 ], |
| 164 | + cadetblue: [ 95, 158, 160 ], |
| 165 | + chartreuse: [ 127, 255, 0], |
| 166 | + chocolate: [ 210, 105, 30 ], |
| 167 | + coral: [ 255, 127, 80 ], |
| 168 | + cornflowerblue: [ 100, 149, 237 ], |
| 169 | + cornsilk: [ 255, 248, 220 ], |
| 170 | + crimson: [ 220, 20, 60 ], |
| 171 | + cyan: [0, 255, 255 ], |
| 172 | + darkblue: [0, 0, 139 ], |
| 173 | + darkcyan: [0, 139, 139 ], |
| 174 | + darkgoldenrod: [ 184, 134, 11 ], |
| 175 | + darkgray: [ 169, 169, 169 ], |
| 176 | + darkgreen: [0, 100, 0], |
| 177 | + darkgrey: [ 169, 169, 169 ], |
| 178 | + darkkhaki: [ 189, 183, 107 ], |
| 179 | + darkmagenta: [ 139, 0, 139 ], |
| 180 | + darkolivegreen: [ 85, 107, 47 ], |
| 181 | + darkorange: [ 255, 140, 0], |
| 182 | + darkorchid: [ 153, 50, 204 ], |
| 183 | + darkred: [ 139, 0, 0], |
| 184 | + darksalmon: [ 233, 150, 122 ], |
| 185 | + darkseagreen: [ 143, 188, 143 ], |
| 186 | + darkslateblue: [ 72, 61, 139 ], |
| 187 | + darkslategray: [ 47, 79, 79 ], |
| 188 | + darkslategrey: [ 47, 79, 79 ], |
| 189 | + darkturquoise: [0, 206, 209 ], |
| 190 | + darkviolet: [ 148, 0, 211 ], |
| 191 | + deeppink: [ 255, 20, 147 ], |
| 192 | + deepskyblue: [0, 191, 255 ], |
| 193 | + dimgray: [ 105, 105, 105 ], |
| 194 | + dimgrey: [ 105, 105, 105 ], |
| 195 | + dodgerblue: [ 30, 144, 255 ], |
| 196 | + firebrick: [ 178, 34, 34 ], |
| 197 | + floralwhite: [ 255, 250, 240 ], |
| 198 | + forestgreen: [ 34, 139, 34 ], |
| 199 | + fuchsia: [ 255, 0, 255 ], |
| 200 | + gainsboro: [ 220, 220, 220 ], |
| 201 | + ghostwhite: [ 248, 248, 255 ], |
| 202 | + gold: [ 255, 215, 0], |
| 203 | + goldenrod: [ 218, 165, 32 ], |
| 204 | + gray: [ 128, 128, 128 ], |
| 205 | + grey: [ 128, 128, 128 ], |
| 206 | + green: [0, 128, 0], |
| 207 | + greenyellow: [ 173, 255, 47 ], |
| 208 | + honeydew: [ 240, 255, 240 ], |
| 209 | + hotpink: [ 255, 105, 180 ], |
| 210 | + indianred: [ 205, 92, 92 ], |
| 211 | + indigo: [ 75, 0, 130 ], |
| 212 | + ivory: [ 255, 255, 240 ], |
| 213 | + khaki: [ 240, 230, 140 ], |
| 214 | + lavender: [ 230, 230, 250 ], |
| 215 | + lavenderblush: [ 255, 240, 245 ], |
| 216 | + lawngreen: [ 124, 252, 0], |
| 217 | + lemonchiffon: [ 255, 250, 205 ], |
| 218 | + lightblue: [ 173, 216, 230 ], |
| 219 | + lightcoral: [ 240, 128, 128 ], |
| 220 | + lightcyan: [ 224, 255, 255 ], |
| 221 | + lightgoldenrodyellow: [ 250, 250, 210 ], |
| 222 | + lightgray: [ 211, 211, 211 ], |
| 223 | + lightgreen: [ 144, 238, 144 ], |
| 224 | + lightgrey: [ 211, 211, 211 ], |
| 225 | + lightpink: [ 255, 182, 193 ], |
| 226 | + lightsalmon: [ 255, 160, 122 ], |
| 227 | + lightseagreen: [ 32, 178, 170 ], |
| 228 | + lightskyblue: [ 135, 206, 250 ], |
| 229 | + lightslategray: [ 119, 136, 153 ], |
| 230 | + lightslategrey: [ 119, 136, 153 ], |
| 231 | + lightsteelblue: [ 176, 196, 222 ], |
| 232 | + lightyellow: [ 255, 255, 224 ], |
| 233 | + lime: [0, 255, 0], |
| 234 | + limegreen: [ 50, 205, 50 ], |
| 235 | + linen: [ 250, 240, 230 ], |
| 236 | + magenta: [ 255, 0, 255 ], |
| 237 | + maroon: [ 128, 0, 0], |
| 238 | + mediumaquamarine: [ 102, 205, 170 ], |
| 239 | + mediumblue: [0, 0, 205 ], |
| 240 | + mediumorchid: [ 186, 85, 211 ], |
| 241 | + mediumpurple: [ 147, 112, 219 ], |
| 242 | + mediumseagreen: [ 60, 179, 113 ], |
| 243 | + mediumslateblue: [ 123, 104, 238 ], |
| 244 | + mediumspringgreen: [0, 250, 154 ], |
| 245 | + mediumturquoise: [ 72, 209, 204 ], |
| 246 | + mediumvioletred: [ 199, 21, 133 ], |
| 247 | + midnightblue: [ 25, 25, 112 ], |
| 248 | + mintcream: [ 245, 255, 250 ], |
| 249 | + mistyrose: [ 255, 228, 225 ], |
| 250 | + moccasin: [ 255, 228, 181 ], |
| 251 | + navajowhite: [ 255, 222, 173 ], |
| 252 | + navy: [0, 0, 128 ], |
| 253 | + oldlace: [ 253, 245, 230 ], |
| 254 | + olive: [ 128, 128, 0], |
| 255 | + olivedrab: [ 107, 142, 35 ], |
| 256 | + orange: [ 255, 165, 0], |
| 257 | + orangered: [ 255, 69, 0], |
| 258 | + orchid: [ 218, 112, 214 ], |
| 259 | + palegoldenrod: [ 238, 232, 170 ], |
| 260 | + palegreen: [ 152, 251, 152 ], |
| 261 | + paleturquoise: [ 175, 238, 238 ], |
| 262 | + palevioletred: [ 219, 112, 147 ], |
| 263 | + papayawhip: [ 255, 239, 213 ], |
| 264 | + peachpuff: [ 255, 218, 185 ], |
| 265 | + peru: [ 205, 133, 63 ], |
| 266 | + pink: [ 255, 192, 203 ], |
| 267 | + plum: [ 221, 160, 221 ], |
| 268 | + powderblue: [ 176, 224, 230 ], |
| 269 | + purple: [ 128, 0, 128 ], |
| 270 | + red: [ 255, 0, 0], |
| 271 | + rosybrown: [ 188, 143, 143 ], |
| 272 | + royalblue: [ 65, 105, 225 ], |
| 273 | + saddlebrown: [ 139, 69, 19 ], |
| 274 | + salmon: [ 250, 128, 114 ], |
| 275 | + sandybrown: [ 244, 164, 96 ], |
| 276 | + seagreen: [ 46, 139, 87 ], |
| 277 | + seashell: [ 255, 245, 238 ], |
| 278 | + sienna: [ 160, 82, 45 ], |
| 279 | + silver: [ 192, 192, 192 ], |
| 280 | + skyblue: [ 135, 206, 235 ], |
| 281 | + slateblue: [ 106, 90, 205 ], |
| 282 | + slategray: [ 112, 128, 144 ], |
| 283 | + slategrey: [ 112, 128, 144 ], |
| 284 | + snow: [ 255, 250, 250 ], |
| 285 | + springgreen: [0, 255, 127 ], |
| 286 | + steelblue: [ 70, 130, 180 ], |
| 287 | + tan: [ 210, 180, 140 ], |
| 288 | + teal: [0, 128, 128 ], |
| 289 | + thistle: [ 216, 191, 216 ], |
| 290 | + tomato: [ 255, 99, 71 ], |
| 291 | + turquoise: [ 64, 224, 208 ], |
| 292 | + violet: [ 238, 130, 238 ], |
| 293 | + wheat: [ 245, 222, 179 ], |
| 294 | + white: [ 255, 255, 255 ], |
| 295 | + whitesmoke: [ 245, 245, 245 ], |
| 296 | + yellow: [ 255, 255, 0], |
| 297 | + yellowgreen: [ 154, 205, 50 ] |
| 298 | +}; |
0 commit comments