fix: input validation in hexToRgb, pre-initialize h in rgbToHsl
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
// Hex → RGB
|
// Hex → RGB
|
||||||
export function hexToRgb(hex) {
|
export function hexToRgb(hex) {
|
||||||
const h = hex.replace('#', '');
|
const h = hex.replace('#', '');
|
||||||
|
if (!/^[0-9a-fA-F]{3}$/.test(h) && !/^[0-9a-fA-F]{6}$/.test(h)) return null;
|
||||||
const n = parseInt(h.length === 3
|
const n = parseInt(h.length === 3
|
||||||
? h.split('').map(c => c + c).join('')
|
? h.split('').map(c => c + c).join('')
|
||||||
: h, 16);
|
: h, 16);
|
||||||
@@ -16,10 +17,10 @@ export function rgbToHex({ r, g, b }) {
|
|||||||
export function rgbToHsl({ r, g, b }) {
|
export function rgbToHsl({ r, g, b }) {
|
||||||
r /= 255; g /= 255; b /= 255;
|
r /= 255; g /= 255; b /= 255;
|
||||||
const max = Math.max(r, g, b), min = Math.min(r, g, b);
|
const max = Math.max(r, g, b), min = Math.min(r, g, b);
|
||||||
let h, s;
|
let h = 0, s = 0;
|
||||||
const l = (max + min) / 2;
|
const l = (max + min) / 2;
|
||||||
if (max === min) {
|
if (max === min) {
|
||||||
h = s = 0;
|
// h and s remain 0 (achromatic)
|
||||||
} else {
|
} else {
|
||||||
const d = max - min;
|
const d = max - min;
|
||||||
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
||||||
|
|||||||
Reference in New Issue
Block a user