1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
30 using System.Drawing.Drawing2D;
31 using System.Drawing.Imaging;
33 namespace System.Windows.Forms {
34 public sealed class ControlPaint {
35 #region Local Variables
36 static int RGBMax=255;
37 static int HLSMax=255;
38 #endregion // Local Variables
40 #region Private Enumerations
44 // Prevent a public constructor from being created
45 private ControlPaint() {
47 #endregion // Constructor
50 #endregion // Private Enumerations
53 private static Color Win32ToColor(int Win32Color) {
54 return(Color.FromArgb(
55 (int)(Win32Color) & 0xff0000 >> 16, // blue
56 (int)(Win32Color) & 0xff00 >> 8, // green
57 (int)(Win32Color) & 0xff // red
61 internal static void Color2HBS(Color color, out int h, out int l, out int s) {
75 cMax = Math.Max(Math.Max(r, g), b);
76 cMin = Math.Min(Math.Min(r, g), b);
78 l = (((cMax+cMin)*HLSMax)+RGBMax)/(2*RGBMax);
80 if (cMax==cMin) { // Achromatic
88 s=(((cMax-cMin)*HLSMax)+((cMax+cMin)/2))/(cMax+cMin);
90 s=(((cMax-cMin)*HLSMax)+((2*RGBMax-cMax-cMin)/2))/(2*RGBMax-cMax-cMin);
94 rDelta=(((cMax-r)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
95 gDelta=(((cMax-g)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
96 bDelta=(((cMax-b)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
100 } else if (g == cMax) {
101 h=(HLSMax/3) + rDelta - bDelta;
102 } else { /* B == cMax */
103 h=((2*HLSMax)/3) + gDelta - rDelta;
115 private static int HueToRGB(int n1, int n2, int hue) {
124 /* return r,g, or b value from this tridrant */
125 if (hue<(HLSMax/6)) {
126 return(n1+(((n2-n1)*hue+(HLSMax/12))/(HLSMax/6)));
129 if (hue<(HLSMax/2)) {
133 if (hue<((HLSMax*2)/3)) {
134 return(n1+(((n2-n1)*(((HLSMax*2)/3)-hue)+(HLSMax/12))/(HLSMax/6)));
140 internal static Color HBS2Color(int hue, int lum, int sat) {
147 if (sat == 0) { /* Achromatic */
148 R=G=B=(lum*RGBMax)/HLSMax;
149 // FIXME : Should throw exception if hue!=0
151 if (lum<=(HLSMax/2)) {
152 Magic2=(lum*(HLSMax+sat)+(HLSMax/2))/HLSMax;
154 Magic2=sat+lum-((sat*lum)+(HLSMax/2))/HLSMax;
158 R = Math.Min(255, (HueToRGB(Magic1,Magic2,hue+(HLSMax/3))*RGBMax+(HLSMax/2))/HLSMax);
159 G = Math.Min(255, (HueToRGB(Magic1,Magic2,hue)*RGBMax+(HLSMax/2))/HLSMax);
160 B = Math.Min(255, (HueToRGB(Magic1,Magic2,hue-(HLSMax/3))*RGBMax+(HLSMax/2))/HLSMax);
162 return(Color.FromArgb(R, G, B));
164 #endregion // Helpers
166 #region Public Static Properties
167 public static Color ContrastControlDark {
168 get { return(SystemColors.ControlDark); }
170 #endregion // Public Static Properties
172 #region Public Static Methods
173 public static IntPtr CreateHBitmap16Bit(Bitmap bitmap, Color background){
174 throw new NotImplementedException ();
177 public static IntPtr CreateHBitmapColorMask(Bitmap bitmap, IntPtr monochromeMask){
178 throw new NotImplementedException ();
181 public static IntPtr CreateHBitmapTransparencyMask(Bitmap bitmap){
182 throw new NotImplementedException ();
185 public static Color Light(Color baseColor) {
186 if (baseColor == ThemeEngine.Current.ColorButtonFace) {
187 return ThemeEngine.Current.ColorButtonLight;
190 return Light( baseColor, 10.0f);
193 public static Color Light(Color baseColor,float percOfLightLight) {
196 ControlPaint.Color2HBS(baseColor, out H, out I, out S);
197 int NewIntensity = Math.Min( 255, I + ((255*(int)percOfLightLight)/100));
198 return ControlPaint.HBS2Color(H, NewIntensity, S);
201 public static Color LightLight(Color baseColor) {
202 if (baseColor == ThemeEngine.Current.ColorButtonFace) {
203 return ThemeEngine.Current.ColorButtonHilight;
206 return Light( baseColor, 20.0f);
209 public static Color Dark(Color baseColor) {
210 if (baseColor == ThemeEngine.Current.ColorButtonFace) {
211 return ThemeEngine.Current.ColorButtonShadow;
214 return Dark(baseColor, 10.0f);
217 public static Color Dark(Color baseColor,float percOfDarkDark) {
220 ControlPaint.Color2HBS(baseColor, out H, out I, out S);
221 int NewIntensity = Math.Max(0, I - ((255*(int)percOfDarkDark) / 100));
222 return ControlPaint.HBS2Color(H, NewIntensity, S);
225 public static Color DarkDark(Color baseColor) {
226 if (baseColor == ThemeEngine.Current.ColorButtonFace) {
227 return ThemeEngine.Current.ColorButtonDkShadow;
230 return Dark(baseColor, 20.0f);
233 public static void DrawBorder(Graphics graphics, Rectangle bounds, Color color, ButtonBorderStyle style) {
234 DrawBorder(graphics, bounds, color, 1, style, color, 1, style, color, 1, style, color, 1, style);
237 public static void DrawBorder( Graphics graphics, Rectangle bounds, Color leftColor, int leftWidth,
238 ButtonBorderStyle leftStyle, Color topColor, int topWidth, ButtonBorderStyle topStyle,
239 Color rightColor, int rightWidth, ButtonBorderStyle rightStyle, Color bottomColor, int bottomWidth,
240 ButtonBorderStyle bottomStyle) {
242 ThemeEngine.Current.CPDrawBorder (graphics, bounds, leftColor, leftWidth,
243 leftStyle, topColor, topWidth, topStyle, rightColor, rightWidth, rightStyle,
244 bottomColor, bottomWidth, bottomStyle);
248 public static void DrawBorder3D(Graphics graphics, Rectangle rectangle) {
249 DrawBorder3D(graphics, rectangle, Border3DStyle.Etched, Border3DSide.All);
252 public static void DrawBorder3D(Graphics graphics, Rectangle rectangle, Border3DStyle style) {
253 DrawBorder3D(graphics, rectangle, style, Border3DSide.All);
256 public static void DrawBorder3D(Graphics graphics, int x, int y, int width, int height) {
257 DrawBorder3D(graphics, new Rectangle(x, y, width, height), Border3DStyle.Etched, Border3DSide.All);
260 public static void DrawBorder3D(Graphics graphics, int x, int y, int width, int height, Border3DStyle style) {
261 DrawBorder3D(graphics, new Rectangle(x, y, width, height), style, Border3DSide.All);
264 public static void DrawBorder3D( Graphics graphics, int x, int y, int width, int height, Border3DStyle style,Border3DSide sides) {
265 DrawBorder3D( graphics, new Rectangle(x, y, width, height), style, sides);
268 public static void DrawBorder3D( Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides) {
270 ThemeEngine.Current.CPDrawBorder3D (graphics, rectangle, style, sides);
273 public static void DrawButton( Graphics graphics, int x, int y, int width, int height, ButtonState state) {
274 DrawButton(graphics, new Rectangle(x, y, width, height), state);
277 public static void DrawButton( Graphics graphics, Rectangle rectangle, ButtonState state) {
279 ThemeEngine.Current.CPDrawButton (graphics, rectangle, state);
283 public static void DrawCaptionButton(Graphics graphics, int x, int y, int width, int height, CaptionButton button, ButtonState state) {
284 DrawCaptionButton(graphics, new Rectangle(x, y, width, height), button, state);
287 public static void DrawCaptionButton(Graphics graphics, Rectangle rectangle, CaptionButton button, ButtonState state) {
289 ThemeEngine.Current.CPDrawCaptionButton (graphics, rectangle, button, state);
292 public static void DrawCheckBox(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
293 DrawCheckBox(graphics, new Rectangle(x, y, width, height), state);
296 public static void DrawCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
298 ThemeEngine.Current.CPDrawCheckBox (graphics, rectangle, state);
301 public static void DrawComboButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
303 ThemeEngine.Current.CPDrawComboButton (graphics, rectangle, state);
306 public static void DrawComboButton(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
307 DrawComboButton(graphics, new Rectangle(x, y, width, height), state);
310 public static void DrawContainerGrabHandle(Graphics graphics, Rectangle bounds) {
312 ThemeEngine.Current.CPDrawContainerGrabHandle (graphics, bounds);
315 public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle) {
316 DrawFocusRectangle(graphics, rectangle, SystemColors.Control, SystemColors.ControlText);
319 public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle, Color foreColor, Color backColor) {
321 ThemeEngine.Current.CPDrawFocusRectangle (graphics, rectangle, foreColor, backColor);
324 public static void DrawGrabHandle(Graphics graphics, Rectangle rectangle, bool primary, bool enabled) {
326 ThemeEngine.Current.CPDrawGrabHandle (graphics, rectangle, primary, enabled);
329 public static void DrawGrid(Graphics graphics, Rectangle area, Size pixelsBetweenDots, Color backColor) {
331 ThemeEngine.Current.CPDrawGrid (graphics, area, pixelsBetweenDots, backColor);
334 public static void DrawImageDisabled(Graphics graphics, Image image, int x, int y, Color background) {
336 ThemeEngine.Current.CPDrawImageDisabled (graphics, image, x, y, background);
339 public static void DrawLockedFrame(Graphics graphics, Rectangle rectangle, bool primary) {
341 ThemeEngine.Current.CPDrawLockedFrame (graphics, rectangle, primary);
344 public static void DrawMenuGlyph(Graphics graphics, Rectangle rectangle, MenuGlyph glyph) {
346 ThemeEngine.Current.CPDrawMenuGlyph (graphics, rectangle, glyph);
349 public static void DrawMenuGlyph(Graphics graphics, int x, int y, int width, int height, MenuGlyph glyph) {
350 DrawMenuGlyph(graphics, new Rectangle(x, y, width, height), glyph);
353 public static void DrawMixedCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
354 DrawCheckBox(graphics, rectangle, state);
357 public static void DrawMixedCheckBox(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
358 DrawMixedCheckBox(graphics, new Rectangle(x, y, width, height), state);
362 public static void DrawRadioButton(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
363 DrawRadioButton(graphics, new Rectangle(x, y, width, height), state);
366 public static void DrawRadioButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
368 ThemeEngine.Current.CPDrawRadioButton (graphics, rectangle, state);
371 [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
372 private static bool DRFNotImpl = false;
373 public static void DrawReversibleFrame(Rectangle rectangle, Color backColor, FrameStyle style) {
376 Console.WriteLine("NOT IMPLEMENTED: FillReversibleRectangle(Rectangle rectangle, Color backColor)");
378 //throw new NotImplementedException();
381 [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
382 private static bool DRLNotImpl = false;
383 public static void DrawReversibleLine(Point start, Point end, Color backColor) {
386 Console.WriteLine("NOT IMPLEMENTED: FillReversibleRectangle(Rectangle rectangle, Color backColor)");
388 //throw new NotImplementedException();
391 [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
392 private static bool FRRNotImpl = false;
393 public static void FillReversibleRectangle(Rectangle rectangle, Color backColor) {
396 Console.WriteLine("NOT IMPLEMENTED: FillReversibleRectangle(Rectangle rectangle, Color backColor)");
398 //throw new NotImplementedException();
402 public static void DrawScrollButton (Graphics graphics, int x, int y, int width, int height, ScrollButton button, ButtonState state) {
403 ThemeEngine.Current.CPDrawScrollButton (graphics, new Rectangle(x, y, width, height), button, state);
406 public static void DrawScrollButton (Graphics graphics, Rectangle rectangle, ScrollButton button, ButtonState state) {
407 ThemeEngine.Current.CPDrawScrollButton (graphics, rectangle, button, state);
411 private static bool DSFNotImpl = false;
412 public static void DrawSelectionFrame(Graphics graphics, bool active, Rectangle outsideRect, Rectangle insideRect, Color backColor) {
415 Console.WriteLine("NOT IMPLEMENTED: DrawSelectionFrame(Graphics graphics, bool active, Rectangle outsideRect, Rectangle insideRect, Color backColor)");
417 //throw new NotImplementedException();
420 public static void DrawSizeGrip (Graphics graphics, Color backColor, Rectangle bounds)
422 ThemeEngine.Current.CPDrawSizeGrip (graphics, backColor, bounds);
425 public static void DrawSizeGrip(Graphics graphics, Color backColor, int x, int y, int width, int height) {
426 DrawSizeGrip(graphics, backColor, new Rectangle(x, y, width, height));
429 public static void DrawStringDisabled(Graphics graphics, string s, Font font, Color color, RectangleF layoutRectangle, StringFormat format) {
431 ThemeEngine.Current.CPDrawStringDisabled (graphics, s, font, color, layoutRectangle, format);
433 #endregion // Public Static Methods