color_to_hsl¶
The color_to_hsl filter is designed to convert color values from Hexadecimal (Hex) or RGB (Red, Green, Blue) format to HSL (Hue, Saturation, Lightness) format. HSL is often preferred for its intuitive representation of colors using hue, saturation, and lightness components.
Functionality
- Color Values: Takes a color value as input, expecting it to be in either Hex or RGB format.
- Hex Input:
- Supports both shorthand (
#RGB) and longhand (#RRGGBB) Hex color codes.
- Supports both shorthand (
- RGB Input:
- Supports both RGB (
rgb(R, G, B)) and RGBA (rgba(R, G, B, A)) formats, where A is the alpha (transparency) value (0 to 1).
- Supports both RGB (
- Conversion: Converts the input color to its equivalent HSL representation.
- Output: Returns a string representing the color in HSL format.
- For opaque colors (no transparency), the format is
hsl(H, S%, L%). - For colors with transparency, the format is
hsla(H, S%, L%, A), whereAis the alpha value (0 to 1).
- For opaque colors (no transparency), the format is
Syntax
Arguments
The color_to_hsl filter does not require any arguments.
Code Samples
Example 1: Converting from Hex to HSL
OutputExample 2: Converting from RGB to HSL
OutputOutliers and Special Cases¶
- Invalid Input: If the input string is not a valid Hex or RGB 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 RGBA to HSLA.
Key Points¶
- The
color_to_hslfilter is helpful when you prefer working with the HSL color model, which is often considered more intuitive for adjusting colors. - It allows you to easily switch between different color representations within your templates.
- Ensure that the input string is a valid Hex or RGB color to avoid errors.