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