A few questions on Color #489
-
Hi, I was working with
// RGB
const colA = rgb([1.0, 0.2, 0.0]);
// SRGB
const colB = srgb([1.0, 0.2, 0.0]);
// RGB -> SRGB
const colC = srgb(colA);
draw(
ctx,
group({}, [
rect([0, 0], [Math.ceil(width / 3), height], {
fill: colA,
}),
rect([Math.ceil((width / 3) * 1), 0], [Math.ceil(width / 3), height], {
fill: colB,
}),
rect(
[Math.ceil((width / 3) * 2), 0],
[Math.ceil((width / 3) * 2), height],
{
fill: colC,
},
),
]),
);
rect([0, 0], [Math.ceil(width / 3), height], {
fill: `color(display-p3 ${colA[0]} ${colA[1]} ${colA[2]})`,
}) Thank you for reading! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Hi @cdaein — currently, there's no (not yet) built-in support for P3 in the color and/or hiccup-canvas packages, simply because these libraries predate the general P3 support in browsers... I'm also still wondering how to best solve this and would be grateful for any ideas oreven other references how this could be handled best... As to your other question, I will get back to you on that later... |
Beta Was this translation helpful? Give feedback.
-
Hey @cdaein — sorry for the delay (had to get a new release out first), here's an attempt to explain why your 3 colors make sense the way they're dealt with right now: When you create a color (any type) in the form rgb("#abc")
// $Color [rgb] { buf: [ 0.40197777983219574, 0.4969329950608704, 0.6038273388553378, 1 ], offset: 0, stride: 1 }
srgb("#abc")
// $Color [srgb] { buf: [ 0.6666666666666666, 0.7333333333333333, 0.8, 1 ], offset: 0, stride: 1 }
hsl("#abc")
// $Color [hsl] { buf: [ 0.5833334374998698, 0.24999953125087915, 0.7333333333333334, 1 ], offset: 0, stride: 1 } Now maybe you can already see why colA = rgb([1.0, 0.2, 0.0]);
// $Color [rgb] { buf: [ 1, 0.2, 0 ], offset: 0, stride: 1 }
colC = srgb(colA);
// $Color [srgb] { buf: [ 0.9999999999999999, 0.48452920448170694, 0, 1 ], offset: 0, stride: 1 } The same logic (in reverse) then is used when converting colors to CSS strings: A linear RGB color will first be converted to sRGB and then to CSS, whilst an sRGB color will not have to undergo a gamma correction step: css(colA);
// '#ff7c00'
css(colB);
// '#ff3300'
css(colC);
// '#ff7c00' Also, the library attempts to use whatever is the best conversion route for a color type. By default the library assumes CSS Color Level 3, but you can also force L4: colD = oklch(colA);
// $Color [oklch] { buf: [ 0.72528468699035, 0.1886412384962652, 0.14211743633366036, 1 ], offset: 0, stride: 1 }
// CSS L3 doesn't support oklch color space, so conversion will be to hex
css(colD);
// '#ff7c00'
import { CSS_LEVEL4 } from "@thi.ng/color";
// ...but specifying L4 conversions as option, we get...
css(colD, CSS_LEVEL4);
// 'oklch(72.528% 0.189 51.162)' Does this help? |
Beta Was this translation helpful? Give feedback.
-
@cdaein Just released a new version of thi.ng/color (v5.7.0), which now includes the Still have to think how to best support the new |
Beta Was this translation helpful? Give feedback.
Hey @cdaein — sorry for the delay (had to get a new release out first), here's an attempt to explain why your 3 colors make sense the way they're dealt with right now:
When you create a color (any type) in the form
rgb(a,b,c)
orrgb([a, b, c])
then the values given are assumed to be in that intended color space already. For example, if I createhsl(0,1,0.5)
(or same forhsl([0,1,0.5])
), then the values given are interpreted as H,S,L, as you'd expect.... The only time an automatic conversion happens is if you pass a color of a different type as argument, e.g. as in yourcolC
case, or if you'd pass a CSS stringrgb("#abc")
. In the latter case, the CSS color would first be parsed as integer,…