stringman
Version:
Stringman does string manipulation and other string operations. Do anything from lightening color codes to swapping email address in a string!
129 lines (128 loc) • 3.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.colors = void 0;
const numbers_1 = require("./numbers");
const common_1 = require("./utility/common");
function rgbToHex(r, g, b) {
return '#' + numbers_1.numbers.convertToHex(r) + numbers_1.numbers.convertToHex(g) + numbers_1.numbers.convertToHex(b);
}
function isHex(color) {
if (typeof color !== 'string') {
return false;
}
const colorCleaned = color.charAt(0) === '#' ? color.slice(1) : color;
return common_1.common.isValid(colorCleaned, /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/);
}
function hexToRgb(hex) {
if (hex.charAt(0) === '#') {
hex = hex.substr(1);
}
if (hex.length < 2 || hex.length > 6) {
return null;
}
const values = hex.split('');
let r;
let g;
let b;
if (hex.length === 2) {
r = parseInt(values[0].toString() + values[1].toString(), 16);
g = r;
b = r;
}
else if (hex.length === 3) {
r = parseInt(values[0].toString() + values[0].toString(), 16);
g = parseInt(values[1].toString() + values[1].toString(), 16);
b = parseInt(values[2].toString() + values[2].toString(), 16);
}
else if (hex.length === 6) {
r = parseInt(values[0].toString() + values[1].toString(), 16);
g = parseInt(values[2].toString() + values[3].toString(), 16);
b = parseInt(values[4].toString() + values[5].toString(), 16);
}
else {
return null;
}
return [r, g, b];
}
function luminance(color, percent) {
if (!isHex(color)) {
return null;
}
const Rhex = parseInt(color.substring(1, 3), 16);
const Ghex = parseInt(color.substring(3, 5), 16);
const Bhex = parseInt(color.substring(5, 7), 16);
const Rcon = (Rhex * (100 + percent)) / 100;
const Gcon = (Ghex * (100 + percent)) / 100;
const Bcon = (Bhex * (100 + percent)) / 100;
const R = Rcon < 255 ? parseInt(Rcon.toString(), 10) : 255;
const G = Gcon < 255 ? parseInt(Gcon.toString(), 10) : 255;
const B = Bcon < 255 ? parseInt(Bcon.toString(), 10) : 255;
const RR = R.toString(16).length === 1 ? '0' + R.toString(16) : R.toString(16);
const GG = G.toString(16).length === 1 ? '0' + G.toString(16) : G.toString(16);
const BB = B.toString(16).length === 1 ? '0' + B.toString(16) : B.toString(16);
return '#' + RR + GG + BB;
}
function hexToHsl(str) {
if (isHex(str)) {
const hex = str.charAt(0) === '#' ? str.slice(1) : str;
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
if (result && result.length) {
const r = parseInt(result[1], 16);
const g = parseInt(result[2], 16);
const b = parseInt(result[3], 16);
return rgbToHsl(r, g, b);
}
else {
return null;
}
}
else {
return null;
}
}
function rgbToHsl(r, g, b) {
if (typeof r !== 'number' || typeof g !== 'number' || typeof b !== 'number') {
return null;
}
r /= 255;
g /= 255;
b /= 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
let h;
let s;
const l = (max + min) / 2;
if (max === min) {
h = s = 0;
}
else {
const 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 = h ? h / 6 : 0;
}
return {
h: parseFloat((h * 360).toFixed(1)),
l: parseFloat((l * 100).toFixed(1)),
s: parseFloat((s * 100).toFixed(1))
};
}
const colors = {
hexToHsl,
hexToRgb,
isHex,
luminance,
rgbToHex,
rgbToHsl
};
exports.colors = colors;