Jump to page: 1 2
Thread overview
template? mixin? template mixins? for modifying a struct setup
May 19, 2022
Chris Katko
May 19, 2022
user1234
May 19, 2022
bauss
May 19, 2022
ag0aep6g
May 20, 2022
Chris Katko
May 20, 2022
Mike Parker
May 20, 2022
Christopher Katko
May 20, 2022
Mike Parker
May 20, 2022
Adam D Ruppe
May 19, 2022

given

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:

auto myColor = GREY!(0.5);
// where GREY!(0.5) becomes COLOR(0.5, 0.5, 0.5, 1.0);
May 19, 2022

On Thursday, 19 May 2022 at 10:15:32 UTC, Chris Katko wrote:

>

given

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:

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 ;)

May 19, 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

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:

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:

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:

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)
May 19, 2022
On 19.05.22 12:15, 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);
> ```

What's wrong with a simple plain function?

COLOR grey(float rgb)
{
    return COLOR(rgb, rgb, rgb, 1);
}
auto myColor = grey(0.5);
May 20, 2022
On Thursday, 19 May 2022 at 10:35:30 UTC, ag0aep6g wrote:
> On 19.05.22 12:15, 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);
>> ```
>
> What's wrong with a simple plain function?
>
> COLOR grey(float rgb)
> {
>     return COLOR(rgb, rgb, rgb, 1);
> }
> auto myColor = grey(0.5);

Yeah that occurred to me as I was falling asleep. Though, do I have to a specify

```D
static auto myColor = grey(0.5);
```
to ensure it's done at compile time? It's not the end of the world, but ideally, these are static / hardcoded values that can be used thousands of times a second.
May 19, 2022

On 5/19/22 8:12 PM, Chris Katko wrote:

>

On Thursday, 19 May 2022 at 10:35:30 UTC, ag0aep6g wrote:

>

On 19.05.22 12:15, Chris Katko wrote:

>

given

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:

auto myColor = GREY!(0.5);
// where GREY!(0.5) becomes COLOR(0.5, 0.5, 0.5, 1.0);

What's wrong with a simple plain function?

COLOR grey(float rgb)
{
    return COLOR(rgb, rgb, rgb, 1);
}
auto myColor = grey(0.5);

Yeah that occurred to me as I was falling asleep. Though, do I have to a specify

static auto myColor = grey(0.5);

to ensure it's done at compile time? It's not the end of the world, but ideally, these are static / hardcoded values that can be used thousands of times a second.

Given a CTFE function it's very easy to wrap for ensuring compile-time usage:

enum ctGrey(float f) = grey(f);

auto myColor = ctGrey!(0.5);

-Steve

May 19, 2022

On 5/19/22 8:29 PM, Steven Schveighoffer wrote:

>

Given a CTFE function it's very easy to wrap for ensuring compile-time usage:

enum ctGrey(float f) = grey(f);

auto myColor = ctGrey!(0.5);

That being said, if it's calculatable at compile time, chances are the compiler is already going to do it, even for a standard constructor call, especially if there's no custom constructor.

-Steve

May 20, 2022
On Friday, 20 May 2022 at 00:12:44 UTC, Chris Katko wrote:

> Yeah that occurred to me as I was falling asleep. Though, do I have to a specify
>
> ```D
> static auto myColor = grey(0.5);
> ```
> to ensure it's done at compile time? It's not the end of the world, but ideally, these are static / hardcoded values that can be used thousands of times a second.

If the declarations are at module scope, `static` has no effect, and CTFE will be used for initialization.
May 20, 2022
On Friday, 20 May 2022 at 02:30:10 UTC, Mike Parker wrote:
> On Friday, 20 May 2022 at 00:12:44 UTC, Chris Katko wrote:
>
>> Yeah that occurred to me as I was falling asleep. Though, do I have to a specify
>>
>> ```D
>> static auto myColor = grey(0.5);
>> ```
>> to ensure it's done at compile time? It's not the end of the world, but ideally, these are static / hardcoded values that can be used thousands of times a second.
>
> If the declarations are at module scope, `static` has no effect, and CTFE will be used for initialization.

So wait, that means if I have a module with extra stuff like

````D
colors.d

auto red =
// grey
````

and then in my other file
````D
auto white = grey(1.0);
````

It won't use CTFE? Why is there a local module requirement?

May 20, 2022
On Friday, 20 May 2022 at 14:54:31 UTC, Christopher Katko wrote:

> So wait, that means if I have a module with extra stuff like
>
> ````D
> colors.d
>
> auto red =
> // grey
> ````
>
> and then in my other file
> ````D
> auto white = grey(1.0);
> ````
>
> It won't use CTFE? Why is there a local module requirement?

I'm not sure what you mean by "local module requirement". The static storage class means that a variable will be around for the lifetime of the program (or more specifically in D's case, the lifetime of the thread). Module-scope variables are static by default.
« First   ‹ Prev
1 2