All files / packages/core/src/utilities color.ts

0% Statements 0/5
0% Branches 0/4
0% Functions 0/3
0% Lines 0/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34                                                                   
function componentToHex(c) {
  const hex = c.toString(16);
  return hex.length == 1 ? '0' + hex : hex;
}
 
/**
 * Converts RGB color values to a hexadecimal color string.
 * @param r - The red component value (0-255).
 * @param g - The green component value (0-255).
 * @param b - The blue component value (0-255).
 * @returns The hexadecimal color string representation.
 */
function rgbToHex(r, g, b) {
  return '#' + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
 
/**
 * Converts a hexadecimal color code to an RGB object.
 * @param hex - The hexadecimal color code to convert.
 * @returns An object representing the RGB values of the color, or null if the input is invalid.
 */
function hexToRgb(hex) {
  const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result
    ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16),
      }
    : null;
}
 
export { hexToRgb, rgbToHex };