/// <summary> /// RGBカラーからLCHカラースベースへ変換する /// </summary> /// <param name="rgb">RGBカラー</param> /// <returns>LCHカラースベース</returns> internal static Lch ToColor(Rgb rgb) { Lch lch = new Lch(); Lab lab = LabConverter.ToColor(rgb); var h = Math.Atan2(lab.B, lab.A); if (h > 0) { h = (h / Math.PI) * 180.0; } else { h = 360 - (Math.Abs(h) / Math.PI) * 180.0; } if (h < 0) { h += 360.0; } else if (h >= 360) { h -= 360.0; } lch.L = lab.L; lch.C = Math.Sqrt(lab.A * lab.A + lab.B * lab.B); lch.H = h; return(lch); }
/// <summary> /// RGBカラーからLAB文字列へ変換する /// </summary> /// <param name="rgb">RGBカラー</param> /// <returns>LAB文字列</returns> public string getLabString(Rgb rgb) { Lab lab = LabConverter.ToColor(rgb); return("L" + ((int)lab.L).ToString() + " A" + ((int)lab.A).ToString() + " B" + ((int)lab.B).ToString()); }
/// <summary> /// RGBカラーからMUNSELLカラースベースへ変換する /// </summary> /// <param name="rgb">RGBカラー</param> /// <returns>MUNSELLカラースベース</returns> internal static Munsell ToColor(Rgb rgb) { Munsell item = new Munsell(); Lch lch = LchConverter.ToColor(rgb); if (rgb.R == rgb.G && rgb.G == rgb.B) { Lab lab = LabConverter.ToColor(rgb); item.H = new MunsellHue { Base = HueBase.N }; item.V = Math.Round(ConvertLtoV(lab.L), 1); return(item); } var q = Table.Select(x => new { diff = Math.Abs(x.Lch.L - lch.L) + Math.Abs(x.Lch.C - lch.C) + Math.Abs(x.Lch.H - lch.H), self = x }); var min = q.Min(x => x.diff); var munsell = q.Where(x => x.diff == min).FirstOrDefault().self; if (min < 3.0) { item.H = munsell.H; item.V = munsell.V; item.C = munsell.C; return(item); } var hue = new MunsellHue { Base = munsell.H.Base, Number = munsell.H.Number }; MunsellHue newHue; if (munsell.Lch.H > lch.H) { hue.Number -= 2.5; newHue = new MunsellHue { Base = hue.Base, Number = hue.Number }; } else { hue.Number += 2.5; newHue = new MunsellHue { Base = munsell.H.Base, Number = munsell.H.Number }; } var munsellX = FindMunsell(hue, munsell.V, munsell.C, true); newHue.Number += Math.Round((lch.H - Math.Min(munsell.Lch.H, munsellX.Lch.H)) / Math.Abs(munsell.Lch.H - munsellX.Lch.H) * 2.5, 1, MidpointRounding.AwayFromZero); double newChroma; //彩度max min var c = Table.Where(x => x.H == munsell.H && x.V == munsell.V) .GroupBy(x => x.V).Select(x => new { min = x.Min(y => y.C), max = x.Max(y => y.C) }).First(); if (c.min < munsell.C && munsell.C < c.max) { var chroma = munsell.Lch.C > lch.C ? munsell.C - 2.0 : munsell.C + 2.0; var munsellY = FindMunsell(munsell.H, munsell.V, chroma); newChroma = Math.Round(Math.Min(munsell.C, munsellY.C) + (lch.C - Math.Min(munsell.Lch.C, munsellY.Lch.C)) / Math.Abs(munsell.Lch.C - munsellY.Lch.C) * 2.0, 1, MidpointRounding.AwayFromZero); } else { newChroma = Math.Round(munsell.C / munsell.Lch.C * lch.C, 1, MidpointRounding.AwayFromZero); } var newValue = Math.Round(ConvertLtoV(lch.L), 1, MidpointRounding.AwayFromZero); item.H = newHue; item.C = newChroma; item.V = newValue; return(item); }