Changing Ribbon Colors - harborsiem/WinForms-Ribbon GitHub Wiki
Changing Ribbon Colors
Introduction to the feature The feature I want to talk about today is how to change the ribbon general colors. Note that you can’t change the colors of a specific ribbon item, only the entire ribbon.
There are 3 colors we can change:
-
Background Color
-
Highlight Color
-
Text Color
Here is an example of a colored ribbon:
How to do it? I’ve added new methods to the WinForms.Ribbon.RibbonStrip class. Following is an example of how to use it:
partial class RibbonItems
{
UI_HSBCOLOR _backgroundDefault;
UI_HSBCOLOR _highlightDefault;
UI_HSBCOLOR _textDefault;
UI_HSBCOLOR _appButtonColorDefault;
UI_HSBCOLOR _backgroundCurrent;
UI_HSBCOLOR _highlightCurrent;
UI_HSBCOLOR _textCurrent;
UI_HSBCOLOR _appButtonColorCurrent;
public void Init()
{
ToggleDark.ToggleChanged += ToggleDark_ToggleChanged;
ButtonDefaultColors.Click += ButtonDefaultColors_Click;
}
public void Load()
{
_backgroundDefault = Ribbon.GetBackgroundColor();
_highlightDefault = Ribbon.GetHighlightColor();
_textDefault = Ribbon.GetTextColor();
_appButtonColorDefault = Ribbon.GetApplicationButtonColor();
// set ribbon colors
Ribbon.SetBackgroundColor(_backgroundCurrent = new UI_HSBCOLOR(Color.Wheat));
Ribbon.SetHighlightColor(_highlightCurrent = new UI_HSBCOLOR(Color.IndianRed));
Ribbon.SetTextColor(_textCurrent = new UI_HSBCOLOR(Color.BlueViolet));
//Ribbon.SetApplicationButtonColor(_appButtonColorCurrent = new UI_HSBCOLOR(Color.BlueViolet));
}
}
Behind the scenes What the SetxxxColor method actually does is:
-
Get IPropertyStore interface from the IUIFramework (which represents the ribbon framework)
-
Create a PROPVARIANT variable that will hold the UI_HSBCOLOR we want to set
-
Convert the color: RGB –> HSL –> HSB –> uint, see next section.
-
Set the relevant properties by calling a suitable ctor UI_HSBCOLOR with the converted color value
Color Format I didn’t dive into the colors format world, so I’ll just give a quick reference:
RGB is the well-known color format that us, developers, like and understand.
RGB can be converted to HSL. This should be a common transformation. or as Microsoft wrote here, “easily accomplished with most photo editing software”. In this page Microsoft also gives the formulas for converting HSL to HSB.
You can find code for transforming RGB to HSL and vice versa at W3C or Wikipedia. The transformations are not lossless.
Finally, HSB is converted to uint by just OR-ing the values, much like RGB to uint conversion. I’ve encapsulated all these details in a helper struct named WinForms.Ribbon.UI_HSBCOLOR.
A new sample, named 07-RibbonColor, summarize the details of this post. Find it on the project site.