# Color filters

Color filters change or extract properties of CSS color strings.

Color filters change or extract properties of CSS color strings. They are usually used together with color settings of the theme design.

## color_to_rgb

Converts a CSS color string to the CSS `rgb()` format.

```liquid
{{ '#7ab55c' | color_to_rgb }}
```

```text
rgb(122, 181, 92)
```

If the input color has an alpha component, the output uses the CSS `rgba()` format.

```liquid
{{ 'hsla(100, 38%, 54%, 0.5)' | color_to_rgb }}
```

```text
rgba(122, 181, 92, 0.5)
```

## color_to_hsl

Converts a CSS color string to the CSS `hsl()` format.

```liquid
{{ '#7ab55c' | color_to_hsl }}
```

```text
hsl(100, 38%, 54%)
```

If the input color has an alpha component, the output uses the CSS `hsla()` format.

```liquid
{{ 'rgba(122, 181, 92, 0.5)' | color_to_hsl }}
```

```text
hsla(100, 38%, 54%, 0.5)
```

## color_to_hex

Converts a CSS color string to hex6 format.

```liquid
{{ 'rgb(122, 181, 92)' | color_to_hex }}
```

```text
#7ab55c
```

The output is always hex6. If the input color has an alpha channel, it is dropped in the output.

```liquid
{{ 'rgba(122, 181, 92, 0.5)' | color_to_hex }}
```

```text
#7ab55c
```

## color_extract

Extracts a component from the color. Valid components are alpha, red, green, blue, hue, saturation and lightness.

```liquid
{{ '#7ab55c' | color_extract: 'red' }}
```

```text
122
```

## color_brightness

Calculates the perceived brightness of the given color. Uses the [W3C recommendations for calculating perceived brightness](https://www.w3.org/WAI/GL/wiki/Relative_luminance) to ensure adequate contrast.

```liquid
{{ '#7ab55c' | color_brightness }}
```

```text
153.21
```

## color_modify

Changes the given component of a color.

- Red, green and blue values must be a number between 0 and 255
- Alpha must be a decimal number between 0 and 1
- Hue must be between 0 and 360 degrees
- Saturation and lightness must be between 0 and 100 percent

```liquid
{{ '#7ab55c' | color_modify: 'red', 255 }}
```

```text
#ffb55c
```

The filter returns the color in the format that fits the change. For example, if you change the alpha channel, the filter returns `rgba()` even if the input was hex.

```liquid
{{ '#7ab55c' | color_modify: 'alpha', 0.85 }}
```

```text
rgba(122, 181, 92, 0.85)
```

## color_lighten

Lightens the input color. Takes a value between 0 and 100 percent.

```liquid
{{ '#7ab55c' | color_lighten: 30 }}
```

```text
#d0e5c5
```

## color_darken

Darkens the input color. Takes a value between 0 and 100 percent.

```liquid
{{ '#7ab55c' | color_darken: 30 }}
```

```text
#355325
```

## color_saturate

Saturates the input color. Takes a value between 0 and 100 percent.

```liquid
{{ '#7ab55c' | color_saturate: 30 }}
```

```text
#6ed938
```

## color_desaturate

Desaturates the input color. Takes a value between 0 and 100 percent.

```liquid
{{ '#7ab55c' | color_desaturate: 30 }}
```

```text
#869180
```

## color_mix

Mixes two colors. The blend factor must be a value between 0 and 100 percent.

```liquid
{{ '#7ab55c' | color_mix: '#ffc0cb', 50 }}
```

```text
#bdbb94
```

If one input has an alpha component and the other does not, an alpha of 1 is assumed for the input without it.

```liquid
{{ 'rgba(122, 181, 92, 0.75)' | color_mix: '#ffc0cb', 50 }}
```

```text
rgba(189, 187, 148, 0.875)
```

## color_contrast

Calculates the contrast ratio between two colors. Returns the numerator of the ratio, whose denominator is 1. For a contrast ratio of 3.5:1, the filter returns 3.5.

For accessibility, [WCAG 2.0 level AA](https://www.w3.org/WAI/GL/task-forces/silver/wiki/Visual_Contrast_of_Text_Subgroup#WCAG_2.x_to_Silver.2FAPCA_Comparison_Table) requires a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text. [Level AAA](https://www.w3.org/WAI/GL/task-forces/silver/wiki/Visual_Contrast_of_Text_Subgroup#WCAG_2.x_to_Silver.2FAPCA_Comparison_Table) requires at least 7:1 for normal text and 4.5:1 for large text.

The order of the colors does not matter. The filter detects the lighter and darker color automatically.

```liquid
{{ '#495859' | color_contrast: '#fffffb' }}
```

```text
7.4
```

## color_difference

Calculates the color difference or distance between two colors. For accessibility, the W3C suggests a color difference greater than 500.

```liquid
{{ '#ff0000' | color_difference: '#abcdef' }}
```

```text
528
```

## brightness_difference

Calculates the perceived brightness difference between two colors. For accessibility, the W3C suggests a brightness difference greater than 125.

```liquid
{{ '#fff00f' | brightness_difference: '#0b72ab' }}
```

```text
129
```
