25 lines
693 B
JavaScript
25 lines
693 B
JavaScript
/**
|
||
* Converts a single color component to a 2-digit hexadecimal string.
|
||
* @param {number} c - A number between 0 and 255.
|
||
* @returns {string} The 2-digit hex representation.
|
||
*/
|
||
const componentToHex = (c) => {
|
||
const hex = c.toString(16);
|
||
return hex.length === 1 ? '0' + hex : hex;
|
||
};
|
||
|
||
/**
|
||
* Converts an RGB object to a hexadecimal color string.
|
||
* @param {{ r: number, g: number, b: number }} color - An object with r, g, and b properties (0–255).
|
||
* @returns {string} The hex color string (e.g., "#ffcc00").
|
||
*/
|
||
const rgbToHex = (color) => {
|
||
return (
|
||
'#' +
|
||
componentToHex(color.r) +
|
||
componentToHex(color.g) +
|
||
componentToHex(color.b)
|
||
);
|
||
};
|
||
|
||
export { rgbToHex };
|