public static System.Drawing.Color HSBToRGB(HSBCOLOR hsbColor) { int tmpColorR = 0, tmpColorG = 0, tmpColorB = 0; int tmpMaximum = HuiruiSoft.Drawing.ColorTranslator.Round(hsbColor.Brightness * 255); int tmpMinimum = HuiruiSoft.Drawing.ColorTranslator.Round((1.0 - hsbColor.Saturation) * (hsbColor.Brightness / 1.0) * 255); var tmpMargin = (double)(tmpMaximum - tmpMinimum) / 255; if (hsbColor.Hue >= 0 && hsbColor.Hue <= (double)1 / 6) { tmpColorR = tmpMaximum; tmpColorG = HuiruiSoft.Drawing.ColorTranslator.Round(((hsbColor.Hue - 0) * tmpMargin) * 1530 + tmpMinimum); tmpColorB = tmpMinimum; } else if (hsbColor.Hue <= (double)1 / 3) { tmpColorR = HuiruiSoft.Drawing.ColorTranslator.Round(-((hsbColor.Hue - (double)1 / 6) * tmpMargin) * 1530 + tmpMaximum); tmpColorG = tmpMaximum; tmpColorB = tmpMinimum; } else if (hsbColor.Hue <= 0.5) { tmpColorR = tmpMinimum; tmpColorG = tmpMaximum; tmpColorB = HuiruiSoft.Drawing.ColorTranslator.Round(((hsbColor.Hue - (double)1 / 3) * tmpMargin) * 1530 + tmpMinimum); } else if (hsbColor.Hue <= (double)2 / 3) { tmpColorR = tmpMinimum; tmpColorG = HuiruiSoft.Drawing.ColorTranslator.Round(-((hsbColor.Hue - 0.5) * tmpMargin) * 1530 + tmpMaximum); tmpColorB = tmpMaximum; } else if (hsbColor.Hue <= (double)5 / 6) { tmpColorR = HuiruiSoft.Drawing.ColorTranslator.Round(((hsbColor.Hue - (double)2 / 3) * tmpMargin) * 1530 + tmpMinimum); tmpColorG = tmpMinimum; tmpColorB = tmpMaximum; } else if (hsbColor.Hue <= 1.0) { tmpColorR = tmpMaximum; tmpColorG = tmpMinimum; tmpColorB = HuiruiSoft.Drawing.ColorTranslator.Round(-((hsbColor.Hue - (double)5 / 6) * tmpMargin) * 1530 + tmpMaximum); } return(System.Drawing.Color.FromArgb(tmpColorR, tmpColorG, tmpColorB)); }
public static HSBCOLOR RGBToHSB(System.Drawing.Color color) { var tmpHSBColor = new HSBCOLOR( ); int tmpMaximum, tmpMinimum; if (color.R > color.G) { tmpMaximum = color.R; tmpMinimum = color.G; } else { tmpMaximum = color.G; tmpMinimum = color.R; } if (color.B > tmpMaximum) { tmpMaximum = color.B; } else if (color.B < tmpMinimum) { tmpMinimum = color.B; } int tmpDifference = tmpMaximum - tmpMinimum; tmpHSBColor.Brightness = (double)tmpMaximum / 255; if (tmpMaximum == 0) { tmpHSBColor.Saturation = 0; } else { tmpHSBColor.Saturation = (double)tmpDifference / tmpMaximum; } double q; if (tmpDifference == 0) { q = 0; } else { q = (double)60 / tmpDifference; } if (tmpMaximum == color.R) { if (color.G >= color.B) { tmpHSBColor.Hue = q * (color.G - color.B) / 360; } else { tmpHSBColor.Hue = (360 + q * (color.G - color.B)) / 360; } } else if (tmpMaximum == color.G) { tmpHSBColor.Hue = (120 + q * (color.B - color.R)) / 360; } else if (tmpMaximum == color.B) { tmpHSBColor.Hue = (240 + q * (color.R - color.G)) / 360; } else { tmpHSBColor.Hue = 0.0; } return(tmpHSBColor); }
public static System.Drawing.Color ToColor(HSBCOLOR hsbColor) { return(HuiruiSoft.Drawing.ColorTranslator.HSBToRGB(hsbColor)); }