mirror of
https://github.com/iib0011/omni-tools.git
synced 2026-12-05 18:55:31 +03:00
chore: rgba parser from hex added to color util
This commit is contained in:
1 parent
ca8cf2b4b9
commit
5dd5f49d69
1 file changed
+24
@@ -29,3 +29,27 @@ export function convertHexToRGBA(color: string): number {
|
||||
const alphaChannel = 0xff;
|
||||
return (colorValue << 8) | alphaChannel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses any valid CSS color string into its RGB components,
|
||||
* using the canvas's own color parser for correctness across
|
||||
* hex (#fff, #ffffff), rgb()/rgba(), hsl(), and named colors.
|
||||
*/
|
||||
export function parseColorToRgb(color: string): {
|
||||
r: number;
|
||||
g: number;
|
||||
b: number;
|
||||
} {
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = canvas.height = 1;
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (!ctx) {
|
||||
throw new Error('Canvas context not supported');
|
||||
}
|
||||
|
||||
ctx.fillStyle = color; // browser validates & normalizes the color here
|
||||
ctx.fillRect(0, 0, 1, 1);
|
||||
|
||||
const [r, g, b] = ctx.getImageData(0, 0, 1, 1).data;
|
||||
return { r, g, b };
|
||||
}
|
||||
Reference in new issue
Block a user