You are not logged in.

#1 2011-11-14 20:57:56

wilco2009
Member

HSL management, Brightness efect and copytext funcion

GRRLIB_CopyTex
Copy src texture to dst texture. Texture must be created with CreateEmptyTexture and must have same size.

Code:

void GRRLIB_CopyTex(GRRLIB_texImg* src, GRRLIB_texImg* dst) {
    memcpy(dst->data, src->data, (src->h * src->w) << 2);
    GRRLIB_FlushTex(dst);
}

HSL MANAGEMENT FUNCTIONS
u32 GRRLIB_RGBToHSL(u32 cRGB)
Convert from RGBA color to HSLA. All values from 0x00 to 0xFF
cRGB: RGBA Color
returns HSLA equivalent color

u32 GRRLIB_HSLToRGB(u32 cHSL)
Convert from HSLA color to RGBA. All values from 0x00 to 0xFF
cHSL: HSLA Color
returns RGBA equivalent color

Code:

#define H( c ) (((c) >> 24) &0xFF)
#define S( c ) (((c) >> 16) &0xFF)
#define V( c ) (((c) >> 8) &0xFF)
#define L( c ) (((c) >> 8) &0xFF)

#define HSLA(h,s,l,a) ( (u32)( ( ((u32)(h))        <<24) |  \
                               ((((u32)(s)) &0xFF) <<16) |  \
                               ((((u32)(l)) &0xFF) << 8) |  \
                               ( ((u32)(a)) &0xFF      ) ) )

float max(float a, float b){
    if (a > b) return a;
    else return b;
}

float min(float a, float b){
    if (a < b) return a;
    else return b;
}

u32 GRRLIB_RGBToHSL(u32 cRGB){
    float r= (float) R(cRGB)/0xFF;    //RGB from 0 to 255
    float g= (float) G(cRGB)/0xFF;
    float b= (float) B(cRGB)/0xFF;

    float maximo = max(r,max(g,b));
    float minimo = min(r,min(g,b));
    float delta = maximo-minimo;

    float h=0,s=0;
    float l = (maximo+minimo)/2;

    if (delta == 0){    //This is a gray, no chroma...
        h = 0;            //HSL results from 0 to 
        s = 0;
    } else {            //Chromatic data...
        if (l < 0.5) 
            s = delta/(maximo + minimo);
        else
            s = delta/(2.0-maximo-minimo);

        float del_R=(((maximo-r)/6.0)+(delta/2.0))/delta;
        float del_G=(((maximo-g)/6.0)+(delta/2.0))/delta;
        float del_B=(((maximo-b)/6.0)+(delta/2.0))/delta;
        if(r == maximo) h = del_B - del_G;
        else if (g == maximo) h = (1.0/3.0)+del_R-del_B;
        else if (b == maximo) h = (2.0/3.0)+del_G-del_R;

        if ( h < 0.0 ) h += 1.0;
        if ( h > 1.0 ) h -= 1.0;
    }
    return HSLA(h*0xFF,s*0xFF,l*0xFF,A(cRGB));
}

float Hue_2_RGB(float v1, float v2, float vH){             //Function Hue_2_RGB
   if ( vH < 0 ) vH += 1.0;
   if ( vH > 1 ) vH -= 1.0;
   if ( ( 6.0 * vH ) < 1 ) return ( v1 + ( v2 - v1 ) * 6.0 * vH );
   if ( ( 2.0 * vH ) < 1 ) return ( v2 );
   if ( ( 3.0 * vH ) < 2 ) return ( v1 + ( v2 - v1 ) * ( ( 2.0 / 3.0 ) - vH ) * 6.0 );
   return ( v1 );
}

u32 GRRLIB_HSLToRGB(u32 cHSL){
    float h= (float) H(cHSL)/0xFF;    //RGB from 0 to 255
    float s= (float) S(cHSL)/0xFF;
    float l= (float) L(cHSL)/0xFF;
    float var_1 = 0, var_2 = 0;
    float r,g,b;
    
    if (s == 0 ){                       //HSL from 0 to 1
        r = l;
        g = l;
        b = l;
    } else {
        if (l < 0.5) var_2 = l*(1.0+s);
        else var_2 = (l+s)-(s*l);
        var_1 = 2.0*l-var_2;

        r = Hue_2_RGB(var_1, var_2, h+(1.0/3.0)); 
        g = Hue_2_RGB(var_1, var_2, h);
        b = Hue_2_RGB(var_1, var_2, h-(1.0/3.0));
    }    
    return RGBA(r*0xFF,g*0xFF,b*0xFF,A(cHSL));
}

BMFX FUNCTIONS
void  GRRLIB_BMFX_Brightness (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, int value)
Add a brightness value to the texsrc texture
texsrc: Texture to apply the efect
value: Brightness value to add

Code:

void  GRRLIB_BMFX_Brightness (const GRRLIB_texImg *texsrc,
                             GRRLIB_texImg *texdest, int value) {
    unsigned int x, y;
    u32 hsl;
    u32 color;

    for (y = 0; y < texsrc->h; y++) {
        for (x = 0; x < texsrc->w; x++) {
            color = GRRLIB_GetPixelFromtexImg(x, y, texsrc);
            
            hsl = GRRLIB_RGBToHSL(color);
            
            int l = L(hsl)+value;
            if (l>0xFF) l = 0xFF;

            color = GRRLIB_HSLToRGB(RGBA(H(hsl),S(hsl),l,A(hsl)));

            GRRLIB_SetPixelTotexImg(x, y, texdest,color);
        }
    }
    GRRLIB_SetHandle(texdest, 0, 0);
}

In development:
           BMFX_Saturate
           BMFX_ChangeColor
           BMFX_AutoLevelAdjust

Offline

 

#2 2011-11-19 01:30:25

wilco2009
Member

Re: HSL management, Brightness efect and copytext funcion

Is there anyone there?
Is this forum alive?

Offline

 

#3 2011-11-28 20:50:18

owen
Member

Re: HSL management, Brightness efect and copytext funcion

The forum is quiet through most of the month.

Offline

 

#4 2011-11-29 00:18:34

wilco2009
Member

Re: HSL management, Brightness efect and copytext funcion

Yes..... Crayon answered some questions in General Help at beginning of the month and after that forum is totally quiet.

I hope GRRLIB is not being abandoned

Offline

 

#5 2011-11-30 23:38:13

Crayon
Bad Mother Fucker

Re: HSL management, Brightness efect and copytext funcion

Two commits this month, that's not so bad : http://code.google.com/p/grrlib/source/list

I'm mostly just updating 3rd party libs and deletting spam on this forum. GRRLIB is not completly dead smile

Offline

 

#6 2011-12-01 09:04:33

wilco2009
Member

Re: HSL management, Brightness efect and copytext funcion

Very good news......
I'm glad to hear it. wink

Last edited by wilco2009 (2011-12-01 09:05:08)

Offline

 

Board footer

Powered by FluxBB