Color with Transparency

Post Reply
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Color with Transparency

Post by James Bott »

I need to create the rgb equivalent of a color with opacity.

Cristobal and I have been trying to recreate the colors used in FW's menus. I discovered that Windows apps (those that come with Windows, like Notepad) didn't use standard windows colors, like COLOR_MENU. But after much searching I found that they are adjusting the transparency of the color to create a new RGB color.

So, in order to keep using standard windows colors for interfaces, I would like to just change the opacity (transparency, luminosity) like the Windows apps do. Does anyone know how to do this?

I found this on a Microsoft site:

---------------------------------
Using other colors

While the Windows theme defines a comprehensive set of theme parts, you may find that your program needs colors that aren't defined in the theme file. While you could hardwire such colors, a better approach is to derive colors from the theme or system colors. Strategically using this approach gives you all the benefits of using theme and system colors, but with much more flexibility.

For example, suppose you need a window background that is darker than the theme window background color. In the HSL color space, having a darker color means a color with a lower luminosity. Thus, you can derive a darker window background color using the following steps:
•Obtain the window background theme color RGB.
•Convert the RGB to its HSL value.
•Reduce the luminosity value (by, say, 20 percent).
•Convert back to RGB values.

https://msdn.microsoft.com/en-us/library/windows/desktop/dn742482(v=vs.85).aspx
------------------------------------------

Any ideas on how to do this?

James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
Antonio Linares
Site Admin
Posts: 42655
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 67 times
Been thanked: 96 times
Contact:

Re: Color with Transparency

Post by Antonio Linares »

James,

Here you have some C functions that you could use:

https://www.cs.rit.edu/~ncs/color/t_convert.html

Code: Select all | Expand

RGB to HSV & HSV to RGB

The Hue/Saturation/Value model was created by A. R. Smith in 1978. It is based on such intuitive color characteristics as tint, shade and tone (or family, purety and intensity). The coordinate system is cylindrical, and the colors are defined inside a hexcone. The hue value H runs from 0 to 360º. The saturation S is the degree of strength or purity and is from 0 to 1. Purity is how much white is added to the color, so S=1 makes the purest color (no white). Brightness V also ranges from 0 to 1, where 0 is the black.

There is no transformation matrix for RGB/HSV conversion, but the algorithm follows:

// r,g,b values are from 0 to 1
// h = [0,360], s = [0,1], v = [0,1]
//      if s == 0, then h = -1 (undefined)
void RGBtoHSV( float r, float g, float b, float *h, float *s, float *v )
{
    float min, max, delta;
    min = MIN( r, g, b );
    max = MAX( r, g, b );
    *v = max;               // v
    delta = max - min;
    if( max != 0 )
        *s = delta / max;       // s
    else {
        // r = g = b = 0        // s = 0, v is undefined
        *s = 0;
        *h = -1;
        return;
    }
    if( r == max )
        *h = ( g - b ) / delta;     // between yellow & magenta
    else if( g == max )
        *h = 2 + ( b - r ) / delta; // between cyan & yellow
    else
        *h = 4 + ( r - g ) / delta; // between magenta & cyan
    *h *= 60;               // degrees
    if( *h < 0 )
        *h += 360;
}
void HSVtoRGB( float *r, float *g, float *b, float h, float s, float v )
{
    int i;
    float f, p, q, t;
    if( s == 0 ) {
        // achromatic (grey)
        *r = *g = *b = v;
        return;
    }
    h /= 60;            // sector 0 to 5
    i = floor( h );
    f = h - i;          // factorial part of h
    p = v * ( 1 - s );
    q = v * ( 1 - s * f );
    t = v * ( 1 - s * ( 1 - f ) );
    switch( i ) {
        case 0:
            *r = v;
            *g = t;
            *b = p;
            break;
        case 1:
            *r = q;
            *g = v;
            *b = p;
            break;
        case 2:
            *r = p;
            *g = v;
            *b = t;
            break;
        case 3:
            *r = p;
            *g = q;
            *b = v;
            break;
        case 4:
            *r = t;
            *g = p;
            *b = v;
            break;
        default:        // case 5:
            *r = v;
            *g = p;
            *b = q;
            break;
    }
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42655
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 67 times
Been thanked: 96 times
Contact:

Re: Color with Transparency

Post by Antonio Linares »

James,

FWH provides a function LightColor( nScale, nRGBColor ) --> nRGBLightColor

There are samples of its use in Class TGraph
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: Color with Transparency

Post by James Bott »

Antonio,

Thanks for the feedback. I am still working on this. I am also trying to convert the color number returned from the RGB() function back to RGB values. I found this example on the web:

Code: Select all | Expand

'assumes lReturnValue contains the RGB color from GetSysColor

    Dim lBlue As Long
    Dim lGreen As Long
    Dim lRed As Long
   
    lBlue = (lReturnValue \ 65536) And &HFF&
    lGreen = (lReturnValue \ 256) And &HFF&
    lRed = lReturnValue And &HFF&

Source: http://www.xtremevbtalk.com/interface-and-graphics/164878-getsyscolor-rgb.html

But I don't know how to convert this to FWH. I don't know what &HFF& stands for and/or how to implement it in FW. Anyone?

James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: Color with Transparency

Post by James Bott »

Ok, I have found the answer. This is how to use the number returned from the RGB() function to get back the Red, Green, and Blue values.

James

Code: Select all | Expand

#include "fivewin.ch"

Function Main()
   local nRGBColor := rgb( 204,232,255 )
   
   msgInfo( nAnd( nRGBColor , 255 ), "Red" )
   msgInfo( nAnd( nRGBColor/256, 255), "Green" )
   msgInfo( nAnd( nRGBColor/65536, 255), "Blue" )
   
Return nil
Last edited by James Bott on Mon Mar 21, 2016 6:12 pm, edited 1 time in total.
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
Antonio Linares
Site Admin
Posts: 42655
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 67 times
Been thanked: 96 times
Contact:

Re: Color with Transparency

Post by Antonio Linares »

very good :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
nageswaragunupudi
Posts: 10733
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Been thanked: 11 times
Contact:

Re: Color with Transparency

Post by nageswaragunupudi »

•Convert the RGB to its HSL value.
•Reduce the luminosity value (by, say, 20 percent).
•Convert back to RGB values.


Code: Select all | Expand


aHSL  := RGBTOHSL( nRGBColor )
aHSL[ 3 ] := Max( 0, aHSL[ 3 ] - 20 )
nRGBNew := HSLTORGB( aHSL )[ 4 ]
 

These functions are available in the FWH 1603 under release.
Regards

G. N. Rao.
Hyderabad, India
User avatar
James Bott
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: Color with Transparency

Post by James Bott »

Nages,

Great, I am anxious to try them.

These functions are available in the FWH 1603 under release.


Did you mean, "not yet released," or "already released?"

James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
Antonio Linares
Site Admin
Posts: 42655
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 67 times
Been thanked: 96 times
Contact:

Re: Color with Transparency

Post by Antonio Linares »

James,

not yet released
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply