public static Color ShiftSaturation(Color c, float saturationDelta) { ColorHSB hsb = ColorHSB.FromColor(c); hsb.s += saturationDelta; hsb.s = Math.Min(Math.Max(hsb.s, 0), 255); return(FromHSB(hsb)); }
public static Color ShiftBrighness(Color c, float brightnessDelta) { ColorHSB hsb = ColorHSB.FromColor(c); hsb.b += brightnessDelta; hsb.b = Math.Min(Math.Max(hsb.b, 0), 255); return(FromHSB(hsb)); }
public static Color ShiftHue(Color c, float hueDelta) { ColorHSB hsb = ColorHSB.FromColor(c); hsb.h += hueDelta; hsb.h = Math.Min(Math.Max(hsb.h, 0), 360); return(FromHSB(hsb)); }
public ColorHSB(Color color) { ColorHSB temp = FromColor(color); this.a = temp.a; this.h = temp.h; this.s = temp.s; this.b = temp.b; }
public static ColorHSB FromColor(Color color) { ColorHSB ret = new ColorHSB(0f, 0f, 0f); ret.a = color.A; float r = color.R; float g = color.G; float b = color.B; float max = Math.Max(r, Math.Max(g, b)); if (max <= 0) { return(ret); } float min = Math.Min(r, Math.Min(g, b)); float dif = max - min; if (max > min) { if (g == max) { ret.h = (b - r) / dif * 60f + 120f; } else if (b == max) { ret.h = (r - g) / dif * 60f + 240f; } else if (b > g) { ret.h = (g - b) / dif * 60f + 360f; } else { ret.h = (g - b) / dif * 60f; } if (ret.h < 0) { ret.h = ret.h + 360f; } } else { ret.h = 0; } //ret.h *= 255f / 360f; ret.s = (dif / max) * 255f; ret.b = max; return(ret); }
private void b_WinPick_Click(object sender, EventArgs e) { ColorDialog cd = new ColorDialog(); cd.FullOpen = true; cd.ShowDialog(); if (cd.Color == null) { return; } cTwo = new ColorHSB(cd.Color); RefreshControls(true, true, true); }
private void SetFromSliders() { float fh = a_Set_Hue.Value; float fs = a_Set_Sat.Value; float fv = a_Set_Bri.Value; float fhd = a_Diff_Hue.Value; float fsd = a_Diff_Sat.Value; float fvd = a_Diff_Bri.Value; cOne = new ColorHSB(fh, fs, fv); cOneD = new ColorHSB(fhd, fsd, fvd); float th = b_Set_Hue.Value; float ts = b_Set_Sat.Value; float tv = b_Set_Bri.Value; float thd = b_Diff_Hue.Value; float tsd = b_Diff_Sat.Value; float tvd = b_Diff_Bri.Value; cTwo = new ColorHSB(th, ts, tv); cTwoD = new ColorHSB(thd, tsd, tvd); RefreshControls(true, true, false); }
public static Color FromHSB(ColorHSB hsbColor) { float r = hsbColor.b; float g = hsbColor.b; float b = hsbColor.b; if (hsbColor.s != 0) { float max = hsbColor.b; float dif = hsbColor.b * hsbColor.s / 255f; float min = hsbColor.b - dif; float h = hsbColor.h; if (h < 60f) { r = max; g = h * dif / 60f + min; b = min; } else if (h < 120f) { r = -(h - 120f) * dif / 60f + min; g = max; b = min; } else if (h < 180f) { r = min; g = max; b = (h - 120f) * dif / 60f + min; } else if (h < 240f) { r = min; g = -(h - 240f) * dif / 60f + min; b = max; } else if (h < 300f) { r = (h - 240f) * dif / 60f + min; g = min; b = max; } else if (h <= 360f) { r = max; g = min; b = -(h - 360f) * dif / 60 + min; } else { r = 0; g = 0; b = 0; } } return(Color.FromArgb( hsbColor.a, (int)Math.Round(Math.Min(Math.Max(r, 0), 255)), (int)Math.Round(Math.Min(Math.Max(g, 0), 255)), (int)Math.Round(Math.Min(Math.Max(b, 0), 255)))); }
private void cmdCancel_Click(object sender, EventArgs e) { cOne = null; cTwo = null; cOneD = null; cTwoD = null; this.Close(); }
private void SetManually(int iSauce, string sValues, bool bRGB, bool bHEX, bool bHSB) { if (bRGB) { try { string[] vals = sValues.Split(','); if (vals.Length != 3) { throw new Exception(); } int r = Convert.ToInt32(vals[0].Trim()); int g = Convert.ToInt32(vals[1].Trim()); int b = Convert.ToInt32(vals[2].Trim()); ColorHSB col = new ColorHSB(Color.FromArgb(r, g, b)); if (iSauce == 1) { cOne = col; } else { cTwo = col; } } catch { MessageBox.Show("That is not a valid RGB colour!" + "\r\n\r\n" + "Valid example: 96, 192, 255", "You are doing it wrong.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } if (bHEX) { string val = sValues.Substring(1); if (val.Length == 3) { try { int r = Convert.ToInt32(val[0].ToString(), 16) * 17; int g = Convert.ToInt32(val[1].ToString(), 16) * 17; int b = Convert.ToInt32(val[2].ToString(), 16) * 17; ColorHSB col = new ColorHSB(Color.FromArgb(r, g, b)); if (iSauce == 1) { cOne = col; } else { cTwo = col; } } catch { MessageBox.Show("That is not a valid 3-digit hexadecimal colour!" + "\r\n\r\n" + "Valid example: #8ac" + "\r\n" + "Valid example: #1379AE", "You are doing it wrong.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } else if (val.Length == 6) { try { int r = Convert.ToInt32(val.Substring(0, 2), 16); int g = Convert.ToInt32(val.Substring(2, 2), 16); int b = Convert.ToInt32(val.Substring(4, 2), 16); ColorHSB col = new ColorHSB(Color.FromArgb(r, g, b)); if (iSauce == 1) { cOne = col; } else { cTwo = col; } } catch { MessageBox.Show("That is not a valid 6-digit hexadecimal colour!" + "\r\n\r\n" + "Valid example: #8ac" + "\r\n" + "Valid example: #1379AE", "You are doing it wrong.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } else { MessageBox.Show("That is not a valid hexadecimal colour!" + "\r\n\r\n" + "Valid example: #8ac" + "\r\n" + "Valid example: #1379AE", "You are doing it wrong.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } if (bHSB) { try { string[] vals = sValues.Split('.'); if (vals.Length != 3) { throw new Exception(); } int h = Convert.ToInt32(vals[0].Trim()); int s = Convert.ToInt32(vals[1].Trim()); int b = Convert.ToInt32(vals[2].Trim()); ColorHSB col = new ColorHSB(h, s, b); if (iSauce == 1) { cOne = col; } else { cTwo = col; } } catch { MessageBox.Show("That is not a valid HSV colour!" + "\r\n\r\n" + "Valid example: 320, 224, 128", "You are doing it wrong.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } RefreshControls(true, true, true); }
private void RefreshControls(bool bGUI, bool bManual, bool bSliders) { int fr = cOne.Color.R; int fg = cOne.Color.G; int fb = cOne.Color.B; int fh = (int)Math.Round(cOne.H); int fs = (int)Math.Round(cOne.S); int fv = (int)Math.Round(cOne.B); if (fh == 360) { fh = 0; } int fhd = (int)Math.Round(cOneD.H); int fsd = (int)Math.Round(cOneD.S); int fvd = (int)Math.Round(cOneD.B); if (fhd == 360) { fhd = 0; } int tr = cTwo.Color.R; int tg = cTwo.Color.G; int tb = cTwo.Color.B; int th = (int)Math.Round(cTwo.H); int ts = (int)Math.Round(cTwo.S); int tv = (int)Math.Round(cTwo.B); if (th == 360) { th = 0; } int thd = (int)Math.Round(cTwoD.H); int tsd = (int)Math.Round(cTwoD.S); int tvd = (int)Math.Round(cTwoD.B); if (thd == 360) { thd = 0; } if (bManual) { if (a_RGB.Checked) { a_ManValue.Text = fr.ToString("d") + ", " + fg.ToString("d") + ", " + fb.ToString("d"); } if (a_HSB.Checked) { a_ManValue.Text = fh.ToString("d") + "." + fs.ToString("d") + "." + fv.ToString("d"); } if (a_HEX.Checked) { a_ManValue.Text = "#" + fr.ToString("X2") + fg.ToString("X2") + fb.ToString("X2"); } if (b_RGB.Checked) { b_ManValue.Text = tr.ToString("d") + ", " + tg.ToString("d") + ", " + tb.ToString("d"); } if (b_HSB.Checked) { b_ManValue.Text = th.ToString("d") + "." + ts.ToString("d") + "." + tv.ToString("d"); } if (b_HEX.Checked) { b_ManValue.Text = "#" + tr.ToString("X2") + tg.ToString("X2") + tb.ToString("X2"); } } if (bSliders) { a_Set_Hue.Value = fh; a_Set_Sat.Value = fs; a_Set_Bri.Value = fv; a_Diff_Hue.Value = fhd; a_Diff_Sat.Value = fsd; a_Diff_Bri.Value = fvd; b_Set_Hue.Value = th; b_Set_Sat.Value = ts; b_Set_Bri.Value = tv; b_Diff_Hue.Value = thd; b_Diff_Sat.Value = tsd; b_Diff_Bri.Value = tvd; } if (bGUI) { ColorHSB cMinA = new ColorHSB(cOne.H - fhd, cOne.S - fsd, cOne.B - fvd); ColorHSB cMaxA = new ColorHSB(cOne.H + fhd, cOne.S + fsd, cOne.B + fvd); a_Min.BackColor = cMinA.Color; a_Avg.BackColor = cOne.Color; a_Max.BackColor = cMaxA.Color; ColorHSB cMinB = new ColorHSB(cTwo.H - thd, cTwo.S - tsd, cTwo.B - tvd); ColorHSB cMaxB = new ColorHSB(cTwo.H + thd, cTwo.S + tsd, cTwo.B + tvd); b_Min.BackColor = cMinB.Color; b_Avg.BackColor = cTwo.Color; b_Max.BackColor = cMaxB.Color; } }