Skip to content

Commit

Permalink
copy colour and update startup location
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryQuan committed Aug 22, 2019
1 parent 5ec448e commit 968a654
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
21 changes: 19 additions & 2 deletions colourpicker/colourpicker/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 25 additions & 4 deletions colourpicker/colourpicker/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public MainForm()
{
InitializeComponent();
lastLocaion = Cursor.Position;
resetTip();

// Set a timer to update colour every 10ms
Timer t = new Timer();
Expand All @@ -34,10 +35,16 @@ private void T_Tick(object sender, EventArgs e)
Console.WriteLine(curr);
// Same location don't update anything
if (lastLocaion.X == curr.X && lastLocaion.Y == curr.Y) return;
lastLocaion = curr;

// Update panel colour
updateColour(GetColorAt(curr));
resetTip();
}

private void resetTip()
{
tipLabel.Text = "Ctrl + C to copy current colour\nIt only works if this window is being focused";
}

/// <summary>
Expand All @@ -51,15 +58,20 @@ private void updateColour(Color c)
colourPanel.BackColor = c;

// Get RGB and HEX string
colorLabel.Text = RGBConverter(c) + "\n" + HexConverter(c);
colorLabel.Text = getFormattedColour(c);
}

private string getFormattedColour(Color c)
{
return RGBConverter(c) + "\n" + HexConverter(c);
}

/// <summary>
/// Convert COlor to HEX format, from https://stackoverflow.com/questions/2395438/convert-system-drawing-color-to-rgb-and-hex-value
/// </summary>
/// <param name="c">Colour</param>
/// <returns>A string like #123456</returns>
private static string HexConverter(Color c)
private string HexConverter(Color c)
{
return string.Format("#{0}{1}{2}", c.R.ToString("X2"), c.G.ToString("X2"), c.B.ToString("X2"));
}
Expand All @@ -69,9 +81,9 @@ private static string HexConverter(Color c)
/// </summary>
/// <param name="c">Color</param>
/// <returns>A string like RGB(0, 0, 0)</returns>
private static string RGBConverter(Color c)
private string RGBConverter(Color c)
{
return string.Format("RGB({0}, {1}, {2})\n", c.R, c.G, c.B);
return string.Format("RGB({0}, {1}, {2})", c.R, c.G, c.B);
}

[DllImport("user32.dll", SetLastError = true)]
Expand All @@ -96,5 +108,14 @@ public static Color GetColorAt(Point p)
ReleaseDC(desk, dc);
return Color.FromArgb(255, (a >> 0) & 0xff, (a >> 8) & 0xff, (a >> 16) & 0xff);
}

private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
{
Clipboard.SetText(getFormattedColour(currColour));
tipLabel.Text = "Copied :)";
}
}
}
}

0 comments on commit 968a654

Please sign in to comment.