question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Color math should ideally always be in floats

See original GitHub issue

Version: All, master

Platform(s): All, but mostly affects newer devices

Color math should ideally be done with float to support arbitrary precision. It’s somewhat common to have 10-bpc and 12-bpc displays, and HDR color math often needs 16 bits per channel. float aka Single precision floats have 23 bits of significant binary digits which is plenty for colors.

Floats also avoid the problem of clipping, as colors above 1 still work fine. Even if they clip when displayed, if the brightness is reduced for the whole image, the brightness differences in bright areas is preserved when using floats.

Floats are also more friendly to the user. It’s easier to understand that “0.5” is 50% strength than “127”.

Xenko already has Color3 and Color4 classes which already work with floats. However, there’s not much point to Color3 in my opinion. You can just have constructors and methods that take the alpha value as an optional parameter for use cases that don’t need it.

If it were up to me, I’d do the following:

  1. Modify Color4 to make the alpha value optional. Consider deprecating Color3. This isn’t a breaking change.

  2. Rename Color to Color8 and consider deprecating it. This is a breaking change.

  3. Rename Color4 to Color. This is a breaking change.

These don’t all necessarily have to be done at once. In fact it may be preferable to leave a time gap between 2 and 3 to avoid people using “Color” in their code and having the implementation change without them knowing.

Obviously it’s the Xenko team’s decision of what to do, but I do think that something should be done, since color math being done with bytes is not ideal. Having a “Color” class with nothing after the name implies that it’s the recommended / default choice.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
Kryptos-FRcommented, Nov 12, 2018

I think you did not understand the point of Color3.

3reactions
aaronfrankecommented, Nov 12, 2018

@Kryptos-FR No, using optional parameters, it would be done like this:

public Color4(float red, float green, float blue, float alpha = 1.0f)
{
        R = red;
        G = green;
        B = blue;
        A = alpha;
}

Then you can write code in either of these two ways:

new Color4(0.1f, 0.2f, 0.3f); // Alpha is 1.0
new Color4(0.1f, 0.2f, 0.3f, 0.4f); // Alpha is 0.4
Read more comments on GitHub >

github_iconTop Results From Across the Web

Color math should ideally always be in floats · Issue #259
Color math should ideally be done with float to support arbitrary precision. It's somewhat common to have 10-bpc and 12-bpc displays, and HDR...
Read more >
Confusion about Floating Point in Color Space Transforms ...
In theory the best practice would be to linearize the footage and leave it linear, but in reality that's problematic, as each software...
Read more >
When do you use float and when do you use double
float should only be used if you need to operate on a lot of floating-point numbers (think in the order of thousands or...
Read more >
Why does unity recommend us to use float type for texture ...
GPUs that can do 16 bit floats (what half precision means are) can do that math roughly twice as fast as full precision,...
Read more >
Floating-point arithmetic
In computing, floating-point arithmetic (FP) is arithmetic that represents subsets of real numbers using an integer with a fixed precision, called the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found