You are not logged in.

#1 2010-12-09 06:35:12

owen
Member

[RESOLVED] Manipulating a u32 color in C

Is there s simple way to change, brighten or darken a color u32 color in c?  I'm passing one color to my cube function but I want a different shade of the same color on each side of the cube.  Is there a way to increased of decrease the hex value by a specified amount?

Also I've noticed that when I specify 0x11111100 or 0x00000000 that the object is not completely transparent.  I'm using the code in Grrlib examples.

Offline

 

#2 2010-12-09 15:06:42

owen
Member

Re: [RESOLVED] Manipulating a u32 color in C

RESOLVED  I figured it out after looking at the GRRLIB image manipulation Source

Code:

u32 color_darken(u32 color) {
    u16 sr, sg, sb;
    sr = R(color)-30;    sg = G(color)-30;    sb = B(color);
    if (sr>255) sr=255;  if (sg>255) sg=255;  if (sb>255) sb=255;
    return RGBA(sr,sg,sb,A(color));
}

u32 color_brighten(u32 color) {
    u16 sr, sg, sb;
    sr = R(color)+30;    sg = G(color)+30;    sb = B(color)-3;
    if (sr>255) sr=255;  if (sg>255) sg=255;  if (sb>255) sb=255;
    return RGBA(sr,sg,sb,A(color));
}

//GRRLIB_GetColor(sr,sg,sb,color&0xFF); deprecated or something or removed

Offline

 

#3 2010-12-09 21:37:34

NoNameNo
Administrator

Re: [RESOLVED] Manipulating a u32 color in C

here is an improved and corrected version wink

Code:

u32 color_darken(u32 color, int value) {
    u16 sr, sg, sb;
    sr = R(color)- value;    sg = G(color)-value;    sb = B(color)-value;
    if (sr<0) sr=0;  if (sg<0) sg=0;  if (sb<0) sb=0;
    return RGBA(sr,sg,sb,A(color));
}

u32 color_brighten(u32 color, int value) {
    u16 sr, sg, sb;
    sr = R(color)+ value;    sg = G(color)+value;    sb = B(color)-value;
    if (sr>255) sr=255;  if (sg>255) sg=255;  if (sb>255) sb=255;
    return RGBA(sr,sg,sb,A(color));
}

Offline

 

#4 2010-12-09 22:30:00

owen
Member

Re: [RESOLVED] Manipulating a u32 color in C

oh ok, but what about the second part of the question?  I've noticed that when I specify 0x11111100 or 0x00000000 that the object is not completely transparent.  why is that?

Offline

 

#5 2010-12-09 22:36:33

Crayon
Bad Mother Fucker

Re: [RESOLVED] Manipulating a u32 color in C

In the color_brighten function, with this code by NNN:

Code:

sb = B(color)-value;

or this code by owen:

Code:

sb = B(color)-3;

It's wrong to do:

Code:

if (sb>255) sb=255;

You need to do:

Code:

if (sb<0) sb=0;

Except if value parameter could be set to negative value in the NNN code.

Offline

 

#6 2010-12-10 10:23:51

NoNameNo
Administrator

Re: [RESOLVED] Manipulating a u32 color in C

Right Crayon,

Anyway, he have to clamp the final value of each component to 0-255, by the way he wants wink

Offline

 

Board footer

Powered by FluxBB