UNPKG

sigma

Version:

A JavaScript library aimed at visualizing graphs of thousands of nodes and edges.

360 lines (342 loc) 10.1 kB
'use strict'; function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } var HTML_COLORS = { black: "#000000", silver: "#C0C0C0", gray: "#808080", grey: "#808080", white: "#FFFFFF", maroon: "#800000", red: "#FF0000", purple: "#800080", fuchsia: "#FF00FF", green: "#008000", lime: "#00FF00", olive: "#808000", yellow: "#FFFF00", navy: "#000080", blue: "#0000FF", teal: "#008080", aqua: "#00FFFF", darkblue: "#00008B", mediumblue: "#0000CD", darkgreen: "#006400", darkcyan: "#008B8B", deepskyblue: "#00BFFF", darkturquoise: "#00CED1", mediumspringgreen: "#00FA9A", springgreen: "#00FF7F", cyan: "#00FFFF", midnightblue: "#191970", dodgerblue: "#1E90FF", lightseagreen: "#20B2AA", forestgreen: "#228B22", seagreen: "#2E8B57", darkslategray: "#2F4F4F", darkslategrey: "#2F4F4F", limegreen: "#32CD32", mediumseagreen: "#3CB371", turquoise: "#40E0D0", royalblue: "#4169E1", steelblue: "#4682B4", darkslateblue: "#483D8B", mediumturquoise: "#48D1CC", indigo: "#4B0082", darkolivegreen: "#556B2F", cadetblue: "#5F9EA0", cornflowerblue: "#6495ED", rebeccapurple: "#663399", mediumaquamarine: "#66CDAA", dimgray: "#696969", dimgrey: "#696969", slateblue: "#6A5ACD", olivedrab: "#6B8E23", slategray: "#708090", slategrey: "#708090", lightslategray: "#778899", lightslategrey: "#778899", mediumslateblue: "#7B68EE", lawngreen: "#7CFC00", chartreuse: "#7FFF00", aquamarine: "#7FFFD4", skyblue: "#87CEEB", lightskyblue: "#87CEFA", blueviolet: "#8A2BE2", darkred: "#8B0000", darkmagenta: "#8B008B", saddlebrown: "#8B4513", darkseagreen: "#8FBC8F", lightgreen: "#90EE90", mediumpurple: "#9370DB", darkviolet: "#9400D3", palegreen: "#98FB98", darkorchid: "#9932CC", yellowgreen: "#9ACD32", sienna: "#A0522D", brown: "#A52A2A", darkgray: "#A9A9A9", darkgrey: "#A9A9A9", lightblue: "#ADD8E6", greenyellow: "#ADFF2F", paleturquoise: "#AFEEEE", lightsteelblue: "#B0C4DE", powderblue: "#B0E0E6", firebrick: "#B22222", darkgoldenrod: "#B8860B", mediumorchid: "#BA55D3", rosybrown: "#BC8F8F", darkkhaki: "#BDB76B", mediumvioletred: "#C71585", indianred: "#CD5C5C", peru: "#CD853F", chocolate: "#D2691E", tan: "#D2B48C", lightgray: "#D3D3D3", lightgrey: "#D3D3D3", thistle: "#D8BFD8", orchid: "#DA70D6", goldenrod: "#DAA520", palevioletred: "#DB7093", crimson: "#DC143C", gainsboro: "#DCDCDC", plum: "#DDA0DD", burlywood: "#DEB887", lightcyan: "#E0FFFF", lavender: "#E6E6FA", darksalmon: "#E9967A", violet: "#EE82EE", palegoldenrod: "#EEE8AA", lightcoral: "#F08080", khaki: "#F0E68C", aliceblue: "#F0F8FF", honeydew: "#F0FFF0", azure: "#F0FFFF", sandybrown: "#F4A460", wheat: "#F5DEB3", beige: "#F5F5DC", whitesmoke: "#F5F5F5", mintcream: "#F5FFFA", ghostwhite: "#F8F8FF", salmon: "#FA8072", antiquewhite: "#FAEBD7", linen: "#FAF0E6", lightgoldenrodyellow: "#FAFAD2", oldlace: "#FDF5E6", magenta: "#FF00FF", deeppink: "#FF1493", orangered: "#FF4500", tomato: "#FF6347", hotpink: "#FF69B4", coral: "#FF7F50", darkorange: "#FF8C00", lightsalmon: "#FFA07A", orange: "#FFA500", lightpink: "#FFB6C1", pink: "#FFC0CB", gold: "#FFD700", peachpuff: "#FFDAB9", navajowhite: "#FFDEAD", moccasin: "#FFE4B5", bisque: "#FFE4C4", mistyrose: "#FFE4E1", blanchedalmond: "#FFEBCD", papayawhip: "#FFEFD5", lavenderblush: "#FFF0F5", seashell: "#FFF5EE", cornsilk: "#FFF8DC", lemonchiffon: "#FFFACD", floralwhite: "#FFFAF0", snow: "#FFFAFA", lightyellow: "#FFFFE0", ivory: "#FFFFF0" }; /** * Function extracting the color at the given pixel. */ function extractPixel(gl, x, y, array) { var data = array || new Uint8Array(4); gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, data); return data; } /** * Memoized function returning a float-encoded color from various string * formats describing colors. */ var INT8 = new Int8Array(4); var INT32 = new Int32Array(INT8.buffer, 0, 1); var FLOAT32 = new Float32Array(INT8.buffer, 0, 1); var RGBA_TEST_REGEX = /^\s*rgba?\s*\(/; var RGBA_EXTRACT_REGEX = /^\s*rgba?\s*\(\s*([0-9]*)\s*,\s*([0-9]*)\s*,\s*([0-9]*)(?:\s*,\s*(.*)?)?\)\s*$/; function parseColor(val) { var r = 0; // byte var g = 0; // byte var b = 0; // byte var a = 1; // float // Handling hexadecimal notation if (val[0] === "#") { if (val.length === 4) { r = parseInt(val.charAt(1) + val.charAt(1), 16); g = parseInt(val.charAt(2) + val.charAt(2), 16); b = parseInt(val.charAt(3) + val.charAt(3), 16); } else { r = parseInt(val.charAt(1) + val.charAt(2), 16); g = parseInt(val.charAt(3) + val.charAt(4), 16); b = parseInt(val.charAt(5) + val.charAt(6), 16); } if (val.length === 9) { a = parseInt(val.charAt(7) + val.charAt(8), 16) / 255; } } // Handling rgb notation else if (RGBA_TEST_REGEX.test(val)) { var match = val.match(RGBA_EXTRACT_REGEX); if (match) { r = +match[1]; g = +match[2]; b = +match[3]; if (match[4]) a = +match[4]; } } return { r: r, g: g, b: b, a: a }; } var FLOAT_COLOR_CACHE = {}; for (var htmlColor in HTML_COLORS) { FLOAT_COLOR_CACHE[htmlColor] = floatColor(HTML_COLORS[htmlColor]); // Replicating cache for hex values for free FLOAT_COLOR_CACHE[HTML_COLORS[htmlColor]] = FLOAT_COLOR_CACHE[htmlColor]; } function rgbaToFloat(r, g, b, a, masking) { INT32[0] = a << 24 | b << 16 | g << 8 | r; if (masking) INT32[0] = INT32[0] & 0xfeffffff; return FLOAT32[0]; } function floatColor(val) { // The html color names are case-insensitive val = val.toLowerCase(); // If the color is already computed, we yield it if (typeof FLOAT_COLOR_CACHE[val] !== "undefined") return FLOAT_COLOR_CACHE[val]; var parsed = parseColor(val); var r = parsed.r, g = parsed.g, b = parsed.b; var a = parsed.a; a = a * 255 | 0; var color = rgbaToFloat(r, g, b, a, true); FLOAT_COLOR_CACHE[val] = color; return color; } function colorToArray(val, masking) { FLOAT32[0] = floatColor(val); var intValue = INT32[0]; if (masking) { intValue = intValue | 0x01000000; } var r = intValue & 0xff; var g = intValue >> 8 & 0xff; var b = intValue >> 16 & 0xff; var a = intValue >> 24 & 0xff; return [r, g, b, a]; } var FLOAT_INDEX_CACHE = {}; function indexToColor(index) { // If the index is already computed, we yield it if (typeof FLOAT_INDEX_CACHE[index] !== "undefined") return FLOAT_INDEX_CACHE[index]; // To address issue #1397, one strategy is to keep encoding 4 bytes colors, // but with alpha hard-set to 1.0 (or 255): var r = (index & 0x00ff0000) >>> 16; var g = (index & 0x0000ff00) >>> 8; var b = index & 0x000000ff; var a = 0x000000ff; // The original 4 bytes color encoding was the following: // const r = (index & 0xff000000) >>> 24; // const g = (index & 0x00ff0000) >>> 16; // const b = (index & 0x0000ff00) >>> 8; // const a = index & 0x000000ff; var color = rgbaToFloat(r, g, b, a, true); FLOAT_INDEX_CACHE[index] = color; return color; } function colorToIndex(r, g, b, _a) { // As for the function indexToColor, because of #1397 and the "alpha is always // 1.0" strategy, we need to fix this function as well: return b + (g << 8) + (r << 16); // The original 4 bytes color decoding is the following: // return a + (b << 8) + (g << 16) + (r << 24); } function getPixelColor(gl, frameBuffer, x, y, pixelRatio, downSizingRatio) { var bufferX = Math.floor(x / downSizingRatio * pixelRatio); var bufferY = Math.floor(gl.drawingBufferHeight / downSizingRatio - y / downSizingRatio * pixelRatio); var pixel = new Uint8Array(4); gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer); gl.readPixels(bufferX, bufferY, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel); var _pixel = _slicedToArray(pixel, 4), r = _pixel[0], g = _pixel[1], b = _pixel[2], a = _pixel[3]; return [r, g, b, a]; } exports.HTML_COLORS = HTML_COLORS; exports._arrayLikeToArray = _arrayLikeToArray; exports._slicedToArray = _slicedToArray; exports._unsupportedIterableToArray = _unsupportedIterableToArray; exports.colorToArray = colorToArray; exports.colorToIndex = colorToIndex; exports.extractPixel = extractPixel; exports.floatColor = floatColor; exports.getPixelColor = getPixelColor; exports.indexToColor = indexToColor; exports.parseColor = parseColor; exports.rgbaToFloat = rgbaToFloat;