mirror of
https://github.com/mue/mue.git
synced 2026-07-10 05:55:17 +02:00
Merge things
This commit is contained in:
31
src/modules/helpers/background/hexToRgb.js
Normal file
31
src/modules/helpers/background/hexToRgb.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import rgbToHsv from './rgbToHsv';
|
||||
import setRgba from './setRgba';
|
||||
|
||||
const hexRegexp = /(^#{0,1}[0-9A-F]{6}$)|(^#{0,1}[0-9A-F]{3}$)|(^#{0,1}[0-9A-F]{8}$)/i;
|
||||
|
||||
const regexp = /([0-9A-F])([0-9A-F])([0-9A-F])/i;
|
||||
|
||||
export default function hexToRgb(value) {
|
||||
const valid = hexRegexp.test(value);
|
||||
|
||||
if (valid) {
|
||||
if (value[0] === '#') value = value.slice(1, value.length);
|
||||
|
||||
if (value.length === 3) value = value.replace(regexp, '$1$1$2$2$3$3');
|
||||
|
||||
const red = parseInt(value.substr(0, 2), 16);
|
||||
const green = parseInt(value.substr(2, 2), 16);
|
||||
const blue = parseInt(value.substr(4, 2), 16);
|
||||
const alpha = parseInt(value.substr(6, 2), 16) / 255;
|
||||
|
||||
const color = setRgba(red, green, blue, alpha);
|
||||
const hsv = rgbToHsv({ ...color });
|
||||
|
||||
return {
|
||||
...color,
|
||||
...hsv,
|
||||
};
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
11
src/modules/helpers/background/rgbToHex.js
Normal file
11
src/modules/helpers/background/rgbToHex.js
Normal file
@@ -0,0 +1,11 @@
|
||||
export default function rgbToHex(red, green, blue) {
|
||||
let r16 = red.toString(16);
|
||||
let g16 = green.toString(16);
|
||||
let b16 = blue.toString(16);
|
||||
|
||||
if (red < 16) r16 = `0${r16}`;
|
||||
if (green < 16) g16 = `0${g16}`;
|
||||
if (blue < 16) b16 = `0${b16}`;
|
||||
|
||||
return r16 + g16 + b16;
|
||||
}
|
||||
42
src/modules/helpers/background/rgbToHsv.js
Normal file
42
src/modules/helpers/background/rgbToHsv.js
Normal file
@@ -0,0 +1,42 @@
|
||||
export default function rgbToHSv({ red, green, blue }) {
|
||||
let rr;
|
||||
let gg;
|
||||
let bb;
|
||||
let h;
|
||||
let s;
|
||||
|
||||
const rabs = red / 255;
|
||||
const gabs = green / 255;
|
||||
const babs = blue / 255;
|
||||
const v = Math.max(rabs, gabs, babs);
|
||||
const diff = v - Math.min(rabs, gabs, babs);
|
||||
const diffc = c => (v - c) / 6 / diff + 1 / 2;
|
||||
if (diff === 0) {
|
||||
h = 0;
|
||||
s = 0;
|
||||
} else {
|
||||
s = diff / v;
|
||||
rr = diffc(rabs);
|
||||
gg = diffc(gabs);
|
||||
bb = diffc(babs);
|
||||
|
||||
if (rabs === v) {
|
||||
h = bb - gg;
|
||||
} else if (gabs === v) {
|
||||
h = (1 / 3) + rr - bb;
|
||||
} else if (babs === v) {
|
||||
h = (2 / 3) + gg - rr;
|
||||
}
|
||||
if (h < 0) {
|
||||
h += 1;
|
||||
} else if (h > 1) {
|
||||
h -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
hue: Math.round(h * 360),
|
||||
saturation: Math.round(s * 100),
|
||||
value: Math.round(v * 100),
|
||||
};
|
||||
}
|
||||
21
src/modules/helpers/background/setRgba.js
Normal file
21
src/modules/helpers/background/setRgba.js
Normal file
@@ -0,0 +1,21 @@
|
||||
function isValidRGBValue(value) {
|
||||
return (typeof (value) === 'number' && Number.isNaN(value) === false && value >= 0 && value <= 255);
|
||||
}
|
||||
|
||||
export default function setRGBA(red, green, blue, alpha) {
|
||||
if (isValidRGBValue(red) && isValidRGBValue(green) && isValidRGBValue(blue)) {
|
||||
const color = {
|
||||
red: red | 0,
|
||||
green: green | 0,
|
||||
blue: blue | 0,
|
||||
};
|
||||
|
||||
if (isValidRGBValue(alpha) === true) {
|
||||
color.alpha = alpha | 0;
|
||||
}
|
||||
|
||||
// RGBToHSL(color.r, color.g, color.b);
|
||||
|
||||
return color;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user