System color -consistent color schemes

Share your advanced PureBasic knowledge/code with the community.
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

System color -consistent color schemes

Post by eesau »

Hi.

Here's a trick that most MS Office-products use for coloring menus, controls and other GUI stuff. Maybe someone will find it useful.

The different GUI colors in Office -products are really close to, but not quite the same as the system colors you can get with GetSysColor API-function. They're usually a little brighter and not so bland as the system colors. And if you change your theme or your system color scheme, they change too. So they have to be linked to the system colors, right?

Right.

Here's the trick: MS Office creates highlights and other GUI colors by blending two of the default system colors. Using color blending creates a whole lot of new coloring possibilities while remaining consistent with the system coloring set by the user. Here's a bit of code I used to make a coloring scheme procedure for my Office-style menus. First, we need a macro for blending to colors:

Code: Select all

Macro AlphaBlend ( LColor , RColor , Alpha )

	RGB ( ( ( Red ( RColor ) * Alpha + Red ( LColor ) * ( 256 - Alpha ) ) / 256 ) , ( ( Green ( RColor ) * Alpha + Green ( LColor ) * ( 256 - Alpha ) ) / 256 ) , ( ( Blue ( RColor ) * Alpha + Blue ( LColor ) * ( 256 - Alpha ) ) / 256 ) )

EndMacro
You use it by giving it two colors (LColor and RColor in the code) and an alpha level. The closer the alpha level is to zero, the closer the resulting color is to LColor, and the other way around. Next, we need a bit of code that will handle our Office-like color scheme:

Code: Select all

Enumeration 

	#OfficeFaceBright
	#OfficeFaceDim
	#OfficeDisabled
	#OfficeOutline
	#OfficeSelected
	#OfficeSelectedOutline
	
EndEnumeration

Procedure OfficeColor ( Type )

	Select Type
	
		Case #OfficeFaceBright
		
			ProcedureReturn GetSysColor_ ( #COLOR_3DHIGHLIGHT )
			
		Case #OfficeFaceDim

			ProcedureReturn AlphaBlend ( GetSysColor_ ( #COLOR_3DFACE ) , GetSysColor_ ( #COLOR_3DHILIGHT ) , 55 )
					
		Case #OfficeDisabled
		
			ProcedureReturn AlphaBlend ( GetSysColor_ ( #COLOR_GRAYTEXT ) , GetSysColor_ ( #COLOR_3DHILIGHT ) , 77 )
	
		Case #OfficeOutline	
	
			ProcedureReturn AlphaBlend ( GetSysColor_ ( #COLOR_3DDKSHADOW ) , GetSysColor_ ( #COLOR_3DFACE ) , 115 )
	
		Case #OfficeSelected
			
			ProcedureReturn AlphaBlend ( GetSysColor_ ( #COLOR_ACTIVECAPTION ) , GetSysColor_ ( #COLOR_3DHIGHLIGHT ) , 179 )
			
		Case #OfficeSelectedOutline
	
			ProcedureReturn AlphaBlend ( GetSysColor_ ( #COLOR_ACTIVECAPTION ) , GetSysColor_ ( #COLOR_3DHIGHLIGHT ) , 65 )
	
	EndSelect

EndProcedure
As you can see, we just blend the system colors to create colors matching those found in Office. The resulting colors are 99% Office-accurate, no matter what theme or system color scheme you use (with the exception of some high-contrast color schemes -- you can easily add exceptions for those in the procedure above). With a little testing you can get 100% accuracy if needed.

By extending the procedure, changing the GetSysColor constants for LColor and RColor, and changing the alpha levels you can easily make your own system color consistent color schemes -- something that is too often overlooked these days.
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Post by eesau »

And here is the coloring code in action.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Thank you eesau.
Dare2 cut down to size
Post Reply