On Monday, 12 September 2016 at 13:04:49 UTC, Manu wrote:
2. Q: is there anything to convert a color to grey scale ?
This isn't a short-answer question :) .. There are many ways
depending on the job.
A simple way might be:
L8 grey = cast(L8)RGB8(r, g, b); // L8 is luminance only,
casting
will do the right thing
This is the 'normal' way, but there are better ways...
A superior way if you want a top-quality result would be to use
the Lab luminance value, since Lab is perceptually uniform,
when you desaturate, it maintains the colors relative luminance
correctly, something like:
Lab!float lab = cast(Lab!float)RGB8(r, g, b); // cast your
color to Lab
lab.a = 0; // eliminate saturation on a axis
lab.b = 0; // eliminate saturation on b axis
// lab.L is now pure luminance
RGB8 c = cast(RGB8)lab; // cast it back to RGB, and your RGB
will be
greyscale, but with more perceptually correct conversion.
Lots of pow's in that conversion, so it's slower.
This link demonstrates some competing techniques:
https://en.wikipedia.org/wiki/HSL_and_HSV#/media/File:Fire-breather_CIELAB_L*.jpg
Press left/right to cycle through them.
The first method (cast to L8) is: Rec. 601 luma Y′. (or more
accurately, my code above would be: sRGB luma Y')
The second method (Lab.L) is: CIELAB L*.
L* performs much better in low light.
Is there some uniform policy w.r.t. casts here? If I take one
colour type and cast it to another will it always do something
unambiguously right or not compile? Seems to me that a function
that takes a target type would be more obvious for the user.
I think the multiple different meanings of cast are a mistake in
D. We can't go back now, but I wish there was only
reinterpet_cast and everything else was a function.