1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        src/ribbon/art_internal.cpp
3 // Purpose:     Helper functions & classes used by ribbon art providers
4 // Author:      Peter Cawley
5 // Modified by:
6 // Created:     2009-08-04
7 // Copyright:   (C) Peter Cawley
8 // Licence:     wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10 
11 #include "wx/wxprec.h"
12 
13 #ifdef __BORLANDC__
14     #pragma hdrstop
15 #endif
16 
17 #if wxUSE_RIBBON
18 
19 #include "wx/ribbon/art.h"
20 #include "wx/ribbon/art_internal.h"
21 #include "wx/ribbon/bar.h"
22 #include "wx/ribbon/buttonbar.h"
23 #include "wx/ribbon/gallery.h"
24 
25 #ifndef WX_PRECOMP
26 #include "wx/dc.h"
27 #endif
28 
29 #ifdef __WXMSW__
30 #include "wx/msw/private.h"
31 #endif
32 
wxRibbonArtProvider()33 wxRibbonArtProvider::wxRibbonArtProvider() {}
~wxRibbonArtProvider()34 wxRibbonArtProvider::~wxRibbonArtProvider() {}
35 
wxRibbonInterpolateColour(const wxColour & start_colour,const wxColour & end_colour,int position,int start_position,int end_position)36 wxColour wxRibbonInterpolateColour(const wxColour& start_colour,
37                                 const wxColour& end_colour,
38                                 int position,
39                                 int start_position,
40                                 int end_position)
41 {
42     if(position <= start_position)
43     {
44         return start_colour;
45     }
46     if(position >= end_position)
47     {
48         return end_colour;
49     }
50     position -= start_position;
51     end_position -= start_position;
52     int r = end_colour.Red() - start_colour.Red();
53     int g = end_colour.Green() - start_colour.Green();
54     int b = end_colour.Blue() - start_colour.Blue();
55     r = start_colour.Red()   + (((r * position * 100) / end_position) / 100);
56     g = start_colour.Green() + (((g * position * 100) / end_position) / 100);
57     b = start_colour.Blue()  + (((b * position * 100) / end_position) / 100);
58     return wxColour(r, g, b);
59 }
60 
wxRibbonCanLabelBreakAtPosition(const wxString & label,size_t pos)61 bool wxRibbonCanLabelBreakAtPosition(const wxString& label, size_t pos)
62 {
63     return label[pos] == ' ';
64 }
65 
wxRibbonDrawParallelGradientLines(wxDC & dc,int nlines,const wxPoint * line_origins,int stepx,int stepy,int numsteps,int offset_x,int offset_y,const wxColour & start_colour,const wxColour & end_colour)66 void wxRibbonDrawParallelGradientLines(wxDC& dc,
67                                     int nlines,
68                                     const wxPoint* line_origins,
69                                     int stepx,
70                                     int stepy,
71                                     int numsteps,
72                                     int offset_x,
73                                     int offset_y,
74                                     const wxColour& start_colour,
75                                     const wxColour& end_colour)
76 {
77     int rd, gd, bd;
78     rd = end_colour.Red() - start_colour.Red();
79     gd = end_colour.Green() - start_colour.Green();
80     bd = end_colour.Blue() - start_colour.Blue();
81 
82     for (int step = 0; step < numsteps; ++step)
83     {
84         int r,g,b;
85 
86         r = start_colour.Red() + (((step*rd*100)/numsteps)/100);
87         g = start_colour.Green() + (((step*gd*100)/numsteps)/100);
88         b = start_colour.Blue() + (((step*bd*100)/numsteps)/100);
89 
90         wxPen p(wxColour((unsigned char)r,
91                         (unsigned char)g,
92                         (unsigned char)b));
93         dc.SetPen(p);
94 
95         for(int n = 0; n < nlines; ++n)
96         {
97             dc.DrawLine(offset_x + line_origins[n].x, offset_y + line_origins[n].y,
98                         offset_x + line_origins[n].x + stepx, offset_y + line_origins[n].y + stepy);
99         }
100 
101         offset_x += stepx;
102         offset_y += stepy;
103     }
104 }
105 
wxRibbonShiftLuminance(wxRibbonHSLColour colour,float amount)106 wxRibbonHSLColour wxRibbonShiftLuminance(wxRibbonHSLColour colour,
107                                                 float amount)
108 {
109     if(amount <= 1.0f)
110         return colour.Darker(colour.luminance * (1.0f - amount));
111     else
112         return colour.Lighter((1.0f - colour.luminance) * (amount - 1.0f));
113 }
114 
wxRibbonLoadPixmap(const char * const * bits,wxColour fore)115 wxBitmap wxRibbonLoadPixmap(const char* const* bits, wxColour fore)
116 {
117     wxImage xpm = wxBitmap(bits).ConvertToImage();
118     xpm.Replace(255, 0, 255, fore.Red(), fore.Green(), fore.Blue());
119     return wxBitmap(xpm);
120 }
121 
wxRibbonHSLColour(const wxColour & col)122 wxRibbonHSLColour::wxRibbonHSLColour(const wxColour& col)
123 {
124     float red = float(col.Red()) / 255.0;
125     float green = float(col.Green()) / 255.0;
126     float blue = float(col.Blue()) / 255.0;
127     float Min = wxMin(red, wxMin(green, blue));
128     float Max = wxMax(red, wxMax(green, blue));
129     luminance = 0.5 * (Max + Min);
130     if (Min == Max)
131     {
132         // colour is a shade of grey
133         hue = 0.0;
134         saturation = 0.0;
135     }
136     else
137     {
138         if(luminance <= 0.5)
139             saturation = (Max - Min) / (Max + Min);
140         else
141             saturation = (Max - Min) / (2.0 - (Max + Min));
142 
143         if(Max == red)
144         {
145             hue = 60.0 * (green - blue) / (Max - Min);
146             if(hue < 0.0)
147                 hue += 360.0;
148         }
149         else if(Max == green)
150         {
151             hue = 60.0 * (blue - red) / (Max - Min);
152             hue += 120.0;
153         }
154         else // Max == blue
155         {
156             hue = 60.0 * (red - green) / (Max - Min);
157             hue += 240.0;
158         }
159     }
160 }
161 
ToRGB() const162 wxColour wxRibbonHSLColour::ToRGB() const
163 {
164     float _hue = (hue - floor(hue / 360.0f) * 360.0f);
165     float _saturation = saturation;
166     float _luminance = luminance;
167     if(_saturation > 1.0) _saturation = 1.0;
168     if(_saturation < 0.0) _saturation = 0.0;
169     if(_luminance > 1.0) _luminance = 1.0;
170     if(_luminance < 0.0) _luminance = 0.0;
171 
172     float red, blue, green;
173     if(_saturation == 0.0)
174     {
175         // colour is a shade of grey
176         red = blue = green = _luminance;
177     }
178     else
179     {
180         double tmp2 = (_luminance < 0.5)
181            ? _luminance * (1.0 + _saturation)
182            : (_luminance + _saturation) - (_luminance * _saturation);
183         double tmp1 = 2.0 * _luminance - tmp2;
184 
185         double tmp3R = _hue + 120.0;
186         if(tmp3R > 360.0)
187             tmp3R -= 360.0;
188         if(tmp3R < 60.0)
189             red = tmp1 + (tmp2 - tmp1) * tmp3R / 60.0;
190         else if(tmp3R < 180.0)
191             red = tmp2;
192         else if(tmp3R < 240.0)
193             red = tmp1 + (tmp2 - tmp1) * (240.0 - tmp3R) / 60.0;
194         else
195             red = tmp1;
196 
197         double tmp3G = _hue;
198         if(tmp3G > 360.0)
199             tmp3G -= 360.0;
200         if(tmp3G < 60.0)
201             green = tmp1 + (tmp2 - tmp1) * tmp3G / 60.0;
202         else if(tmp3G < 180.0)
203             green = tmp2;
204         else if(tmp3G < 240.0)
205             green = tmp1 + (tmp2 - tmp1) * (240.0 - tmp3G) / 60.0;
206         else
207             green = tmp1;
208 
209         double tmp3B = _hue + 240.0;
210         if(tmp3B > 360.0)
211             tmp3B -= 360.0;
212         if(tmp3B < 60.0)
213             blue = tmp1 + (tmp2 - tmp1) * tmp3B / 60.0;
214         else if(tmp3B < 180.0)
215             blue = tmp2;
216         else if(tmp3B < 240.0)
217             blue = tmp1 + (tmp2 - tmp1) * (240.0 - tmp3B) / 60.0;
218         else
219             blue = tmp1;
220     }
221     return wxColour(
222         (unsigned char)(red * 255.0),
223         (unsigned char)(green * 255.0),
224         (unsigned char)(blue * 255.0));
225 }
226 
Darker(float delta) const227 wxRibbonHSLColour wxRibbonHSLColour::Darker(float delta) const
228 {
229     return Lighter(-delta);
230 }
231 
MakeDarker(float delta)232 wxRibbonHSLColour& wxRibbonHSLColour::MakeDarker(float delta)
233 {
234     luminance -= delta;
235     return *this;
236 }
237 
Lighter(float delta) const238 wxRibbonHSLColour wxRibbonHSLColour::Lighter(float delta) const
239 {
240     return wxRibbonHSLColour(hue, saturation, luminance + delta);
241 }
242 
Saturated(float delta) const243 wxRibbonHSLColour wxRibbonHSLColour::Saturated(float delta) const
244 {
245     return wxRibbonHSLColour(hue, saturation + delta, luminance);
246 }
247 
Desaturated(float delta) const248 wxRibbonHSLColour wxRibbonHSLColour::Desaturated(float delta) const
249 {
250     return Saturated(-delta);
251 }
252 
ShiftHue(float delta) const253 wxRibbonHSLColour wxRibbonHSLColour::ShiftHue(float delta) const
254 {
255     return wxRibbonHSLColour(hue + delta, saturation, luminance);
256 }
257 
258 #endif // wxUSE_RIBBON
259