edgecase of setting the color to #000

This commit is contained in:
Shea Duma committed 2026-04-23 02:17:19 +03:00
1 parent 6a000a42c1
commit 3446423ed9
1 file changed
+7 -9
+7 -9
View File
@@ -3,15 +3,13 @@ export function shadeColor(initialColor: string, percent: number) {
const ratio = 1 + percent / 100;
// Get hex value, convert it to number, multiply it by the desired amount, then clamp it
const R = Math.floor(
Math.max(Math.min(parseInt(initialColor.substring(1, 3), 16) * ratio, 255), 0)
);
const G = Math.floor(
Math.max(Math.min(parseInt(initialColor.substring(3, 5), 16) * ratio, 255), 0)
);
const B = Math.floor(
Math.max(Math.min(parseInt(initialColor.substring(5, 7), 16) * ratio, 255), 0)
);
let R = Math.floor(Math.min(parseInt(initialColor.substring(1, 3), 16) * ratio, 255));
let G = Math.floor(Math.min(parseInt(initialColor.substring(3, 5), 16) * ratio, 255));
let B = Math.floor(Math.min(parseInt(initialColor.substring(5, 7), 16) * ratio, 255));
if (R <= 0 && percent > 0) R = Math.max(178 - R, 0);
if (G <= 0 && percent > 0) G = Math.max(178 - G, 0);
if (B <= 0 && percent > 0) B = Math.max(178 - B, 0);
const RR = `${R < 16 ? '0' : ''}${R.toString(16)}`;
const GG = `${G < 16 ? '0' : ''}${G.toString(16)}`;