template? mixin? template mixins? for modifying a struct setup

bauss jj_1337 at live.dk
Thu May 19 10:34:48 UTC 2022


On Thursday, 19 May 2022 at 10:18:38 UTC, user1234 wrote:
> On Thursday, 19 May 2022 at 10:15:32 UTC, Chris Katko wrote:
>> given
>> ```D
>> struct COLOR
>> {
>> float r, g, b, a; // a is alpha (opposite of transparency)
>> }
>>
>> auto red   = COLOR(1,0,0,1);
>> auto green = COLOR(0,1,0,1);
>> auto blue  = COLOR(0,0,1,1);
>> auto white = COLOR(1,1,1,1);
>> //etc
>> ```
>>
>> is there a way to do:
>> ```D
>> auto myColor = GREY!(0.5);
>> // where GREY!(0.5) becomes COLOR(0.5, 0.5, 0.5, 1.0);
>> ```
>
> average is a bad way to grayscale FYI ;)

This is correct, you actually have to do something like this:

```d
uint g = (uint)((0.3f * r) + (0.59f * g) + (0.11f * b));
```

Where g is the new value for the current pixel's rgb value.

However, OP doesn't seem to be grayscaling images, but rather 
just wanting to specify gray colors.

In which case something like this could work:

```d
COLOR GREY(float amount)() { return COLOR(amount, amount, amount, 
1.0); }

...

auto myColor = GREY!(0.5);

myColor is COLOR(0.5, 0.5, 0.5, 1.0)
```



More information about the Digitalmars-d-learn mailing list