Página 1 de 1

rgb to/from hsl

Publicado: Vie May 03, 2013 8:59 pm
por Roberto
Hi,
Is there any sample to convert from rgb to hsl format and vice-versa?
tks,

rgb to/from hsl

Publicado: Sab May 04, 2013 3:58 pm
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]

rgb to/from hsl

Publicado: Lun May 06, 2013 1:39 pm
por emeasoft
Roberto, bom dia.
Apenas por curiosidade, qual é a finalidade desta conversão?
Abraços.
Mário Angelini

rgb to/from hsl

Publicado: Mar May 07, 2013 12:16 am
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.

rgb to/from hsl

Publicado: Mar May 07, 2013 9:52 am
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

rgb to/from hsl

Publicado: Dom Jun 30, 2013 5:09 am
por Roberto
tanks.
could you give me an working sample?

rgb to/from hsl

Publicado: Lun Jul 01, 2013 9:59 pm
por joselalinf
Roberto,
the source is autodocumented:
RGBTOHLS( nColor ) --> { nHue, nLuminance, nSaturation }
HLSTORGB( nHue, nLuminance, nSaturation ) --> nColor
Regards,
José Lalí­n

rgb to/from hsl

Publicado: Mar Jul 02, 2013 12:38 am
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

rgb to/from hsl

Publicado: Mar Jul 02, 2013 8:29 am
por joselalinf
Roberto,
> What am I doing wrong?
#include <windows.h>
#include <shlwapi.h>
#include <xailer.h>
Regards,
José Lalí­n

rgb to/from hsl

Publicado: Mar Jul 02, 2013 11:18 pm
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

rgb to/from hsl

Publicado: Mar Jul 02, 2013 11:22 pm
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

rgb to/from hsl

Publicado: Mié Jul 03, 2013 8:55 am
por joselalinf
Roberto,
hb_storvnl is a Harbour function. If you want to use xHarbour change it to
hb_stornl.
Regards,
José Lalí­n