Color with Transparency

Color with Transparency

Postby James Bott » Sat Mar 19, 2016 5:08 pm

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
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Color with Transparency

Postby Antonio Linares » Sat Mar 19, 2016 5:17 pm

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 view
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: 42076
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Color with Transparency

Postby Antonio Linares » Sat Mar 19, 2016 9:19 pm

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
Antonio Linares
Site Admin
 
Posts: 42076
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Color with Transparency

Postby James Bott » Mon Mar 21, 2016 2:41 pm

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 view
'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

Re: Color with Transparency

Postby James Bott » Mon Mar 21, 2016 4:21 pm

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 view
#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
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Color with Transparency

Postby Antonio Linares » Mon Mar 21, 2016 4:53 pm

very good :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42076
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Color with Transparency

Postby nageswaragunupudi » Mon Mar 21, 2016 11:56 pm

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


Code: Select all  Expand view

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
nageswaragunupudi
 
Posts: 10620
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Color with Transparency

Postby James Bott » Tue Mar 22, 2016 2:27 pm

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
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Color with Transparency

Postby Antonio Linares » Tue Mar 22, 2016 4:01 pm

James,

not yet released
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42076
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 104 guests