MainForm.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. using GitHubUpdate;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace quick_color_picker
  11. {
  12. public partial class MainForm : Form
  13. {
  14. private Pen aimPen = new Pen(Color.Red, 1);
  15. private Color aimColor;
  16. private bool darkMode = false;
  17. private bool alwaysOnTop = true;
  18. public MainForm()
  19. {
  20. InitializeComponent();
  21. }
  22. private void CaptureScreen()
  23. {
  24. try
  25. {
  26. Bitmap bmp = new Bitmap(120, 120);
  27. using (Graphics g = Graphics.FromImage(bmp))
  28. {
  29. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  30. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
  31. g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
  32. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
  33. g.CopyFromScreen(
  34. Cursor.Position.X - 20,
  35. Cursor.Position.Y - 20, 0, 0,
  36. new Size(40, 40)
  37. );
  38. Bitmap scaledBmp = new Bitmap(bmp, 120, 120);
  39. g.Clear(Color.Black);
  40. g.DrawImage(scaledBmp, 0, 0, 360, 360);
  41. scaledBmp.Dispose();
  42. if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
  43. pictureBox1.Image = bmp;
  44. aimColor = bmp.GetPixel(pictureBox1.Width / 2, pictureBox1.Height / 2);
  45. colorPanel.BackColor = aimColor;
  46. g.DrawLine(aimPen, 0, pictureBox1.Height / 2, pictureBox1.Width, pictureBox1.Height / 2);
  47. g.DrawLine(aimPen, pictureBox1.Width / 2, 0, pictureBox1.Height / 2, pictureBox1.Height);
  48. }
  49. xLabel.Invoke((MethodInvoker)(() => xLabel.Text = "X: " + Cursor.Position.X));
  50. yLabel.Invoke((MethodInvoker)(() => yLabel.Text = "Y: " + Cursor.Position.Y));
  51. }
  52. catch
  53. {
  54. }
  55. }
  56. [System.Runtime.InteropServices.DllImport("user32.dll")]
  57. public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
  58. [System.Runtime.InteropServices.DllImport("user32.dll")]
  59. public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
  60. private void MainForm_Load(object sender, EventArgs e)
  61. {
  62. RegisterHotKey(this.Handle, 0, (int) KeyModifier.Alt, Keys.X.GetHashCode());
  63. toolStrip1.Renderer = new ToolStripOverride();
  64. darkMode = ThemeManager.isDarkTheme();
  65. if (darkMode)
  66. {
  67. applyDarkTheme();
  68. }
  69. Task task = new Task(() =>
  70. {
  71. while (true)
  72. {
  73. CaptureScreen();
  74. Thread.Sleep(100);
  75. }
  76. });
  77. task.Start();
  78. LoadColorList();
  79. checkForUpdates(false);
  80. }
  81. enum KeyModifier
  82. {
  83. None = 0,
  84. Alt = 1,
  85. Control = 2,
  86. Shift = 4,
  87. WinKey = 8
  88. }
  89. protected override void WndProc(ref Message m)
  90. {
  91. base.WndProc(ref m);
  92. if (m.Msg == 0x0312)
  93. {
  94. Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
  95. KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF);
  96. int id = m.WParam.ToInt32();
  97. GetColor();
  98. }
  99. }
  100. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  101. {
  102. UnregisterHotKey(this.Handle, 0);
  103. }
  104. private void GetColor()
  105. {
  106. colorList.Items.Add(aimColor.R + "," + aimColor.G + "," + aimColor.B);
  107. colorList.SetSelected(colorList.Items.Count - 1, true);
  108. SaveColorList();
  109. }
  110. private void colorList_DrawItem(object sender, DrawItemEventArgs e)
  111. {
  112. if (e.Index != -1)
  113. {
  114. e.DrawBackground();
  115. int itemIndex = e.Index;
  116. string itemText = colorList.Items[itemIndex].ToString();
  117. if (itemIndex >= 0 && itemIndex < colorList.Items.Count)
  118. {
  119. Graphics g = e.Graphics;
  120. Color c = textToColor(itemText);
  121. // Background color
  122. SolidBrush backgroundColorBrush = new SolidBrush(c);
  123. g.FillRectangle(backgroundColorBrush, e.Bounds);
  124. // Item text
  125. SolidBrush itemTextColorBrush;
  126. if (c.GetBrightness() > 0.5) {
  127. itemTextColorBrush = new SolidBrush(Color.Black);
  128. }
  129. else
  130. {
  131. itemTextColorBrush = new SolidBrush(Color.White);
  132. }
  133. if (e.Index == colorList.SelectedIndex)
  134. {
  135. Font f = new Font(e.Font, FontStyle.Bold);
  136. Point p = new Point(
  137. colorList.GetItemRectangle(itemIndex).Location.X + 10,
  138. colorList.GetItemRectangle(itemIndex).Location.Y
  139. );
  140. g.DrawString(itemText, f, itemTextColorBrush, p);
  141. }
  142. else
  143. {
  144. g.DrawString(itemText, e.Font, itemTextColorBrush, colorList.GetItemRectangle(itemIndex).Location);
  145. }
  146. // Clean up
  147. backgroundColorBrush.Dispose();
  148. itemTextColorBrush.Dispose();
  149. }
  150. }
  151. }
  152. private void colorList_SelectedIndexChanged(object sender, EventArgs e)
  153. {
  154. if (colorList.SelectedIndex != -1)
  155. {
  156. string t = colorList.Items[colorList.SelectedIndex].ToString();
  157. Color c = textToColor(t);
  158. rgbTextBox.Text = t;
  159. htmlTextBox.Text = ColorTranslator.ToHtml(c).ToString();
  160. int[] cmyk = RgbToCmyk(c.R, c.G, c.B);
  161. cmykTextBox.Text = cmyk[0] + "," + cmyk[1] + "," + cmyk[2] + "," + cmyk[3];
  162. int[] hsl = ColorToHsl(c);
  163. hslTextBox.Text = hsl[0] + "," + hsl[1] + "," + hsl[2];
  164. double rOne = c.R / (double)255;
  165. double gOne = c.G / (double)255;
  166. double bOne = c.B / (double)255;
  167. rgbOneTextBox.Text = rOne.ToString("0.##") + "/" + gOne.ToString("0.##") + "/" + bOne.ToString("0.##");
  168. gridPanel.BackColor = c;
  169. int r1 = c.R - 50;
  170. int g1 = c.G - 50;
  171. int b1 = c.B - 50;
  172. if (r1 < 0 || g1 < 0 || b1 < 0)
  173. {
  174. r1 = 0;
  175. g1 = 0;
  176. b1 = 0;
  177. }
  178. gradPanel1.BackColor = Color.FromArgb(r1, g1, b1);
  179. int r2 = c.R - 25;
  180. int g2 = c.G - 25;
  181. int b2 = c.B - 25;
  182. if (r2 < 0 || g2 < 0 || b2 < 0)
  183. {
  184. r2 = 0;
  185. g2 = 0;
  186. b2 = 0;
  187. }
  188. gradPanel2.BackColor = Color.FromArgb(r2, g2, b2);
  189. int r3 = c.R + 25;
  190. int g3 = c.G + 25;
  191. int b3 = c.B + 25;
  192. if (r3 > 255 || g3 > 255 || b3 > 255)
  193. {
  194. r3 = 255;
  195. g3 = 255;
  196. b3 = 255;
  197. }
  198. gradPanel3.BackColor = Color.FromArgb(r3, g3, b3);
  199. int r4 = c.R + 50;
  200. int g4 = c.G + 50;
  201. int b4 = c.B + 50;
  202. if (r4 > 255 || g4 > 255 || b4 > 255)
  203. {
  204. r4 = 255;
  205. g4 = 255;
  206. b4 = 255;
  207. }
  208. gradPanel4.BackColor = Color.FromArgb(r4, g4, b4);
  209. }
  210. colorList.Refresh();
  211. }
  212. private Color textToColor(string text)
  213. {
  214. string[] strArr = text.Split(',');
  215. int[] arr = Array.ConvertAll(strArr, s => int.Parse(s));
  216. return Color.FromArgb(arr[0], arr[1], arr[2]);
  217. }
  218. private int[] RgbToCmyk(int intR, int intG, int intB)
  219. {
  220. double c, m, y, k;
  221. double r = intR, g = intG, b = intB;
  222. double r1 = r / 255, g1 = g / 255, b1 = b / 255;
  223. k = 1 - Math.Max(Math.Max(r1, g1), b1);
  224. if (k == 1)
  225. {
  226. return new int[] { 0, 0, 0, 1 };
  227. }
  228. else
  229. {
  230. c = (1 - r1 - k) / (1 - k);
  231. m = (1 - g1 - k) / (1 - k);
  232. y = (1 - b1 - k) / (1 - k);
  233. int intC = Convert.ToInt32(c * 100);
  234. int intM = Convert.ToInt32(m * 100);
  235. int intY = Convert.ToInt32(y * 100);
  236. int intK = Convert.ToInt32(k * 100);
  237. return new int[] { intC, intM, intY, intK };
  238. }
  239. }
  240. private int[] ColorToHsl(Color color)
  241. {
  242. float hue = color.GetHue();
  243. float saturation = color.GetSaturation();
  244. float lightness = color.GetBrightness();
  245. int intHue = Convert.ToInt32(hue);
  246. int intSaturation = Convert.ToInt32(saturation * 100);
  247. int intLightness = Convert.ToInt32(lightness * 100);
  248. return new int[] { intHue, intSaturation, intLightness };
  249. }
  250. private void rgbCopyButton_Click(object sender, EventArgs e)
  251. {
  252. Clipboard.SetText(rgbTextBox.Text);
  253. }
  254. private void htmlCopyButton_Click(object sender, EventArgs e)
  255. {
  256. Clipboard.SetText(htmlTextBox.Text);
  257. }
  258. private void cmykCopyButton_Click(object sender, EventArgs e)
  259. {
  260. Clipboard.SetText(cmykTextBox.Text);
  261. }
  262. private void applyDarkTheme()
  263. {
  264. this.ForeColor = Color.White;
  265. toolStrip1.BackColor = ThemeManager.BackColorDark;
  266. this.BackColor = ThemeManager.BackColorDark;
  267. colorList.BackColor = ThemeManager.SecondColorDark;
  268. }
  269. private void hslCopyButton_Click(object sender, EventArgs e)
  270. {
  271. Clipboard.SetText(hslTextBox.Text);
  272. }
  273. private void rgbOneCopyButton_Click(object sender, EventArgs e)
  274. {
  275. Clipboard.SetText(rgbOneTextBox.Text);
  276. }
  277. private void onTopButton_Click(object sender, EventArgs e)
  278. {
  279. setAlwaysOnTop(!alwaysOnTop);
  280. }
  281. private void setAlwaysOnTop(bool b)
  282. {
  283. alwaysOnTop = b;
  284. this.TopMost = b;
  285. onTopButton.Checked = b;
  286. }
  287. private void MainForm_KeyDown(object sender, KeyEventArgs e)
  288. {
  289. if (e.Control)
  290. {
  291. if (e.KeyCode == Keys.T)
  292. {
  293. onTopButton.PerformClick();
  294. }
  295. else if (e.KeyCode == Keys.L)
  296. {
  297. clearListButton.PerformClick();
  298. }
  299. }
  300. else
  301. {
  302. if (e.KeyCode == Keys.Delete)
  303. {
  304. deleteButton.PerformClick();
  305. }
  306. else if (e.KeyCode == Keys.F1)
  307. {
  308. aboutButton.PerformClick();
  309. }
  310. }
  311. }
  312. private void clearListButton_Click(object sender, EventArgs e)
  313. {
  314. colorList.Items.Clear();
  315. SaveColorList();
  316. }
  317. private void deleteButton_Click(object sender, EventArgs e)
  318. {
  319. if (colorList.SelectedIndex != -1)
  320. {
  321. colorList.Items.RemoveAt(colorList.SelectedIndex);
  322. SaveColorList();
  323. }
  324. }
  325. private void aboutButton_Click(object sender, EventArgs e)
  326. {
  327. setAlwaysOnTop(false);
  328. AboutForm aboutBox = new AboutForm();
  329. aboutBox.Owner = this;
  330. aboutBox.ShowDialog();
  331. }
  332. public async void checkForUpdates(bool showUpToDateDialog)
  333. {
  334. try
  335. {
  336. var checker = new UpdateChecker("ModuleArt", "quick-color-picker");
  337. UpdateType update = await checker.CheckUpdate();
  338. if (update == UpdateType.None)
  339. {
  340. if (showUpToDateDialog)
  341. {
  342. MessageBox.Show("Application is up to date", "Updator", MessageBoxButtons.OK, MessageBoxIcon.Information);
  343. }
  344. }
  345. else
  346. {
  347. var result = new UpdateNotifyDialog(checker).ShowDialog();
  348. if (result == DialogResult.Yes)
  349. {
  350. checker.DownloadAsset("QuickColorPicker-Setup.msi");
  351. }
  352. }
  353. }
  354. catch
  355. {
  356. if (showUpToDateDialog)
  357. {
  358. MessageBox.Show("Connection error", "Updator", MessageBoxButtons.OK, MessageBoxIcon.Information);
  359. }
  360. }
  361. }
  362. private void SaveColorList()
  363. {
  364. string[] linesToWrite = new string[colorList.Items.Count];
  365. for (int i = 0; i < colorList.Items.Count; i++)
  366. {
  367. linesToWrite[i] = colorList.Items[i].ToString();
  368. }
  369. System.IO.File.WriteAllLines("color-list.txt", linesToWrite);
  370. }
  371. private void LoadColorList()
  372. {
  373. try
  374. {
  375. string[] lines = File.ReadAllLines("color-list.txt");
  376. for (int i = 0; i < lines.Length; i++)
  377. {
  378. colorList.Items.Add(lines[i]);
  379. }
  380. colorList.SetSelected(colorList.Items.Count - 1, true);
  381. }
  382. catch
  383. {
  384. }
  385. }
  386. }
  387. }