Calcular color con brillo

Calcular color con brillo

Postby hmpaquito » Tue Dec 27, 2016 5: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: Select all  Expand view
nBrightness: = Get_Brightness (CLR_HGRAY)

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


Gracias por vuestra atención.
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: Calcular color con brillo

Postby Carlos Mora » 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: Select all  Expand view

/**
 * Converts an RGB color value to HSL. Conversion formula
 * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
 * 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 http://en.wikipedia.org/wiki/HSL_color_space.
 * 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"
Carlos Mora
 
Posts: 988
Joined: Thu Nov 24, 2005 3:01 pm
Location: Madrid, España

Re: Calcular color con brillo

Postby hmpaquito » Wed Dec 28, 2016 8:36 am

Gracias Carlos,

Migraré esas funciones y veré si funciona.

Yo comento el resultado.

De nuevo, muchas gracias.

Saludos
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

Re: Calcular color con brillo

Postby nageswaragunupudi » Fri Dec 30, 2016 8: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
User avatar
nageswaragunupudi
 
Posts: 10295
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Calcular color con brillo

Postby hmpaquito » Fri Dec 30, 2016 8:29 am

Mr. Rao,

Thks for your support.
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 63 guests