Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DrawCylinder Color.FromRGBA #2592

Closed
ptcsoderlund opened this issue May 11, 2024 · 2 comments
Closed

DrawCylinder Color.FromRGBA #2592

ptcsoderlund opened this issue May 11, 2024 · 2 comments
Labels
bug Something isn't working core
Milestone

Comments

@ptcsoderlund
Copy link

Issue description:
Seems like bytes are in order of ARGB and not RGBA as function indicates.
Hence, Color.FromRGBA should be named Color.FromARGB. Or its just when drawing cylinder?
I dont know.

Steps to reproduce:

public override void OnDebugDraw()
{
    base.OnDebugDraw();
    const float radius = 100f;
    const float height = 200f;
    //This gives a semi transparent red color, indicating its actually ARGB.
    DebugDraw.DrawCylinder(Actor.Position, Quaternion.Identity,radius,height, Color.FromRGBA(0x55FF0000u));
}

Flax version:
1.8.1

@cNori
Copy link
Contributor

cNori commented May 13, 2024

letts see

internal code:

R = ((rgb >> 16) & 0xff) / 255.0f;
G = ((rgb >> 8) & 0xff)/ 255.0f;
B = ((rgb >> 0) & 0xff)/ 255.0f;
A = ((rgb >> 24) & 0xff)/ 255.0f;

where rgb is 0x55FF0000u with is in bits (01010101111111110000000000000000)
set do it manualy step by step
bit shift

R = 00000000000000000101010111111111
G = 00000000010101011111111100000000
B = 01010101111111110000000000000000
A = 00000000000000000000000001010101

mask the bit shift with 0xff

R = 00000000000000000000000000000001
G = 00000000000000000000000000000001
B = 00000000000000000000000000000001
A = 00000000000000000000000000000001

the 00000000000000000000000000000001 is just 1 so

R = 1 / 255.0f
G = 1 / 255.0f
B = 1 / 255.0f
A = 1 / 255.0f

output color

R = 0,003921568627451
G = 0,003921568627451
B = 0,003921568627451
A = 0,003921568627451

Conclusion funcions is broken

data masked out in the process

So, Lets fix it

Assuming the function is supposed to convert decode bytes inside the uint32

Input

rgba = 0x55FF0000u

Chanel Bits
R 01010101
G 11111111
B 00000000
A 00000000

version where top bits are R chanel

uint MaskR = 0b11111111_00000000_00000000_00000000;
uint MaskG = 0b00000000_11111111_00000000_00000000;
uint MaskB = 0b00000000_00000000_11111111_00000000;
uint MaskA = 0b00000000_00000000_00000000_11111111;
R = (rgba & MaskR) >> 24
G = (rgba & MaskG) >> 16
B = (rgba & MaskB)  >> 8
A = (rgba & MaskA) >> 0

with results in following byte value output

Chanel Byte Value Bits Float Value
R 85 00000000000000000000000001010101 0,3333333333333333
G 255 00000000000000000000000011111111 1
B 0 00000000000000000000000000000000 0
A 0 00000000000000000000000000000000 0

version where buttom bits are R chanel

uint MaskR = 0b00000000_00000000_00000000_11111111;
uint MaskG = 0b00000000_00000000_11111111_00000000;
uint MaskB = 0b00000000_11111111_00000000_00000000;
uint MaskA = 0b11111111_00000000_00000000_00000000;

R = (rgba & MaskR) >> 0
G = (rgba & MaskG) >> 8
B = (rgba & MaskB)  >> 16
A = (rgba & MaskA) >> 24

with results in following byte value output

Chanel byte Value Bits Float Value
R 0 00000000000000000000000000000000 0
G 0 00000000000000000000000000000000 0
B 255 00000000000000000000000011111111 1
A 85 00000000000000000000000001010101 0,3333333333333333

@mafiesto4 mafiesto4 added bug Something isn't working core labels May 16, 2024
@mafiesto4 mafiesto4 added this to the 1.9 milestone May 16, 2024
@mafiesto4
Copy link
Member

Thanks for your suggestions and for spotting this issue. I refactored color processing with those bits, added proper info in doc comments to clarify which channel is used in the bottom bits, and added unit text to ensure this won't be broken again.

2529312

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working core
Projects
None yet
Development

No branches or pull requests

3 participants