rgbToHex
A utility function to convert RGB values to a hexadecimal color string.> rgbToHex(r: number, g: number, b: number): string
- r (number): The red component of the color. Must be a value between 0 and 255.
- g (number): The green component of the color. Must be a value between 0 and 255.
- b (number): The blue component of the color. Must be a value between 0 and 255.
- Returns: A hexadecimal string representing the color in the format `#RRGGBB`.
Example
import { rgbToHex } from "@explita/daily-toolset";
// Convert RGB to Hex
const hexColor = rgbToHex(255, 99, 71);
console.log(hexColor); // Output: "#ff6347"
// Convert RGB with values out of range (they will be clamped)
const clampedHexColor = rgbToHex(300, -20, 500);
console.log(clampedHexColor); // Output: "#ff0000"
Use Cases
- Converting RGB values to their hexadecimal representation for CSS or HTML color styling.
- Ensuring RGB values stay within the valid range (0-255) when performing color calculations or conversions.
- Generating color codes for web applications or design systems based on RGB inputs.