Add HSL and make the text clickable

This commit is contained in:
Lncvrt committed 2024-08-03 15:38:38 -07:00
1 parent db32425f94
commit be43b0d41e
2 files changed
+138 -11

No files matched your search

+29 -7
View File
@@ -37,6 +37,7 @@
this.colorpreview = new System.Windows.Forms.Panel(); this.colorpreview = new System.Windows.Forms.Panel();
this.bindlabel = new System.Windows.Forms.Label(); this.bindlabel = new System.Windows.Forms.Label();
this.statuslabel = new System.Windows.Forms.Label(); this.statuslabel = new System.Windows.Forms.Label();
this.hsllabel = new System.Windows.Forms.Label();
this.SuspendLayout(); this.SuspendLayout();
// //
// lookingatlabel // lookingatlabel
@@ -52,17 +53,20 @@
// //
// rgblabel // rgblabel
// //
this.rgblabel.Cursor = System.Windows.Forms.Cursors.Hand;
this.rgblabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold); this.rgblabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
this.rgblabel.ForeColor = System.Drawing.Color.White; this.rgblabel.ForeColor = System.Drawing.Color.White;
this.rgblabel.Location = new System.Drawing.Point(100, 65); this.rgblabel.Location = new System.Drawing.Point(100, 90);
this.rgblabel.Name = "rgblabel"; this.rgblabel.Name = "rgblabel";
this.rgblabel.Size = new System.Drawing.Size(150, 15); this.rgblabel.Size = new System.Drawing.Size(150, 15);
this.rgblabel.TabIndex = 2; this.rgblabel.TabIndex = 2;
this.rgblabel.Text = "R: n/a, G: n/a, B: n/a"; this.rgblabel.Text = "R: n/a, G: n/a, B: n/a";
this.rgblabel.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.rgblabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.rgblabel.Click += new System.EventHandler(this.rgblabel_Click);
// //
// hexlabel // hexlabel
// //
this.hexlabel.Cursor = System.Windows.Forms.Cursors.Hand;
this.hexlabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold); this.hexlabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
this.hexlabel.ForeColor = System.Drawing.Color.White; this.hexlabel.ForeColor = System.Drawing.Color.White;
this.hexlabel.Location = new System.Drawing.Point(125, 40); this.hexlabel.Location = new System.Drawing.Point(125, 40);
@@ -71,24 +75,27 @@
this.hexlabel.TabIndex = 3; this.hexlabel.TabIndex = 3;
this.hexlabel.Text = "Hex: n/a"; this.hexlabel.Text = "Hex: n/a";
this.hexlabel.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.hexlabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.hexlabel.Click += new System.EventHandler(this.hexlabel_Click);
// //
// cmkylabel // cmkylabel
// //
this.cmkylabel.Cursor = System.Windows.Forms.Cursors.Hand;
this.cmkylabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold); this.cmkylabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
this.cmkylabel.ForeColor = System.Drawing.Color.White; this.cmkylabel.ForeColor = System.Drawing.Color.White;
this.cmkylabel.Location = new System.Drawing.Point(35, 90); this.cmkylabel.Location = new System.Drawing.Point(35, 115);
this.cmkylabel.Name = "cmkylabel"; this.cmkylabel.Name = "cmkylabel";
this.cmkylabel.Size = new System.Drawing.Size(280, 15); this.cmkylabel.Size = new System.Drawing.Size(280, 15);
this.cmkylabel.TabIndex = 4; this.cmkylabel.TabIndex = 4;
this.cmkylabel.Text = "CMKY: C: n/a%, M: n/a%, Y: n/a%, K: n/a%"; this.cmkylabel.Text = "CMKY: C: n/a%, M: n/a%, Y: n/a%, K: n/a%";
this.cmkylabel.TextAlign = System.Drawing.ContentAlignment.TopCenter; this.cmkylabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.cmkylabel.Click += new System.EventHandler(this.cmkylabel_Click);
// //
// previewlabel // previewlabel
// //
this.previewlabel.AutoSize = true; this.previewlabel.AutoSize = true;
this.previewlabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold); this.previewlabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
this.previewlabel.ForeColor = System.Drawing.Color.White; this.previewlabel.ForeColor = System.Drawing.Color.White;
this.previewlabel.Location = new System.Drawing.Point(147, 115); this.previewlabel.Location = new System.Drawing.Point(147, 140);
this.previewlabel.Name = "previewlabel"; this.previewlabel.Name = "previewlabel";
this.previewlabel.Size = new System.Drawing.Size(56, 15); this.previewlabel.Size = new System.Drawing.Size(56, 15);
this.previewlabel.TabIndex = 5; this.previewlabel.TabIndex = 5;
@@ -99,7 +106,7 @@
// //
this.colorpreview.BackColor = System.Drawing.Color.White; this.colorpreview.BackColor = System.Drawing.Color.White;
this.colorpreview.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.colorpreview.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.colorpreview.Location = new System.Drawing.Point(75, 140); this.colorpreview.Location = new System.Drawing.Point(75, 165);
this.colorpreview.Name = "colorpreview"; this.colorpreview.Name = "colorpreview";
this.colorpreview.Size = new System.Drawing.Size(200, 100); this.colorpreview.Size = new System.Drawing.Size(200, 100);
this.colorpreview.TabIndex = 6; this.colorpreview.TabIndex = 6;
@@ -109,7 +116,7 @@
this.bindlabel.AutoSize = true; this.bindlabel.AutoSize = true;
this.bindlabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold); this.bindlabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
this.bindlabel.ForeColor = System.Drawing.Color.White; this.bindlabel.ForeColor = System.Drawing.Color.White;
this.bindlabel.Location = new System.Drawing.Point(77, 250); this.bindlabel.Location = new System.Drawing.Point(77, 275);
this.bindlabel.Name = "bindlabel"; this.bindlabel.Name = "bindlabel";
this.bindlabel.Size = new System.Drawing.Size(195, 30); this.bindlabel.Size = new System.Drawing.Size(195, 30);
this.bindlabel.TabIndex = 7; this.bindlabel.TabIndex = 7;
@@ -120,19 +127,33 @@
// //
this.statuslabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold); this.statuslabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
this.statuslabel.ForeColor = System.Drawing.Color.White; this.statuslabel.ForeColor = System.Drawing.Color.White;
this.statuslabel.Location = new System.Drawing.Point(95, 290); this.statuslabel.Location = new System.Drawing.Point(95, 315);
this.statuslabel.Name = "statuslabel"; this.statuslabel.Name = "statuslabel";
this.statuslabel.Size = new System.Drawing.Size(160, 15); this.statuslabel.Size = new System.Drawing.Size(160, 15);
this.statuslabel.TabIndex = 8; this.statuslabel.TabIndex = 8;
this.statuslabel.Text = "Current Status: Unlocked"; this.statuslabel.Text = "Current Status: Unlocked";
this.statuslabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; this.statuslabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
// //
// hsllabel
//
this.hsllabel.Cursor = System.Windows.Forms.Cursors.Hand;
this.hsllabel.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
this.hsllabel.ForeColor = System.Drawing.Color.White;
this.hsllabel.Location = new System.Drawing.Point(100, 65);
this.hsllabel.Name = "hsllabel";
this.hsllabel.Size = new System.Drawing.Size(150, 15);
this.hsllabel.TabIndex = 9;
this.hsllabel.Text = "HSL: n/a°, n/a%, n/a%";
this.hsllabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.hsllabel.Click += new System.EventHandler(this.hsllabel_Click);
//
// ColorPicker // ColorPicker
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30))))); this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(30)))), ((int)(((byte)(30)))), ((int)(((byte)(30)))));
this.ClientSize = new System.Drawing.Size(334, 326); this.ClientSize = new System.Drawing.Size(334, 351);
this.Controls.Add(this.hsllabel);
this.Controls.Add(this.statuslabel); this.Controls.Add(this.statuslabel);
this.Controls.Add(this.bindlabel); this.Controls.Add(this.bindlabel);
this.Controls.Add(this.colorpreview); this.Controls.Add(this.colorpreview);
@@ -158,6 +179,7 @@
private System.Windows.Forms.Panel colorpreview; private System.Windows.Forms.Panel colorpreview;
private System.Windows.Forms.Label bindlabel; private System.Windows.Forms.Label bindlabel;
private System.Windows.Forms.Label statuslabel; private System.Windows.Forms.Label statuslabel;
private System.Windows.Forms.Label hsllabel;
} }
} }
+109 -4
View File
@@ -15,6 +15,8 @@ namespace ColorPicker
private IntPtr _hookID = IntPtr.Zero; private IntPtr _hookID = IntPtr.Zero;
private bool _isUnlocked = true; private bool _isUnlocked = true;
private Color _color;
public ColorPicker() public ColorPicker()
{ {
InitializeComponent(); InitializeComponent();
@@ -75,7 +77,6 @@ namespace ColorPicker
Console.WriteLine($"Error: {ex.Message}"); Console.WriteLine($"Error: {ex.Message}");
} }
} }
private void UpdateColorDisplay(Color color) private void UpdateColorDisplay(Color color)
{ {
if (InvokeRequired) if (InvokeRequired)
@@ -84,6 +85,8 @@ namespace ColorPicker
} }
else else
{ {
_color = color;
int r = color.R; int r = color.R;
int g = color.G; int g = color.G;
int b = color.B; int b = color.B;
@@ -104,16 +107,118 @@ namespace ColorPicker
c = m = y = 0; c = m = y = 0;
} }
float rNorm = r / 255f;
float gNorm = g / 255f;
float bNorm = b / 255f;
float max = Math.Max(rNorm, Math.Max(gNorm, bNorm));
float min = Math.Min(rNorm, Math.Min(gNorm, bNorm));
float h, s, l = (max + min) / 2;
if (max == min)
{
h = s = 0;
}
else
{
float d = max - min;
s = l > 0.5f ? d / (2 - max - min) : d / (max + min);
if (max == rNorm)
{
h = (gNorm - bNorm) / d + (gNorm < bNorm ? 6 : 0);
}
else if (max == gNorm)
{
h = (bNorm - rNorm) / d + 2;
}
else
{
h = (rNorm - gNorm) / d + 4;
}
h /= 6;
}
hexlabel.Text = $"#{r:X2}{g:X2}{b:X2}"; hexlabel.Text = $"#{r:X2}{g:X2}{b:X2}";
rgblabel.Text = $"R: {r}, G: {g}, B: {b}"; rgblabel.Text = $"R: {r}, G: {g}, B: {b}";
cmkylabel.Text = $"CMYK: C: {(c * 100):0}%, M: {(m * 100):0}%, Y: {(y * 100):0}%, K: {(k * 100):0}%"; hsllabel.Text = $"H: {(h * 360):0}°, S: {(s * 100):0}%, L: {(l * 100):0}%";
cmkylabel.Text = $"C: {(c * 100):0}%, M: {(m * 100):0}%, Y: {(y * 100):0}%, K: {(k * 100):0}%";
colorpreview.BackColor = color; colorpreview.BackColor = color;
} }
} }
private string ColorToHex(Color color) private void CopyToClipboard(string text)
{ {
return $"#{color.R:X2}{color.G:X2}{color.B:X2}"; Clipboard.SetText(text);
}
private void hexlabel_Click(object sender, EventArgs e)
{
CopyToClipboard($"#{_color.R:X2}{_color.G:X2}{_color.B:X2}");
}
private void rgblabel_Click(object sender, EventArgs e)
{
CopyToClipboard($"R: {_color.R}, G: {_color.G}, B: {_color.B}");
}
private void hsllabel_Click(object sender, EventArgs e)
{
float r = _color.R / 255f;
float g = _color.G / 255f;
float b = _color.B / 255f;
float max = Math.Max(r, Math.Max(g, b));
float min = Math.Min(r, Math.Min(g, b));
float h, s, l = (max + min) / 2;
if (max == min)
{
h = s = 0;
}
else
{
float d = max - min;
s = l > 0.5f ? d / (2 - max - min) : d / (max + min);
if (max == r)
{
h = (g - b) / d + (g < b ? 6 : 0);
}
else if (max == g)
{
h = (b - r) / d + 2;
}
else
{
h = (r - g) / d + 4;
}
h /= 6;
}
CopyToClipboard($"H: {(h * 360):0}°, S: {(s * 100):0}%, L: {(l * 100):0}%");
}
private void cmkylabel_Click(object sender, EventArgs e)
{
float c = 1 - _color.R / 255f;
float m = 1 - _color.G / 255f;
float y = 1 - _color.B / 255f;
float k = Math.Min(c, Math.Min(m, y));
if (k < 1)
{
c = (c - k) / (1 - k);
m = (m - k) / (1 - k);
y = (y - k) / (1 - k);
}
else
{
c = m = y = 0;
}
CopyToClipboard($"C: {(c * 100):0}%, M: {(m * 100):0}%, Y: {(y * 100):0}%, K: {(k * 100):0}%");
} }
private struct MouseHookStruct private struct MouseHookStruct