In order for this site to work correctly we need to store a small file (called a cookie) on your computer. Most every site in the world does this, however since the 25th of May 2011, by law we have to get your permission first. Please abandon the forum if you disagree.

Para que este foro funcione correctamente es necesario guardar un pequeño fichero (llamado cookie) en su ordenador. La mayoría de los sitios de Internet lo hacen, no obstante desde el 25 de Marzo de 2011 y por ley, necesitamos de su permiso con antelación. Abandone este foro si no está conforme.

rgb to/from hsl

Xailer English public forum
Responder
Roberto
Mensajes: 177
Registrado: Mar Oct 16, 2007 11:53 pm

rgb to/from hsl

Mensaje por Roberto »

Hi,
Is there any sample to convert from rgb to hsl format and vice-versa?
tks,
Roberto
Mensajes: 177
Registrado: Mar Oct 16, 2007 11:53 pm

rgb to/from hsl

Mensaje por Roberto »

I found the formula to convert from hsl to rgb, so...
function hsl2Rgb(H, S, L)
LOCAL C, X, m, v
H := H/240 * 360
S := S/240
L := L/240

C := (1 - Abs(2 * L - 1)) * S
X := C * (1 - Abs((H / 60) % 2 - 1))
m := L - C/2

IF H < 60 ; v := {C, X, 0}
ELSEIF H < 120 ; v := {X, C, 0}
ELSEIF H < 180 ; v := {0, C, X}
ELSEIF H < 240 ; v := {0, X, C}
ELSEIF H < 300 ; v := {X, 0, C}
ELSEIF H < 360 ; v := {C, 0, X}
endiF
AEval(v, { |q, p| v[p] := round((v[p] + m) * 255, 0) })

RETURN v[1] + 256 * v[2] + 256 * 256 * v[3]
Avatar de Usuario
emeasoft
Mensajes: 1088
Registrado: Mié Abr 01, 2009 4:12 pm
Ubicación: emeasoft
Contactar:

rgb to/from hsl

Mensaje por emeasoft »

Roberto, bom dia.
Apenas por curiosidade, qual é a finalidade desta conversão?
Abraços.
Mário Angelini
Roberto
Mensajes: 177
Registrado: Mar Oct 16, 2007 11:53 pm

rgb to/from hsl

Mensaje por Roberto »

Olá, Mario.
Eu utilizo varias janelas e preciso fazer um contraste entre elas de modo que a aplicacao nao fique poluida visualmente. Entao, a primeira janela eu uso uma cor e, se preciso abrir uma segunda sobre esta, eu utilizo um cor para o fundo semelhante a primeira, so que com uma luminosidade maior ou menor. Como as cores sao definidas dinamicamente (para evitar repeticao) entao eu preciso de um funcao que faça isso. í‰ exatamento o efeito dos parametros H, L e S das cores do windows (veja a propriedade nClrPane e pressione F2). Tambem utilizo para dar aquele efeito de "acender" ou "apagar" a luz de um controle.
joselalinf
Mensajes: 291
Registrado: Jue Ene 31, 2013 1:10 pm

rgb to/from hsl

Mensaje por joselalinf »

Roberto,
el API de Windows tiene funciones para hacerlo.
Añade un módulo en C a tu proyecto (menú Archivo->Nuevo->C) e incluye este
código:
#include <shlwapi.h>
HB_FUNC( RGBTOHLS ) // RGBTOHLS( nColor ) --> { nHue, nLuminance, nSaturation }
{
WORD wHue = 0;
WORD wLuminance = 0;
WORD wSaturation = 0;
ColorRGBToHLS( (COLORREF) hb_parnl( 1 ), &wHue, &wLuminance,
&wSaturation );
hb_reta( 3 );
hb_storvnl( wHue, -1, 1 );
hb_storvnl( wLuminance, -1, 1 );
hb_storvnl( wSaturation, -1, 1 );
}
HB_FUNC( HLSTORGB ) // HLSTORGB( nHue, nLuminance, nSaturation ) --> nColor
{
hb_retnl( ColorHLSToRGB( hb_parnl( 1 ), hb_parnl( 2 ), hb_parnl( 3 ) ) );
}
Si estás usando Borland tendrás que añadir shwlapi.lib al proyecto y si
compilas con MinGW tendrás que añadir libshwlapi.a
Saludos,
José Lalí­n
Roberto
Mensajes: 177
Registrado: Mar Oct 16, 2007 11:53 pm

rgb to/from hsl

Mensaje por Roberto »

tanks.
could you give me an working sample?
joselalinf
Mensajes: 291
Registrado: Jue Ene 31, 2013 1:10 pm

rgb to/from hsl

Mensaje por joselalinf »

Roberto,
the source is autodocumented:
RGBTOHLS( nColor ) --> { nHue, nLuminance, nSaturation }
HLSTORGB( nHue, nLuminance, nSaturation ) --> nColor
Regards,
José Lalí­n
Roberto
Mensajes: 177
Registrado: Mar Oct 16, 2007 11:53 pm

rgb to/from hsl

Mensaje por Roberto »

