FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Calcular color con brillo
Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Calcular color con brillo
Posted: Tue Dec 27, 2016 05:04 PM
Hola,

Tengo configurada la aplicacion con colores propios.
El usuario selecciona unos colores base y el programa da color a todos los controles segun esos colores base y.... y el brillo del color original del control.

Es decir. La aplicacion está en tonos de grises. Según el color base y con la cantidad de brillo del control 'gris' se establece cada nuevo color.

Lo que yo pretendería es una funcion que dado un color y su brillo, y un color base, que devuelva el color nuevo.

No sé si me explico.


Lo que necesitaria seria una funcion llamada Get_MixColorBrightness(), que recibiendo como parametros un color y un brillo, devuelva ese color mismo color adaptado al parámetro brillo. Algo así:

Code (fw): Select all Collapse
nBrightness: = Get_Brightness (CLR_HGRAY) 

nColor: = Get_MixColorBrightness(CLR_GREEN, nBrightness) // nColor will be green but with Brightness nBrightness


Gracias por vuestra atención.
Posts: 989
Joined: Thu Nov 24, 2005 03:01 PM
Re: Calcular color con brillo
Posted: Tue Dec 27, 2016 11:10 PM
Estimado,

Lo que quieres hacer pasa por transformar el RGB a HSL, cambiar la luminosidad (L) y recomponer el RBG.

http://www.rapidtables.com/convert/color/rgb-to-hsl.htm

http://www.rapidtables.com/convert/color/hsl-to-rgb.htm

Javascripticamente hablando:
Code (fw): Select all Collapse
/**
 * Converts an RGB color value to HSL. Conversion formula
 * adapted from <!-- m --><a class="postlink" href="http://en.wikipedia.org/wiki/HSL_color_space">http://en.wikipedia.org/wiki/HSL_color_space</a><!-- m -->.
 * Assumes r, g, and b are contained in the set [0, 255] and
 * returns h, s, and l in the set [0, 1].
 *
 * @param   {number}  r       The red color value
 * @param   {number}  g       The green color value
 * @param   {number}  b       The blue color value
 * @return  {Array}           The HSL representation
 */
function rgbToHsl(r, g, b){
    r /= 255, g /= 255, b /= 255;
    var max = Math.max(r, g, b), min = Math.min(r, g, b);
    var h, s, l = (max + min) / 2;

    if(max == min){
        h = s = 0; // achromatic
    }else{
        var d = max - min;
        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
        switch(max){
            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
            case g: h = (b - r) / d + 2; break;
            case b: h = (r - g) / d + 4; break;
        }
        h /= 6;
    }

    return [h, s, l];
}

/**
 * Converts an HSL color value to RGB. Conversion formula
 * adapted from <!-- m --><a class="postlink" href="http://en.wikipedia.org/wiki/HSL_color_space">http://en.wikipedia.org/wiki/HSL_color_space</a><!-- m -->.
 * Assumes h, s, and l are contained in the set [0, 1] and
 * returns r, g, and b in the set [0, 255].
 *
 * @param   {number}  h       The hue
 * @param   {number}  s       The saturation
 * @param   {number}  l       The lightness
 * @return  {Array}           The RGB representation
 */
function hslToRgb(h, s, l){
    var r, g, b;

    if(s == 0){
        r = g = b = l; // achromatic
    }else{
        var hue2rgb = function hue2rgb(p, q, t){
            if(t < 0) t += 1;
            if(t > 1) t -= 1;
            if(t < 1/6) return p + (q - p) * 6 * t;
            if(t < 1/2) return q;
            if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
            return p;
        }

        var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
        var p = 2 * l - q;
        r = hue2rgb(p, q, h + 1/3);
        g = hue2rgb(p, q, h);
        b = hue2rgb(p, q, h - 1/3);
    }

    return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
Saludos
Carlos Mora
http://harbouradvisor.blogspot.com/
StackOverflow http://stackoverflow.com/users/549761/carlos-mora
“If you think education is expensive, try ignorance"
Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Re: Calcular color con brillo
Posted: Wed Dec 28, 2016 08:36 AM

Gracias Carlos,

Migraré esas funciones y veré si funciona.

Yo comento el resultado.

De nuevo, muchas gracias.

Saludos

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Calcular color con brillo
Posted: Fri Dec 30, 2016 08:20 AM

FWH has inbuilt functions RGBTOHSL() and HSLTORGB()

Usage:
RGBTOHSL( nRGBColor )
or
RGBTOHSL( nRed, nGreen, nBlue )
or
RGBTOHSL( { nRed, nGreen, nBlue )

------> { nHue, nSatuation, nLuminence }

HSLTORGB( { h, s, l } ) --> { nRed, nGreen, nBlue, nRGB }

Regards



G. N. Rao.

Hyderabad, India
Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Re: Calcular color con brillo
Posted: Fri Dec 30, 2016 08:29 AM

Mr. Rao,

Thks for your support.

Continue the discussion