public static Color FromHSB(HSBColor 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 * 360f / 65535f; 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 ( (byte)0xff, (byte)Math.Round(Math.Min(Math.Max(r, 0), 255)), (byte)Math.Round(Math.Min(Math.Max(g, 0), 255)), (byte)Math.Round(Math.Min(Math.Max(b, 0), 255)) )); }
public bool IsEqualToColor(HSBColor other) { return((h == other.H) && (s == other.S) && (b == other.B)); }
public static Color FromHSB(HSBColor 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 * 360f / 65535f; 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 ( (byte)0xff, (byte)Math.Round(Math.Min(Math.Max(r, 0), 255)), (byte)Math.Round(Math.Min(Math.Max(g, 0), 255)), (byte)Math.Round(Math.Min(Math.Max(b, 0), 255)) ); }
public bool IsEqualToColor(HSBColor other) { return ((h == other.H) && (s == other.S) && (b == other.B)); }
public static HSBColor FromColor(Color color) { HSBColor ret = new HSBColor(0f, 0f, 0f); 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 *= 65535f / 360f; ret.s = (dif / max) * 255f; ret.b = max; return ret; }
public static HueTheme CreateThemeFromJson(string content) { try { HueTheme theme = new HueTheme(); JObject json = (JObject)JObject.Parse(content); // Name JToken nameToken; if (json.TryGetValue("name", out nameToken)) { theme.Name = json["name"].ToString(); } // Is system theme JToken isSystemToken; if (json.TryGetValue("is_system_default", out isSystemToken)) { theme.IsSystemTheme = bool.Parse(json["is_system_default"].ToString()); } // Banner image JToken imageToken; if (json.TryGetValue("banner_image", out imageToken)) { string imageString = json["banner_image"].ToString(); if (imageString.Length == 0) { theme.BannerImage = null; } else { theme.BannerImage = imageString; } } // Color list JToken colorToken; if (json.TryGetValue("colors", out colorToken)) { JArray colorArray = (JArray)json["colors"]; foreach (JObject colorJson in colorArray) { int h = int.Parse(colorJson["h"].ToString()); int s = int.Parse(colorJson["s"].ToString()); int b = int.Parse(colorJson["b"].ToString()); var color = new HSBColor(h, s, b); theme.ColorList.Add(color); } } // Default color list JToken defaultColorToken; if (json.TryGetValue("default_colors", out defaultColorToken)) { JArray colorArray = (JArray)json["default_colors"]; foreach (JObject colorJson in colorArray) { int h = int.Parse(colorJson["h"].ToString()); int s = int.Parse(colorJson["s"].ToString()); int b = int.Parse(colorJson["b"].ToString()); var color = new HSBColor(h, s, b); theme.DefaultColorList.Add(color); } } return theme; } catch (Exception ex) { Debug.WriteLine(ex); return null; } }