José,
Thanks for your answer. I know how to use the functions, the problem is when I try to compile the program.
I added the lib shwlapi.lib as you told me and the file HLS.c below:
/*
* Project: SI
* File: HLS.c
* Description:
* Author:
* Date: 07-01-2013
*/
#include <shlwapi.h>
HB_FUNC( RGBTOHLS ) // RGBTOHLS( nColor ) --> { nHue, nLuminance, nSaturation }
{
WORD wHue = 0;
WORD wLuminance = 0;
WORD wSaturation = 0;
ColorRGBToHLS( (COLORREF) hb_parnl( 1 ), &wHue, &wLuminance, &wSaturation );
hb_reta( 3 );
hb_storvnl( wHue, -1, 1 );
hb_storvnl( wLuminance, -1, 1 );
hb_storvnl( wSaturation, -1, 1 );
}
HB_FUNC( HLSTORGB ) // HLSTORGB( nHue, nLuminance, nSaturation ) --> nColor
{
hb_retnl( ColorHLSToRGB( hb_parnl( 1 ), hb_parnl( 2 ), hb_parnl( 3 ) ) );
}
but I am receiving the following error:
Compiling: HLS.c...
Warning W8065 HLS.c 18: Call to function 'hb_parnl' with no prototype in function HB_FUNC
Warning W8065 HLS.c 20: Call to function 'hb_reta' with no prototype in function HB_FUNC
Warning W8065 HLS.c 21: Call to function 'hb_storvnl' with no prototype in function HB_FUNC
Warning W8065 HLS.c 22: Call to function 'hb_storvnl' with no prototype in function HB_FUNC
Warning W8065 HLS.c 23: Call to function 'hb_storvnl' with no prototype in function HB_FUNC
Warning W8070 HLS.c 24: Function should return a value in function HB_FUNC
Warning W8057 HLS.c 24: Parameter 'RGBTOHLS' is never used in function HB_FUNC
Error E2238 HLS.c 28: Multiple declaration for 'HB_FUNC'
Error E2344 HLS.c 12: Earlier declaration of 'HB_FUNC'
Warning W8065 HLS.c 29: Call to function 'hb_parnl' with no prototype in function HB_FUNC
Warning W8065 HLS.c 29: Call to function 'hb_parnl' with no prototype in function HB_FUNC
Warning W8065 HLS.c 29: Call to function 'hb_parnl' with no prototype in function HB_FUNC
Warning W8065 HLS.c 29: Call to function 'hb_retnl' with no prototype in function HB_FUNC
Warning W8070 HLS.c 30: Function should return a value in function HB_FUNC
Warning W8057 HLS.c 30: Parameter 'HLSTORGB' is never used in function HB_FUNC
1 Files, 13 Warnings, 2 Errors
Compile time: 0.26s Link time: 0s Total time: 0.34s
What am I doing wrong?
TIA
joselalinf
Mensajes: 291
Registrado: Jue Ene 31, 2013 1:10 pm

rgb to/from hsl

Mensaje por joselalinf »

Roberto,
> What am I doing wrong?
#include <windows.h>
#include <shlwapi.h>
#include <xailer.h>
Regards,
José Lalí­n
Roberto
Mensajes: 177
Registrado: Mar Oct 16, 2007 11:53 pm

rgb to/from hsl

Mensaje por Roberto »

thanks, again.
right now I am getting this:
Compiling: HLS.c...
Warning W8065 HLS.c 22: Call to function 'hb_storvnl' with no prototype in function HB_FUN_RGBTOHLS
Warning W8065 HLS.c 23: Call to function 'hb_storvnl' with no prototype in function HB_FUN_RGBTOHLS
Warning W8065 HLS.c 24: Call to function 'hb_storvnl' with no prototype in function HB_FUN_RGBTOHLS
Linking: SI.exe...
Error: Unresolved external '_hb_storvnl' referenced from C:DEVSIOBJHLS.DBJ
1 Files, 3 Warnings, 1 Errors
Compile time: 0.20s Link time: 0.62s Total time: 1.04s
sorry to boring you,
TIA
Roberto
Mensajes: 177
Registrado: Mar Oct 16, 2007 11:53 pm

rgb to/from hsl

Mensaje por Roberto »

thanks, again.
I am still doing something wrong.
Compiling: HLS.c...
Warning W8065 HLS.c 22: Call to function 'hb_storvnl' with no prototype in function HB_FUN_RGBTOHLS
Warning W8065 HLS.c 23: Call to function 'hb_storvnl' with no prototype in function HB_FUN_RGBTOHLS
Warning W8065 HLS.c 24: Call to function 'hb_storvnl' with no prototype in function HB_FUN_RGBTOHLS
Linking: SI.exe...
Error: Unresolved external '_hb_storvnl' referenced from C:DEVSIOBJHLS.DBJ
1 Files, 3 Warnings, 1 Errors
Compile time: 0.20s Link time: 0.62s Total time: 1.04s
joselalinf
Mensajes: 291
Registrado: Jue Ene 31, 2013 1:10 pm

rgb to/from hsl

Mensaje por joselalinf »

Roberto,
hb_storvnl is a Harbour function. If you want to use xHarbour change it to
hb_stornl.
Regards,
José Lalí­n
Responder