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
26 // $Log: ControlPaint.cs,v $
27 // Revision 1.3 2004/08/11 22:20:59 pbartok
30 // Revision 1.2 2004/07/26 17:42:03 jordi
33 // Revision 1.1 2004/07/09 05:21:25 pbartok
41 using System.Drawing.Drawing2D;
42 using System.Drawing.Imaging;
44 namespace System.Windows.Forms {
45 public sealed class ControlPaint {
46 #region Local Variables
47 static int RGBMax=255;
48 static int HLSMax=255;
49 #endregion // Local Variables
51 #region Private Enumerations
55 // Prevent a public constructor from being created
56 private ControlPaint() {
58 #endregion // Constructor
61 #endregion // Private Enumerations
64 private static Color Win32ToColor(int Win32Color) {
65 return(Color.FromArgb(
66 (int)(Win32Color) & 0xff0000 >> 16, // blue
67 (int)(Win32Color) & 0xff00 >> 8, // green
68 (int)(Win32Color) & 0xff // red
72 internal static void Color2HBS(Color color, out int h, out int l, out int s) {
86 cMax = Math.Max(Math.Max(r, g), b);
87 cMin = Math.Min(Math.Min(r, g), b);
89 l = (((cMax+cMin)*HLSMax)+RGBMax)/(2*RGBMax);
91 if (cMax==cMin) { // Achromatic
100 s=(((cMax-cMin)*HLSMax)+((cMax+cMin)/2))/(cMax+cMin);
102 s=(((cMax-cMin)*HLSMax)+((2*RGBMax-cMax-cMin)/2))/(2*RGBMax-cMax-cMin);
106 rDelta=(((cMax-r)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
107 gDelta=(((cMax-g)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
108 bDelta=(((cMax-b)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
112 } else if (g == cMax) {
113 h=(HLSMax/3) + rDelta - bDelta;
114 } else { /* B == cMax */
115 h=((2*HLSMax)/3) + gDelta - rDelta;
127 private static int HueToRGB(int n1, int n2, int hue) {
136 /* return r,g, or b value from this tridrant */
137 if (hue<(HLSMax/6)) {
138 return(n1+(((n2-n1)*hue+(HLSMax/12))/(HLSMax/6)));
141 if (hue<(HLSMax/2)) {
145 if (hue<((HLSMax*2)/3)) {
146 return(n1+(((n2-n1)*(((HLSMax*2)/3)-hue)+(HLSMax/12))/(HLSMax/6)));
152 internal static Color HBS2Color(int hue, int lum, int sat) {
159 if (sat == 0) { /* Achromatic */
160 R=G=B=(lum*RGBMax)/HLSMax;
161 // FIXME : Should throw exception if hue!=0
163 if (lum<=(HLSMax/2)) {
164 Magic2=(lum*(HLSMax+sat)+(HLSMax/2))/HLSMax;
166 Magic2=sat+lum-((sat*lum)+(HLSMax/2))/HLSMax;
170 R = Math.Min(255, (HueToRGB(Magic1,Magic2,hue+(HLSMax/3))*RGBMax+(HLSMax/2))/HLSMax);
171 G = Math.Min(255, (HueToRGB(Magic1,Magic2,hue)*RGBMax+(HLSMax/2))/HLSMax);
172 B = Math.Min(255, (HueToRGB(Magic1,Magic2,hue-(HLSMax/3))*RGBMax+(HLSMax/2))/HLSMax);
174 return(Color.FromArgb(R, G, B));
176 #endregion // Helpers
178 #region Public Static Properties
179 public static Color ContrastControlDark {
180 get { return(SystemColors.ControlDark); }
182 #endregion // Public Static Properties
184 #region Public Static Methods
185 public static IntPtr CreateHBitmap16Bit(Bitmap bitmap, Color background){
186 throw new NotImplementedException ();
189 public static IntPtr CreateHBitmapColorMask(Bitmap bitmap, IntPtr monochromeMask){
190 throw new NotImplementedException ();
193 public static IntPtr CreateHBitmapTransparencyMask(Bitmap bitmap){
194 throw new NotImplementedException ();
197 public static Color Light(Color baseColor) {
198 return Light( baseColor, 10.0f);
201 public static Color Light(Color baseColor,float percOfLightLight) {
204 ControlPaint.Color2HBS(baseColor, out H, out I, out S);
205 int NewIntensity = Math.Min( 255, I + ((255*(int)percOfLightLight)/100));
206 return ControlPaint.HBS2Color(H, NewIntensity, S);
209 public static Color LightLight(Color baseColor) {
210 return Light( baseColor, 20.0f);
213 public static Color Dark(Color baseColor) {
214 return Dark(baseColor, 10.0f);
217 public static Color Dark(Color baseColor,float percOfDarkDark) {
219 ControlPaint.Color2HBS(baseColor, out H, out I, out S);
220 int NewIntensity = Math.Max(0, I - ((255*(int)percOfDarkDark) / 100));
221 return ControlPaint.HBS2Color(H, NewIntensity, S);
224 public static Color DarkDark(Color baseColor) {
225 return Dark(baseColor, 20.0f);
228 public static void DrawBorder(Graphics graphics, Rectangle bounds, Color color, ButtonBorderStyle style) {
229 DrawBorder(graphics, bounds, color, 1, style, color, 1, style, color, 1, style, color, 1, style);
232 public static void DrawBorder( Graphics graphics, Rectangle bounds, Color leftColor, int leftWidth,
233 ButtonBorderStyle leftStyle, Color topColor, int topWidth, ButtonBorderStyle topStyle,
234 Color rightColor, int rightWidth, ButtonBorderStyle rightStyle, Color bottomColor, int bottomWidth,
235 ButtonBorderStyle bottomStyle) {
237 ThemeEngine.Current.DrawBorder (graphics, bounds, leftColor, leftWidth,
238 leftStyle, topColor, topWidth, topStyle, rightColor, rightWidth, rightStyle,
239 bottomColor, bottomWidth, bottomStyle);
243 public static void DrawBorder3D(Graphics graphics, Rectangle rectangle) {
244 DrawBorder3D(graphics, rectangle, Border3DStyle.Etched, Border3DSide.All);
247 public static void DrawBorder3D(Graphics graphics, Rectangle rectangle, Border3DStyle style) {
248 DrawBorder3D(graphics, rectangle, style, Border3DSide.All);
251 public static void DrawBorder3D(Graphics graphics, int x, int y, int width, int height) {
252 DrawBorder3D(graphics, new Rectangle(x, y, width, height), Border3DStyle.Etched, Border3DSide.All);
255 public static void DrawBorder3D(Graphics graphics, int x, int y, int width, int height, Border3DStyle style) {
256 DrawBorder3D(graphics, new Rectangle(x, y, width, height), style, Border3DSide.All);
259 public static void DrawBorder3D( Graphics graphics, int x, int y, int width, int height, Border3DStyle style,Border3DSide sides) {
260 DrawBorder3D( graphics, new Rectangle(x, y, width, height), style, sides);
263 public static void DrawBorder3D( Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides) {
265 ThemeEngine.Current.DrawBorder3D (graphics, rectangle, style, sides);
268 public static void DrawButton( Graphics graphics, int x, int y, int width, int height, ButtonState state) {
269 DrawButton(graphics, new Rectangle(x, y, width, height), state);
272 public static void DrawButton( Graphics graphics, Rectangle rectangle, ButtonState state) {
274 ThemeEngine.Current.DrawButton (graphics, rectangle, state);
278 public static void DrawCaptionButton(Graphics graphics, int x, int y, int width, int height, CaptionButton button, ButtonState state) {
279 DrawCaptionButton(graphics, new Rectangle(x, y, width, height), button, state);
282 public static void DrawCaptionButton(Graphics graphics, Rectangle rectangle, CaptionButton button, ButtonState state) {
284 ThemeEngine.Current.DrawCaptionButton (graphics, rectangle, button, state);
287 public static void DrawCheckBox(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
288 DrawCheckBox(graphics, new Rectangle(x, y, width, height), state);
291 public static void DrawCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
293 ThemeEngine.Current.DrawCheckBox (graphics, rectangle, state);
296 public static void DrawComboButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
298 ThemeEngine.Current.DrawComboButton (graphics, rectangle, state);
301 public static void DrawComboButton(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
302 DrawComboButton(graphics, new Rectangle(x, y, width, height), state);
305 public static void DrawContainerGrabHandle(Graphics graphics, Rectangle bounds) {
307 ThemeEngine.Current.DrawContainerGrabHandle (graphics, bounds);
310 public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle) {
311 DrawFocusRectangle(graphics, rectangle, Color.White, Color.Black);
314 public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle, Color foreColor, Color backColor) {
316 ThemeEngine.Current.DrawFocusRectangle (graphics, rectangle, foreColor, backColor);
319 public static void DrawGrabHandle(Graphics graphics, Rectangle rectangle, bool primary, bool enabled) {
321 ThemeEngine.Current.DrawGrabHandle (graphics, rectangle, primary, enabled);
324 public static void DrawGrid(Graphics graphics, Rectangle area, Size pixelsBetweenDots, Color backColor) {
326 ThemeEngine.Current.DrawGrid (graphics, area, pixelsBetweenDots, backColor);
329 public static void DrawImageDisabled(Graphics graphics, Image image, int x, int y, Color background) {
331 ThemeEngine.Current.DrawImageDisabled (graphics, image, x, y, background);
334 public static void DrawLockedFrame(Graphics graphics, Rectangle rectangle, bool primary) {
336 ThemeEngine.Current.DrawLockedFrame (graphics, rectangle, primary);
339 public static void DrawMenuGlyph(Graphics graphics, Rectangle rectangle, MenuGlyph glyph) {
341 ThemeEngine.Current.DrawMenuGlyph (graphics, rectangle, glyph);
344 public static void DrawMenuGlyph(Graphics graphics, int x, int y, int width, int height, MenuGlyph glyph) {
345 DrawMenuGlyph(graphics, new Rectangle(x, y, width, height), glyph);
348 public static void DrawMixedCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
349 DrawCheckBox(graphics, rectangle, state);
352 public static void DrawMixedCheckBox(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
353 DrawMixedCheckBox(graphics, new Rectangle(x, y, width, height), state);
357 public static void DrawRadioButton(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
358 DrawRadioButton(graphics, new Rectangle(x, y, width, height), state);
361 public static void DrawRadioButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
363 ThemeEngine.Current.DrawRadioButton (graphics, rectangle, state);
366 [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
367 public static void DrawReversibleFrame(Rectangle rectangle, Color backColor, FrameStyle style) {
368 throw new NotImplementedException();
371 [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
372 public static void DrawReversibleLine(Point start, Point end, Color backColor) {
373 throw new NotImplementedException();
376 [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
377 public static void FillReversibleRectangle(Rectangle rectangle, Color backColor) {
378 throw new NotImplementedException();
382 public static void DrawScrollButton (Graphics graphics, int x, int y, int width, int height, ScrollButton button, ButtonState state) {
383 ThemeEngine.Current.DrawScrollButton (graphics, new Rectangle(x, y, width, height), button, state);
386 public static void DrawScrollButton (Graphics graphics, Rectangle rectangle, ScrollButton button, ButtonState state) {
387 ThemeEngine.Current.DrawScrollButton (graphics, rectangle, button, state);
391 public static void DrawSelectionFrame(Graphics graphics, bool active, Rectangle outsideRect, Rectangle insideRect, Color backColor) {
392 throw new NotImplementedException();
395 public static void DrawSizeGrip (Graphics graphics, Color backColor, Rectangle bounds)
397 ThemeEngine.Current.DrawSizeGrip (graphics, backColor, bounds);
400 public static void DrawSizeGrip(Graphics graphics, Color backColor, int x, int y, int width, int height) {
401 DrawSizeGrip(graphics, backColor, new Rectangle(x, y, width, height));
404 public static void DrawStringDisabled(Graphics graphics, string s, Font font, Color color, RectangleF layoutRectangle, StringFormat format) {
406 ThemeEngine.Current.DrawStringDisabled (graphics, s, font, color, layoutRectangle, format);
408 #endregion // Public Static Methods