color_contrast¶
The color_to_rgb filter is designed to convert color values from Hexadecimal (Hex) or HSL (Hue, Saturation, Lightness) format to RGB (Red, Green, Blue) format.
Functionality
- Color Values: Takes a color value as input, expecting it to be in either Hex or HSL format.
- Hex Input:
- Supports both shorthand (
#RGB) and longhand (#RRGGBB) Hex color codes.
- Supports both shorthand (
- HSL Input:
- Supports both HSL (
hsl(H, S%, L%)) and HSLA (hsla(H, S%, L%, A)) formats, where A is the alpha (transparency) value (0 to 1).
- Supports both HSL (
- Conversion: Converts the input color to its equivalent RGB representation.
- Output: Returns a string representing the color in RGB format.
- For opaque colors (no transparency), the format is
rgb(R, G, B). - For colors with transparency, the format is
rgba(R, G, B, A), whereAis the alpha value (0 to 1).
- For opaque colors (no transparency), the format is
Syntax
Arguments
The color_to_rgb filter does not require any arguments.
Code Samples
Example 1: Converting from Hex to RGB
OutputExample 2: Converting from HSL to RGB
OutputOutliers and Special Cases¶
- Invalid Input: If the input string is not a valid Hex or HSL color, the filter will return a
nilvalue or an error message. - Non-String Input: If the input is not a string, the filter might attempt to convert it to a string or return an error.
- Alpha Values: The filter preserves alpha (transparency) values when converting from HSLA to RGBA.
Key Points¶
- The
color_to_rgbfilter is primarily useful when you need to work with colors in a consistent format, such as when manipulating colors in JavaScript or CSS. - It allows you to easily convert colors between different representations.
- Ensure that the input string is a valid Hex or HSL color to avoid errors.