ControlPaint.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  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.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. //
  26. // $Log: ControlPaint.cs,v $
  27. // Revision 1.3 2004/08/11 22:20:59 pbartok
  28. // - Signature fixes
  29. //
  30. // Revision 1.2 2004/07/26 17:42:03 jordi
  31. // Theme support
  32. //
  33. // Revision 1.1 2004/07/09 05:21:25 pbartok
  34. // - Initial check-in
  35. //
  36. //
  37. // NOT COMPLETE
  38. using System.Drawing;
  39. using System.Drawing.Drawing2D;
  40. using System.Drawing.Imaging;
  41. namespace System.Windows.Forms {
  42. public sealed class ControlPaint {
  43. #region Local Variables
  44. static int RGBMax=255;
  45. static int HLSMax=255;
  46. #endregion // Local Variables
  47. #region Private Enumerations
  48. #region Constructor
  49. // Prevent a public constructor from being created
  50. private ControlPaint() {
  51. }
  52. #endregion // Constructor
  53. #endregion // Private Enumerations
  54. #region Helpers
  55. private static Color Win32ToColor(int Win32Color) {
  56. return(Color.FromArgb(
  57. (int)(Win32Color) & 0xff0000 >> 16, // blue
  58. (int)(Win32Color) & 0xff00 >> 8, // green
  59. (int)(Win32Color) & 0xff // red
  60. ));
  61. }
  62. internal static void Color2HBS(Color color, out int h, out int l, out int s) {
  63. int r;
  64. int g;
  65. int b;
  66. int cMax;
  67. int cMin;
  68. int rDelta;
  69. int gDelta;
  70. int bDelta;
  71. r=color.R;
  72. g=color.G;
  73. b=color.B;
  74. cMax = Math.Max(Math.Max(r, g), b);
  75. cMin = Math.Min(Math.Min(r, g), b);
  76. l = (((cMax+cMin)*HLSMax)+RGBMax)/(2*RGBMax);
  77. if (cMax==cMin) { // Achromatic
  78. h=0; // h undefined
  79. s=0;
  80. l=r;
  81. return;
  82. }
  83. /* saturation */
  84. if (l<=(HLSMax/2)) {
  85. s=(((cMax-cMin)*HLSMax)+((cMax+cMin)/2))/(cMax+cMin);
  86. } else {
  87. s=(((cMax-cMin)*HLSMax)+((2*RGBMax-cMax-cMin)/2))/(2*RGBMax-cMax-cMin);
  88. }
  89. /* hue */
  90. rDelta=(((cMax-r)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
  91. gDelta=(((cMax-g)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
  92. bDelta=(((cMax-b)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
  93. if (r == cMax) {
  94. h=bDelta - gDelta;
  95. } else if (g == cMax) {
  96. h=(HLSMax/3) + rDelta - bDelta;
  97. } else { /* B == cMax */
  98. h=((2*HLSMax)/3) + gDelta - rDelta;
  99. }
  100. if (h<0) {
  101. h+=HLSMax;
  102. }
  103. if (h>HLSMax) {
  104. h-=HLSMax;
  105. }
  106. }
  107. private static int HueToRGB(int n1, int n2, int hue) {
  108. if (hue<0) {
  109. hue+=HLSMax;
  110. }
  111. if (hue>HLSMax) {
  112. hue -= HLSMax;
  113. }
  114. /* return r,g, or b value from this tridrant */
  115. if (hue<(HLSMax/6)) {
  116. return(n1+(((n2-n1)*hue+(HLSMax/12))/(HLSMax/6)));
  117. }
  118. if (hue<(HLSMax/2)) {
  119. return(n2);
  120. }
  121. if (hue<((HLSMax*2)/3)) {
  122. return(n1+(((n2-n1)*(((HLSMax*2)/3)-hue)+(HLSMax/12))/(HLSMax/6)));
  123. } else {
  124. return(n1);
  125. }
  126. }
  127. internal static Color HBS2Color(int hue, int lum, int sat) {
  128. int R;
  129. int G;
  130. int B;
  131. int Magic1;
  132. int Magic2;
  133. if (sat == 0) { /* Achromatic */
  134. R=G=B=(lum*RGBMax)/HLSMax;
  135. // FIXME : Should throw exception if hue!=0
  136. } else {
  137. if (lum<=(HLSMax/2)) {
  138. Magic2=(lum*(HLSMax+sat)+(HLSMax/2))/HLSMax;
  139. } else {
  140. Magic2=sat+lum-((sat*lum)+(HLSMax/2))/HLSMax;
  141. }
  142. Magic1=2*lum-Magic2;
  143. R = Math.Min(255, (HueToRGB(Magic1,Magic2,hue+(HLSMax/3))*RGBMax+(HLSMax/2))/HLSMax);
  144. G = Math.Min(255, (HueToRGB(Magic1,Magic2,hue)*RGBMax+(HLSMax/2))/HLSMax);
  145. B = Math.Min(255, (HueToRGB(Magic1,Magic2,hue-(HLSMax/3))*RGBMax+(HLSMax/2))/HLSMax);
  146. }
  147. return(Color.FromArgb(R, G, B));
  148. }
  149. #endregion // Helpers
  150. #region Public Static Properties
  151. public static Color ContrastControlDark {
  152. get { return(SystemColors.ControlDark); }
  153. }
  154. #endregion // Public Static Properties
  155. #region Public Static Methods
  156. public static IntPtr CreateHBitmap16Bit(Bitmap bitmap, Color background){
  157. throw new NotImplementedException ();
  158. }
  159. public static IntPtr CreateHBitmapColorMask(Bitmap bitmap, IntPtr monochromeMask){
  160. throw new NotImplementedException ();
  161. }
  162. public static IntPtr CreateHBitmapTransparencyMask(Bitmap bitmap){
  163. throw new NotImplementedException ();
  164. }
  165. public static Color Light(Color baseColor) {
  166. return Light( baseColor, 10.0f);
  167. }
  168. public static Color Light(Color baseColor,float percOfLightLight) {
  169. int H, I, S;
  170. ControlPaint.Color2HBS(baseColor, out H, out I, out S);
  171. int NewIntensity = Math.Min( 255, I + ((255*(int)percOfLightLight)/100));
  172. return ControlPaint.HBS2Color(H, NewIntensity, S);
  173. }
  174. public static Color LightLight(Color baseColor) {
  175. return Light( baseColor, 20.0f);
  176. }
  177. public static Color Dark(Color baseColor) {
  178. return Dark(baseColor, 10.0f);
  179. }
  180. public static Color Dark(Color baseColor,float percOfDarkDark) {
  181. int H, I, S;
  182. ControlPaint.Color2HBS(baseColor, out H, out I, out S);
  183. int NewIntensity = Math.Max(0, I - ((255*(int)percOfDarkDark) / 100));
  184. return ControlPaint.HBS2Color(H, NewIntensity, S);
  185. }
  186. public static Color DarkDark(Color baseColor) {
  187. return Dark(baseColor, 20.0f);
  188. }
  189. public static void DrawBorder(Graphics graphics, Rectangle bounds, Color color, ButtonBorderStyle style) {
  190. DrawBorder(graphics, bounds, color, 1, style, color, 1, style, color, 1, style, color, 1, style);
  191. }
  192. public static void DrawBorder( Graphics graphics, Rectangle bounds, Color leftColor, int leftWidth,
  193. ButtonBorderStyle leftStyle, Color topColor, int topWidth, ButtonBorderStyle topStyle,
  194. Color rightColor, int rightWidth, ButtonBorderStyle rightStyle, Color bottomColor, int bottomWidth,
  195. ButtonBorderStyle bottomStyle) {
  196. ThemeEngine.Current.DrawBorder (graphics, bounds, leftColor, leftWidth,
  197. leftStyle, topColor, topWidth, topStyle, rightColor, rightWidth, rightStyle,
  198. bottomColor, bottomWidth, bottomStyle);
  199. }
  200. public static void DrawBorder3D(Graphics graphics, Rectangle rectangle) {
  201. DrawBorder3D(graphics, rectangle, Border3DStyle.Etched, Border3DSide.All);
  202. }
  203. public static void DrawBorder3D(Graphics graphics, Rectangle rectangle, Border3DStyle style) {
  204. DrawBorder3D(graphics, rectangle, style, Border3DSide.All);
  205. }
  206. public static void DrawBorder3D(Graphics graphics, int x, int y, int width, int height) {
  207. DrawBorder3D(graphics, new Rectangle(x, y, width, height), Border3DStyle.Etched, Border3DSide.All);
  208. }
  209. public static void DrawBorder3D(Graphics graphics, int x, int y, int width, int height, Border3DStyle style) {
  210. DrawBorder3D(graphics, new Rectangle(x, y, width, height), style, Border3DSide.All);
  211. }
  212. public static void DrawBorder3D( Graphics graphics, int x, int y, int width, int height, Border3DStyle style,Border3DSide sides) {
  213. DrawBorder3D( graphics, new Rectangle(x, y, width, height), style, sides);
  214. }
  215. public static void DrawBorder3D( Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides) {
  216. ThemeEngine.Current.DrawBorder3D (graphics, rectangle, style, sides);
  217. }
  218. public static void DrawButton( Graphics graphics, int x, int y, int width, int height, ButtonState state) {
  219. DrawButton(graphics, new Rectangle(x, y, width, height), state);
  220. }
  221. public static void DrawButton( Graphics graphics, Rectangle rectangle, ButtonState state) {
  222. ThemeEngine.Current.DrawButton (graphics, rectangle, state);
  223. }
  224. public static void DrawCaptionButton(Graphics graphics, int x, int y, int width, int height, CaptionButton button, ButtonState state) {
  225. DrawCaptionButton(graphics, new Rectangle(x, y, width, height), button, state);
  226. }
  227. public static void DrawCaptionButton(Graphics graphics, Rectangle rectangle, CaptionButton button, ButtonState state) {
  228. ThemeEngine.Current.DrawCaptionButton (graphics, rectangle, button, state);
  229. }
  230. public static void DrawCheckBox(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
  231. DrawCheckBox(graphics, new Rectangle(x, y, width, height), state);
  232. }
  233. public static void DrawCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
  234. ThemeEngine.Current.DrawCheckBox (graphics, rectangle, state);
  235. }
  236. public static void DrawComboButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
  237. ThemeEngine.Current.DrawComboButton (graphics, rectangle, state);
  238. }
  239. public static void DrawComboButton(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
  240. DrawComboButton(graphics, new Rectangle(x, y, width, height), state);
  241. }
  242. public static void DrawContainerGrabHandle(Graphics graphics, Rectangle bounds) {
  243. ThemeEngine.Current.DrawContainerGrabHandle (graphics, bounds);
  244. }
  245. public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle) {
  246. DrawFocusRectangle(graphics, rectangle, Color.White, Color.Black);
  247. }
  248. public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle, Color foreColor, Color backColor) {
  249. ThemeEngine.Current.DrawFocusRectangle (graphics, rectangle, foreColor, backColor);
  250. }
  251. public static void DrawGrabHandle(Graphics graphics, Rectangle rectangle, bool primary, bool enabled) {
  252. ThemeEngine.Current.DrawGrabHandle (graphics, rectangle, primary, enabled);
  253. }
  254. public static void DrawGrid(Graphics graphics, Rectangle area, Size pixelsBetweenDots, Color backColor) {
  255. ThemeEngine.Current.DrawGrid (graphics, area, pixelsBetweenDots, backColor);
  256. }
  257. public static void DrawImageDisabled(Graphics graphics, Image image, int x, int y, Color background) {
  258. ThemeEngine.Current.DrawImageDisabled (graphics, image, x, y, background);
  259. }
  260. public static void DrawLockedFrame(Graphics graphics, Rectangle rectangle, bool primary) {
  261. ThemeEngine.Current.DrawLockedFrame (graphics, rectangle, primary);
  262. }
  263. public static void DrawMenuGlyph(Graphics graphics, Rectangle rectangle, MenuGlyph glyph) {
  264. ThemeEngine.Current.DrawMenuGlyph (graphics, rectangle, glyph);
  265. }
  266. public static void DrawMenuGlyph(Graphics graphics, int x, int y, int width, int height, MenuGlyph glyph) {
  267. DrawMenuGlyph(graphics, new Rectangle(x, y, width, height), glyph);
  268. }
  269. public static void DrawMixedCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
  270. DrawCheckBox(graphics, rectangle, state);
  271. }
  272. public static void DrawMixedCheckBox(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
  273. DrawMixedCheckBox(graphics, new Rectangle(x, y, width, height), state);
  274. }
  275. public static void DrawRadioButton(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
  276. DrawRadioButton(graphics, new Rectangle(x, y, width, height), state);
  277. }
  278. public static void DrawRadioButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
  279. ThemeEngine.Current.DrawRadioButton (graphics, rectangle, state);
  280. }
  281. [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
  282. public static void DrawReversibleFrame(Rectangle rectangle, Color backColor, FrameStyle style) {
  283. throw new NotImplementedException();
  284. }
  285. [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
  286. public static void DrawReversibleLine(Point start, Point end, Color backColor) {
  287. throw new NotImplementedException();
  288. }
  289. [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
  290. public static void FillReversibleRectangle(Rectangle rectangle, Color backColor) {
  291. throw new NotImplementedException();
  292. }
  293. public static void DrawScrollButton (Graphics graphics, int x, int y, int width, int height, ScrollButton button, ButtonState state) {
  294. ThemeEngine.Current.DrawScrollButton (graphics, new Rectangle(x, y, width, height), button, state);
  295. }
  296. public static void DrawScrollButton (Graphics graphics, Rectangle rectangle, ScrollButton button, ButtonState state) {
  297. ThemeEngine.Current.DrawScrollButton (graphics, rectangle, button, state);
  298. }
  299. [MonoTODO]
  300. public static void DrawSelectionFrame(Graphics graphics, bool active, Rectangle outsideRect, Rectangle insideRect, Color backColor) {
  301. throw new NotImplementedException();
  302. }
  303. public static void DrawSizeGrip (Graphics graphics, Color backColor, Rectangle bounds)
  304. {
  305. ThemeEngine.Current.DrawSizeGrip (graphics, backColor, bounds);
  306. }
  307. public static void DrawSizeGrip(Graphics graphics, Color backColor, int x, int y, int width, int height) {
  308. DrawSizeGrip(graphics, backColor, new Rectangle(x, y, width, height));
  309. }
  310. public static void DrawStringDisabled(Graphics graphics, string s, Font font, Color color, RectangleF layoutRectangle, StringFormat format) {
  311. ThemeEngine.Current.DrawStringDisabled (graphics, s, font, color, layoutRectangle, format);
  312. }
  313. #endregion // Public Static Methods
  314. }
  315. }