PageRenderTime 45ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/cb0t/Misc/CustomColorPicker.cs

#
C# | 301 lines | 257 code | 44 blank | 0 comment | 45 complexity | 647c75b859a2bb2e32545aa95c5fd578 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. namespace cb0t
  12. {
  13. public partial class CustomColorPicker : Form
  14. {
  15. public CustomColorPicker()
  16. {
  17. this.InitializeComponent();
  18. this.PopulateColorList();
  19. this.doubleBufferedComboBox1.SelectedIndex = 0;
  20. }
  21. public void UpdateTemplate()
  22. {
  23. this.label1.Text = StringTemplate.Get(STType.ColorPicker, 0) + ":";
  24. this.label2.Text = StringTemplate.Get(STType.ColorPicker, 1) + ":";
  25. this.label7.Text = StringTemplate.Get(STType.ColorPicker, 2) + ":";
  26. }
  27. private Pen pen = new Pen(Brushes.Black, 1);
  28. private SolidBrush text_brush = new SolidBrush(Color.Black);
  29. private List<Color> list = new List<Color>();
  30. private void PopulateColorList()
  31. {
  32. MethodAttributes attributes = MethodAttributes.Static | MethodAttributes.Public;
  33. PropertyInfo[] properties = typeof(Color).GetProperties();
  34. for (int i = 0; i < properties.Length; i++)
  35. {
  36. PropertyInfo info = properties[i];
  37. if (info.PropertyType == typeof(Color))
  38. {
  39. MethodInfo getMethod = info.GetGetMethod();
  40. if ((getMethod != null) && ((getMethod.Attributes & attributes) == attributes))
  41. this.list.Add((Color)info.GetValue(null, null));
  42. }
  43. }
  44. this.list.Sort(new StandardColorComparer());
  45. foreach (Color c in this.list)
  46. this.doubleBufferedComboBox1.Items.Add(c);
  47. this.doubleBufferedComboBox2.Items.Add(Color.White);
  48. this.doubleBufferedComboBox2.Items.Add(Color.Black);
  49. this.doubleBufferedComboBox2.Items.Add(Color.Navy);
  50. this.doubleBufferedComboBox2.Items.Add(Color.Green);
  51. this.doubleBufferedComboBox2.Items.Add(Color.Red);
  52. this.doubleBufferedComboBox2.Items.Add(Color.Maroon);
  53. this.doubleBufferedComboBox2.Items.Add(Color.Purple);
  54. this.doubleBufferedComboBox2.Items.Add(Color.Orange);
  55. this.doubleBufferedComboBox2.Items.Add(Color.Yellow);
  56. this.doubleBufferedComboBox2.Items.Add(Color.Lime);
  57. this.doubleBufferedComboBox2.Items.Add(Color.Teal);
  58. this.doubleBufferedComboBox2.Items.Add(Color.Aqua);
  59. this.doubleBufferedComboBox2.Items.Add(Color.Blue);
  60. this.doubleBufferedComboBox2.Items.Add(Color.Fuchsia);
  61. this.doubleBufferedComboBox2.Items.Add(Color.Gray);
  62. this.doubleBufferedComboBox2.Items.Add(Color.Silver);
  63. this.doubleBufferedComboBox1.SelectedIndex = -1;
  64. }
  65. private void doubleBufferedComboBox1_DrawItem(object sender, DrawItemEventArgs e)
  66. {
  67. if (e.Index > -1)
  68. {
  69. e.Graphics.FillRectangle(Brushes.White, e.Bounds);
  70. Color color = (Color)this.doubleBufferedComboBox1.Items[e.Index];
  71. using (SolidBrush sb = new SolidBrush(color))
  72. e.Graphics.FillRectangle(sb, new Rectangle(e.Bounds.X + 1, e.Bounds.Y + 1, 18, 12));
  73. e.Graphics.DrawRectangle(this.pen, new Rectangle(e.Bounds.X + 1, e.Bounds.Y + 1, 17, 11));
  74. if (e.Index == 0)
  75. {
  76. e.Graphics.DrawString("Custom", this.Font, this.text_brush, new PointF(e.Bounds.X + 24, e.Bounds.Y));
  77. e.Graphics.DrawString("??", this.Font, this.text_brush, new PointF(e.Bounds.X + 2, e.Bounds.Y));
  78. }
  79. else e.Graphics.DrawString(color.Name, this.Font, this.text_brush, new PointF(e.Bounds.X + 24, e.Bounds.Y));
  80. }
  81. }
  82. private void doubleBufferedComboBox2_DrawItem(object sender, DrawItemEventArgs e)
  83. {
  84. if (e.Index > -1)
  85. {
  86. using (SolidBrush sb = new SolidBrush((Color)this.doubleBufferedComboBox2.Items[e.Index]))
  87. e.Graphics.FillRectangle(sb, e.Bounds);
  88. }
  89. }
  90. private bool user_changing = false;
  91. private void doubleBufferedComboBox1_SelectedIndexChanged(object sender, EventArgs e)
  92. {
  93. if (!this.user_changing)
  94. {
  95. if (this.doubleBufferedComboBox1.SelectedIndex == 0)
  96. {
  97. this.panel1.BackColor = Color.White;
  98. this.trackBar1.Value = 255;
  99. this.label3.Text = "R: 255";
  100. this.trackBar2.Value = 255;
  101. this.label4.Text = "G: 255";
  102. this.trackBar3.Value = 255;
  103. this.label5.Text = "B: 255";
  104. this.textBox1.Text = String.Format("#{0:x2}{1:x2}{2:x2}", (byte)this.trackBar1.Value, (byte)this.trackBar2.Value, (byte)this.trackBar3.Value);
  105. }
  106. else
  107. {
  108. this.panel1.BackColor = this.list[this.doubleBufferedComboBox1.SelectedIndex];
  109. this.trackBar1.Value = this.panel1.BackColor.R;
  110. this.label3.Text = "R: " + this.trackBar1.Value;
  111. this.trackBar2.Value = this.panel1.BackColor.G;
  112. this.label4.Text = "G: " + this.trackBar2.Value;
  113. this.trackBar3.Value = this.panel1.BackColor.B;
  114. this.label5.Text = "B: " + this.trackBar3.Value;
  115. this.textBox1.Text = String.Format("#{0:x2}{1:x2}{2:x2}", (byte)this.trackBar1.Value, (byte)this.trackBar2.Value, (byte)this.trackBar3.Value);
  116. }
  117. }
  118. else this.user_changing = false;
  119. }
  120. private void trackBar1_Scroll(object sender, EventArgs e)
  121. {
  122. Color color = Color.FromArgb(this.trackBar1.Value, this.trackBar2.Value, this.trackBar3.Value);
  123. for (int i = 1; i < this.list.Count; i++)
  124. if (this.list[i].RGBEquals(color))
  125. if (this.doubleBufferedComboBox1.SelectedIndex != i)
  126. {
  127. this.user_changing = true;
  128. this.doubleBufferedComboBox1.SelectedIndex = i;
  129. this.panel1.BackColor = color;
  130. this.label3.Text = "R: " + this.trackBar1.Value;
  131. this.label4.Text = "G: " + this.trackBar2.Value;
  132. this.label5.Text = "B: " + this.trackBar3.Value;
  133. this.textBox1.Text = String.Format("#{0:x2}{1:x2}{2:x2}", (byte)this.trackBar1.Value, (byte)this.trackBar2.Value, (byte)this.trackBar3.Value);
  134. return;
  135. }
  136. if (this.doubleBufferedComboBox1.SelectedIndex != 0)
  137. {
  138. this.user_changing = true;
  139. this.doubleBufferedComboBox1.SelectedIndex = 0;
  140. }
  141. this.panel1.BackColor = color;
  142. this.label3.Text = "R: " + this.trackBar1.Value;
  143. this.label4.Text = "G: " + this.trackBar2.Value;
  144. this.label5.Text = "B: " + this.trackBar3.Value;
  145. this.textBox1.Text = String.Format("#{0:x2}{1:x2}{2:x2}", (byte)this.trackBar1.Value, (byte)this.trackBar2.Value, (byte)this.trackBar3.Value);
  146. }
  147. public Color SelectedColor
  148. {
  149. get { return this.panel1.BackColor; }
  150. set
  151. {
  152. this.panel1.BackColor = value;
  153. this.trackBar1.Value = this.panel1.BackColor.R;
  154. this.trackBar2.Value = this.panel1.BackColor.G;
  155. this.trackBar3.Value = this.panel1.BackColor.B;
  156. this.trackBar1_Scroll(null, null);
  157. }
  158. }
  159. public String SelectedHex
  160. {
  161. get
  162. {
  163. return String.Format("{0:x2}{1:x2}{2:x2}", (byte)this.trackBar1.Value, (byte)this.trackBar2.Value, (byte)this.trackBar3.Value);
  164. }
  165. set
  166. {
  167. if (value.Length == 6)
  168. {
  169. String r_str = value.Substring(0, 2);
  170. String g_str = value.Substring(2, 2);
  171. String b_str = value.Substring(4, 2);
  172. byte r, g, b;
  173. if (byte.TryParse(r_str, NumberStyles.HexNumber, null, out r))
  174. if (byte.TryParse(g_str, NumberStyles.HexNumber, null, out g))
  175. if (byte.TryParse(b_str, NumberStyles.HexNumber, null, out b))
  176. {
  177. this.trackBar1.Value = r;
  178. this.trackBar2.Value = g;
  179. this.trackBar3.Value = b;
  180. this.trackBar1_Scroll(null, null);
  181. }
  182. }
  183. }
  184. }
  185. private void button2_Click(object sender, EventArgs e)
  186. {
  187. String str = this.textBox1.Text.Replace("#", String.Empty).Replace(" ", String.Empty);
  188. if (str.Length == 6)
  189. {
  190. String r_str = str.Substring(0, 2);
  191. String g_str = str.Substring(2, 2);
  192. String b_str = str.Substring(4, 2);
  193. byte r, g, b;
  194. if (byte.TryParse(r_str, NumberStyles.HexNumber, null, out r))
  195. if (byte.TryParse(g_str, NumberStyles.HexNumber, null, out g))
  196. if (byte.TryParse(b_str, NumberStyles.HexNumber, null, out b))
  197. {
  198. this.trackBar1.Value = r;
  199. this.trackBar2.Value = g;
  200. this.trackBar3.Value = b;
  201. this.trackBar1_Scroll(null, null);
  202. return;
  203. }
  204. MessageBox.Show("invalid hex color",
  205. "cb0t",
  206. MessageBoxButtons.OK,
  207. MessageBoxIcon.Error);
  208. }
  209. }
  210. private void doubleBufferedComboBox2_SelectedIndexChanged(object sender, EventArgs e)
  211. {
  212. if (this.doubleBufferedComboBox2.SelectedIndex > -1)
  213. {
  214. this.panel1.BackColor = (Color)this.doubleBufferedComboBox2.SelectedItem;
  215. this.trackBar1.Value = this.panel1.BackColor.R;
  216. this.label3.Text = "R: " + this.trackBar1.Value;
  217. this.trackBar2.Value = this.panel1.BackColor.G;
  218. this.label4.Text = "G: " + this.trackBar2.Value;
  219. this.trackBar3.Value = this.panel1.BackColor.B;
  220. this.label5.Text = "B: " + this.trackBar3.Value;
  221. this.textBox1.Text = String.Format("#{0:x2}{1:x2}{2:x2}", (byte)this.trackBar1.Value, (byte)this.trackBar2.Value, (byte)this.trackBar3.Value);
  222. for (int i = 1; i < this.list.Count; i++)
  223. if (this.list[i].RGBEquals(this.panel1.BackColor))
  224. if (this.doubleBufferedComboBox1.SelectedIndex != i)
  225. {
  226. this.doubleBufferedComboBox1.SelectedIndex = i;
  227. break;
  228. }
  229. this.doubleBufferedComboBox2.SelectedIndex = -1;
  230. this.DialogResult = DialogResult.OK;
  231. }
  232. }
  233. }
  234. class StandardColorComparer : IComparer<Color>
  235. {
  236. public int Compare(Color color, Color color2)
  237. {
  238. if (color.A < color2.A)
  239. return -1;
  240. if (color.A > color2.A)
  241. return 1;
  242. if (color.GetHue() < color2.GetHue())
  243. return -1;
  244. if (color.GetHue() > color2.GetHue())
  245. return 1;
  246. if (color.GetSaturation() < color2.GetSaturation())
  247. return -1;
  248. if (color.GetSaturation() > color2.GetSaturation())
  249. return 1;
  250. if (color.GetBrightness() < color2.GetBrightness())
  251. return -1;
  252. if (color.GetBrightness() > color2.GetBrightness())
  253. return 1;
  254. return 0;
  255. }
  256. }
  257. }