Skip to content

Commit

Permalink
v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
NattyXO committed Dec 30, 2023
1 parent 13f6cae commit d78ceab
Show file tree
Hide file tree
Showing 39 changed files with 493 additions and 1,118 deletions.
431 changes: 149 additions & 282 deletions color picker/Color Picker.Designer.cs

Large diffs are not rendered by default.

193 changes: 49 additions & 144 deletions color picker/Color Picker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand All @@ -13,6 +14,14 @@ namespace color_picker
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool GetCursorPos(ref Point lpPoint);

[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);

Bitmap screenPixel = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);


Dictionary<string, string> colorDictionary = new Dictionary<string, string>()
{
Expand Down Expand Up @@ -95,7 +104,8 @@ private void pictureBox1_Click(object sender, EventArgs e)

private void Form1_Load(object sender, EventArgs e)
{

txtColor1_TextChanged(null, EventArgs.Empty);
txtColor2_TextChanged(null, EventArgs.Empty);
}


Expand Down Expand Up @@ -199,149 +209,6 @@ private void txtColor2_TextChanged(object sender, EventArgs e)
}
}

private void txtColor3_TextChanged(object sender, EventArgs e)
{
// Get the entered hexadecimal color code
string hexColor = txtColor3.Text.Trim();

// Check if the entered value is a valid hexadecimal color code
if (System.Text.RegularExpressions.Regex.IsMatch(hexColor, "^#(?:[0-9a-fA-F]{3}){1,2}$"))
{
// Convert the hexadecimal color to a Color object
Color newColor = ColorTranslator.FromHtml(hexColor);

// Set the BackColor of pic1 to the entered color
pnl3.BackColor = newColor;

// Find the closest named color to the entered color
string colorName = "";
foreach (KnownColor knownColor in Enum.GetValues(typeof(KnownColor)))
{
Color namedColor = Color.FromKnownColor(knownColor);
if (namedColor.ToArgb() == newColor.ToArgb())
{
colorName = knownColor.ToString();
break;
}
}

// Display the closest named color in lblColor1
lblColor3.Text = colorName != "" ? colorName : "Custom Color";
}
else
{
// If the entered value is not a valid hexadecimal color code
lblColor3.Text = "Invalid Color";
}
}

private void txtColor4_TextChanged(object sender, EventArgs e)
{
// Get the entered hexadecimal color code
string hexColor = txtColor4.Text.Trim();

// Check if the entered value is a valid hexadecimal color code
if (System.Text.RegularExpressions.Regex.IsMatch(hexColor, "^#(?:[0-9a-fA-F]{3}){1,2}$"))
{
// Convert the hexadecimal color to a Color object
Color newColor = ColorTranslator.FromHtml(hexColor);

// Set the BackColor of pic1 to the entered color
pnl4.BackColor = newColor;

// Find the closest named color to the entered color
string colorName = "";
foreach (KnownColor knownColor in Enum.GetValues(typeof(KnownColor)))
{
Color namedColor = Color.FromKnownColor(knownColor);
if (namedColor.ToArgb() == newColor.ToArgb())
{
colorName = knownColor.ToString();
break;
}
}

// Display the closest named color in lblColor1
lblColor4.Text = colorName != "" ? colorName : "Custom Color";
}
else
{
// If the entered value is not a valid hexadecimal color code
lblColor4.Text = "Invalid Color";
}
}

private void txtColor5_TextChanged(object sender, EventArgs e)
{
// Get the entered hexadecimal color code
string hexColor = txtColor5.Text.Trim();

// Check if the entered value is a valid hexadecimal color code
if (System.Text.RegularExpressions.Regex.IsMatch(hexColor, "^#(?:[0-9a-fA-F]{3}){1,2}$"))
{
// Convert the hexadecimal color to a Color object
Color newColor = ColorTranslator.FromHtml(hexColor);

// Set the BackColor of pic1 to the entered color
pnl5.BackColor = newColor;

// Find the closest named color to the entered color
string colorName = "";
foreach (KnownColor knownColor in Enum.GetValues(typeof(KnownColor)))
{
Color namedColor = Color.FromKnownColor(knownColor);
if (namedColor.ToArgb() == newColor.ToArgb())
{
colorName = knownColor.ToString();
break;
}
}

// Display the closest named color in lblColor1
lblColor5.Text = colorName != "" ? colorName : "Custom Color";
}
else
{
// If the entered value is not a valid hexadecimal color code
lblColor5.Text = "Invalid Color";
}
}

private void txtColor6_TextChanged(object sender, EventArgs e)
{
// Get the entered hexadecimal color code
string hexColor = txtColor6.Text.Trim();

// Check if the entered value is a valid hexadecimal color code
if (System.Text.RegularExpressions.Regex.IsMatch(hexColor, "^#(?:[0-9a-fA-F]{3}){1,2}$"))
{
// Convert the hexadecimal color to a Color object
Color newColor = ColorTranslator.FromHtml(hexColor);

// Set the BackColor of pic1 to the entered color
pnl6.BackColor = newColor;

// Find the closest named color to the entered color
string colorName = "";
foreach (KnownColor knownColor in Enum.GetValues(typeof(KnownColor)))
{
Color namedColor = Color.FromKnownColor(knownColor);
if (namedColor.ToArgb() == newColor.ToArgb())
{
colorName = knownColor.ToString();
break;
}
}

// Display the closest named color in lblColor1
lblColor6.Text = colorName != "" ? colorName : "Custom Color";
}
else
{
// If the entered value is not a valid hexadecimal color code
lblColor6.Text = "Invalid Color";
}
}

private void picSearchButton_Click(object sender, EventArgs e)
{
Expand Down Expand Up @@ -413,5 +280,43 @@ private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
{
this.Close();
}
private Color GetColorAt(Point location)
{
using (Graphics gdest = Graphics.FromImage(screenPixel))
{
using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
{
IntPtr hSrcDC = gsrc.GetHdc();
IntPtr hDC = gdest.GetHdc();
int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);
gdest.ReleaseHdc();
gsrc.ReleaseHdc();
}
}

return screenPixel.GetPixel(0, 0);
}


private void timer1_Tick(object sender, EventArgs e)
{
Point cursor = new Point();
GetCursorPos(ref cursor);

var c = GetColorAt(cursor);
pnlColorRealTime.BackColor = c;
lblHexaRealTime.Text = String.Format("#{0:X6}", c.ToArgb() & 0x00FFFFFF);

}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// Check if Ctrl + C is pressed
if (e.Control && e.KeyCode == Keys.C)
{
Clipboard.SetText(lblHexaRealTime.Text); // Copy text to clipboard
e.SuppressKeyPress = true; // Suppress default copy behavior
}
}
}
}
10 changes: 8 additions & 2 deletions color picker/Color Picker.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@
<metadata name="toolTip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>536, 17</value>
</metadata>
<metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>651, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="picRefresh.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Expand Down Expand Up @@ -348,8 +354,8 @@
oKmLebJvsqTs4e8NloIZRlFR0fmsVPoH4/bcQdqRKH4AAAAASUVORK5CYII=
</value>
</data>
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>536, 17</value>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>51</value>
</metadata>
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Expand Down
20 changes: 20 additions & 0 deletions color picker/Properties/Resources.Designer.cs

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

6 changes: 6 additions & 0 deletions color picker/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,10 @@
<data name="icons8-github-250 (1)" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\icons8-github-250 (1).png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="UI2" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\UI2.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="UI1" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\UI1.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
Binary file added color picker/Resources/UI1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added color picker/Resources/UI2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d78ceab

Please sign in to comment.