Day 92: relative color syntax

With the relative color syntax we can modify existing colors using color functions. If an origin color is specified, each color channel can either be directly specified, or taken from the origin color and modified.

For example, we can take a HEX color and add opacity using the rgb() color function and the from keyword.

div {
  --color: #FF0000;
}

div {
  background-color: rgb(from var(--color) r g b / 50%);
}
rgb(255 0 0 / 50%)

Or we can take the color and replace a specific channel.

div {
  background-color: rgb(from var(--color) r g 150);
}
rgb(255 0 0 / 50%)

We can even use the calc() function.

div {
  background-color: hsl(from var(--color) h s calc(l - 10%));
}
hsl(0deg 100% 40%)

We can use channel keywords in their corresponding argument, but we don’t have to. We can use them in any position.

div {
 background-color: rgb(from var(--color)
                    calc(r * .3 + g * .59 + b * .11)
                    calc(r * .3 + g * .59 + b * .11)
                    calc(r * .3 + g * .59 + b * .11));
}
rgb(30% 30% 30%)

How cool is that!? Is there a catch? Well, yeah, it’s currently only supported in Safari behind a flag.

My blog doesn’t support comments yet, but you can reply via blog@matuzo.at.


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović

With the relative color syntax we can modify existing colors using color functions. If an origin color is specified, each color channel can either be directly specified, or taken from the origin color and modified.

For example, we can take a HEX color and add opacity using the rgb() color function and the from keyword.

div {
  --color: #FF0000;
}

div {
  background-color: rgb(from var(--color) r g b / 50%);
}
rgb(255 0 0 / 50%)

Or we can take the color and replace a specific channel.

div {
  background-color: rgb(from var(--color) r g 150);
}
rgb(255 0 0 / 50%)

We can even use the calc() function.

div {
  background-color: hsl(from var(--color) h s calc(l - 10%));
}
hsl(0deg 100% 40%)

We can use channel keywords in their corresponding argument, but we don't have to. We can use them in any position.

div {
 background-color: rgb(from var(--color)
                    calc(r * .3 + g * .59 + b * .11)
                    calc(r * .3 + g * .59 + b * .11)
                    calc(r * .3 + g * .59 + b * .11));
}
rgb(30% 30% 30%)

How cool is that!? Is there a catch? Well, yeah, it's currently only supported in Safari behind a flag.

My blog doesn't support comments yet, but you can reply via blog@matuzo.at.


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

Manuel Matuzović | Sciencx (2023-01-31T00:00:00+00:00) Day 92: relative color syntax. Retrieved from https://www.scien.cx/2023/01/31/day-92-relative-color-syntax-2/

MLA
" » Day 92: relative color syntax." Manuel Matuzović | Sciencx - Tuesday January 31, 2023, https://www.scien.cx/2023/01/31/day-92-relative-color-syntax-2/
HARVARD
Manuel Matuzović | Sciencx Tuesday January 31, 2023 » Day 92: relative color syntax., viewed ,<https://www.scien.cx/2023/01/31/day-92-relative-color-syntax-2/>
VANCOUVER
Manuel Matuzović | Sciencx - » Day 92: relative color syntax. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/01/31/day-92-relative-color-syntax-2/
CHICAGO
" » Day 92: relative color syntax." Manuel Matuzović | Sciencx - Accessed . https://www.scien.cx/2023/01/31/day-92-relative-color-syntax-2/
IEEE
" » Day 92: relative color syntax." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2023/01/31/day-92-relative-color-syntax-2/. [Accessed: ]
rf:citation
» Day 92: relative color syntax | Manuel Matuzović | Sciencx | https://www.scien.cx/2023/01/31/day-92-relative-color-syntax-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.