# HG changeset patch # User Jason Maltzen # Date 2018-05-18 00:19:38 # Node ID 2f97b4ec280b5416fec41926fac36465dfcec926 # Parent 5db137e7ce9040653d7abbea76ba6b27a0e7c900 Use value from HSV for determining whether text should be white or black in exported wiki format recipes. diff --git a/PlayerProfile.cs b/PlayerProfile.cs --- a/PlayerProfile.cs +++ b/PlayerProfile.cs @@ -508,7 +508,57 @@ namespace DesertPaintLab { ExportWikiFormat(writer, this.ribbonRecipes); } - + + static public PaintColor RgbToHsv(PaintColor color) + { + float h = 0; + float s = 0; + float r = color.Red / 255.0f; + float g = color.Green / 255.0f; + float b = color.Blue / 255.0f; + float cmax = Math.Max(Math.Max(r, g), b); + float cmin = Math.Min(Math.Min(r, g), b); + float cd = cmax - cmin; + if (cd == 0) + { + h = 0; + } + else if (cmax == r) + { + h = (((g - b) / cd) % 6) / 6f; + } + else if (cmax == g) + { + h = (((b - r) / cd) + 2) / 6f; + } + else + { + h = (((r - g) / cd) + 4) / 6f; + } + + if (cmax == 0) + { + s = 0; + } + else + { + s = cd / cmax; + } + + if (h < 0) + { + h = 1 + h; + } + + if (h >= 1) + { + h = 0; + } + + + return new PaintColor((byte)(h * 255), (byte)(s * 255), (byte)(cmax * 255)); + } + public void ExportWikiFormat(TextWriter writer, SortedDictionary recipeDict) { PaintRecipe recipe; @@ -522,17 +572,16 @@ namespace DesertPaintLab writer.WriteLine("|-"); string colorLine = "| "; colorLine += "style=\"font-weight: bold; background-color: #" + color.Red.ToString("X2") + color.Green.ToString("X2") + color.Blue.ToString("X2") + ";"; - float rPortion = color.Red / 255.0f; - float gPortion = color.Green / 255.0f; - float bPortion = color.Blue / 255.0f; - float maxColor = Math.Max(rPortion, Math.Max(gPortion, bPortion)); - float minColor = Math.Min(rPortion, Math.Min(gPortion, bPortion)); - float luminance = (maxColor - minColor) * 0.5f; - if (luminance < 0.5f) + PaintColor hsvColor = RgbToHsv(color); + if (hsvColor.Blue < 200) // value { // dark color gets light text colorLine += " color: #FFFFFF;"; } + else + { + colorLine += "color: #000000;"; + } colorLine += "\" | " + color.Name + " || "; if (recipeDict.TryGetValue(color.Name, out recipe)) {