colour lib needs reviewers

Manu via Digitalmars-d digitalmars-d at puremagic.com
Mon Sep 12 08:55:06 PDT 2016


On 13 September 2016 at 01:30, John Colvin via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> 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?

I think, yes?
So, the logic here is, if you cast, and then cast back, you will have
the same thing barring loss of precision.
Casts in this lib just change format (+precision), they don't change
value at all. I feel it's compatible with casting an int to a float...
it's the same sort of conversion.


> Seems to me that a function that takes a target type would be
> more obvious for the user.

Really? I like the cast this way. There is an explicit function in
package.d though...



More information about the Digitalmars-d mailing list