How to extract R,G,B Channels from a color
Using the new relative color syntax, you can easily extract the R,G,B channels of any color and use them as separate colors.
Useful when you want to do some color manipulation.
:root {
--c: #BD1550; /* the main color */
/* the R,G,B channels as separate colors */
--R: rgb(from var(--c) r 0 0);
--G: rgb(from var(--c) 0 g 0);
--B: rgb(from var(--c) 0 0 b);
}
You can do the same thing with a different syntax using the color()
function.
:root {
--c: #BD1550; /* the main color */
/* the R,G,B channels as separate colors */
--R: color(from var(--c) srgb r 0 0);
--G: color(from var(--c) srgb 0 g 0);
--B: color(from var(--c) srgb 0 0 b);
}
In both cases, you can either use "none" or "0". "none" is equivalent to "0" but has a special behavior when color interpolation is in play.
More CSS Tips
- CSS-only background patterns with a minimal code The biggest collection of CSS patterns. One-click to get the code.
- Single-digit inputs with one element (OTP) Turn a simple input into an One Time Password field.
- Avatar with status indicator A few lines of code to add a status indicator to any image.
- Create CSS Shapes with a single element An online resource to grab the code of any CSS shape with one click.