UNPKG

@aappddeevv/dynamics-client-ui

Version:

## What is it? A library to help you create great dynamics applications.

84 lines (76 loc) 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.shadeBlendConvert = shadeBlendConvert; exports.pastelColour = pastelColour; exports.stringToColour = stringToColour; /** * See: https://stackoverflow.com/questions/5560248/programmatically-lighten-or-darken-a-hex-color-or-rgb-and-blend-colors * * First arg is light, then color1, color2. Color2 means convert. * Can take a string color as input. Don't user names of colors * as "from" or "to". */ function shadeBlendConvert(p, from, to) { if (typeof p != "number" || p < -1 || p > 1 || typeof from != "string" || from[0] != 'r' && from[0] != '#' || typeof to != "string" && typeof to != "undefined") return null; //ErrorCheck if (!this.sbcRip) this.sbcRip = function (d) { var l = d.length, RGB = new Object(); if (l > 9) { d = d.split(","); if (d.length < 3 || d.length > 4) return null; //ErrorCheck RGB[0] = i(d[0].slice(4)), RGB[1] = i(d[1]), RGB[2] = i(d[2]), RGB[3] = d[3] ? parseFloat(d[3]) : -1; } else { if (l == 8 || l == 6 || l < 4) return null; //ErrorCheck if (l < 6) d = "#" + d[1] + d[1] + d[2] + d[2] + d[3] + d[3] + (l > 4 ? d[4] + "" + d[4] : ""); //3 digit d = i(d.slice(1), 16), RGB[0] = d >> 16 & 255, RGB[1] = d >> 8 & 255, RGB[2] = d & 255, RGB[3] = l == 9 || l == 5 ? r((d >> 24 & 255) / 255 * 10000) / 10000 : -1; } return RGB; }; var i = parseInt, r = Math.round, h = from.length > 9, h = typeof to == "string" ? to.length > 9 ? true : to == "c" ? !h : false : h, b = p < 0, p = b ? p * -1 : p, to = to && to != "c" ? to : b ? "#000000" : "#FFFFFF", f = this.sbcRip(from), t = this.sbcRip(to); if (!f || !t) return null; //ErrorCheck if (h) return "rgb(" + r((t[0] - f[0]) * p + f[0]) + "," + r((t[1] - f[1]) * p + f[1]) + "," + r((t[2] - f[2]) * p + f[2]) + (f[3] < 0 && t[3] < 0 ? ")" : "," + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 10000) / 10000 : t[3] < 0 ? f[3] : t[3]) + ")");else return "#" + (0x100000000 + (f[3] > -1 && t[3] > -1 ? r(((t[3] - f[3]) * p + f[3]) * 255) : t[3] > -1 ? r(t[3] * 255) : f[3] > -1 ? r(f[3] * 255) : 255) * 0x1000000 + r((t[0] - f[0]) * p + f[0]) * 0x10000 + r((t[1] - f[1]) * p + f[1]) * 0x100 + r((t[2] - f[2]) * p + f[2])).toString(16).slice(f[3] > -1 || t[3] > -1 ? 1 : 3); } function pastelColour(input_str) { //TODO: adjust base colour values below based on theme var baseRed = 128; var baseGreen = 128; var baseBlue = 128; //lazy seeded random hack to get values from 0 - 256 //for seed just take bitwise XOR of first two chars //var seed = input_str.charCodeAt(0) ^ input_str.charCodeAt(1); var seed = 0; for (var i in input_str) { seed ^= i; } var rand_1 = Math.abs(Math.sin(seed++) * 10000) % 256; var rand_2 = Math.abs(Math.sin(seed++) * 10000) % 256; var rand_3 = Math.abs(Math.sin(seed++) * 10000) % 256; //build colour var red = Math.round((rand_1 + baseRed) / 2); var green = Math.round((rand_2 + baseGreen) / 2); var blue = Math.round((rand_3 + baseBlue) / 2); return { red: red, green: green, blue: blue }; } function stringToColour(str) { var hash = 0; for (var i = 0; i < str.length; i++) { hash = str.charCodeAt(i) + ((hash << 5) - hash); } var colour = '#'; for (var i = 0; i < 3; i++) { var value = hash >> i * 8 & 0xFF; colour += ('00' + value.toString(16)).substr(-2); } return colour; } //# sourceMappingURL=colors.js.map