public HlaeColorUc(HlaeColor value) { R = (byte)Math.Min(Math.Max(0, value.R * 255f), 255f); G = (byte)Math.Min(Math.Max(0, value.G * 255f), 255f); B = (byte)Math.Min(Math.Max(0, value.B * 255f), 255f); A = (byte)Math.Min(Math.Max(0, value.A * 255f), 255f); }
public float CalcDistanceSquared(HlaeColor other) { float dR = other.R - R; float dG = other.G - G; float dB = other.B - B; float dA = other.A - A; return(dR * dR + dG * dG + dB * dB + dA * dA); }
public float CalcDistanceSquared(HlaeColor other, float w) { float dR = other.R - R; float dG = other.G - G; float dB = other.B - B; float dA = other.A - A; float result = dR * dR + dG * dG + dB * dB + dA * dA; if (0 != w) { result = result / w; } return(result); }
private bool Iterate(float r, float g, float b, float a, out float outR, out float outG, out float outB, out float outA) { HlaeColor x = new HlaeColor(r, g, b, a); HlaeColor y = new HlaeColor(0, 0, 0, 0); float s = 0; for (int iR = 0; iR < (int)resR.Value; ++iR) { float fiR = iR / (float)(resR.Value - 1); for (int iG = 0; iG < (int)resG.Value; ++iG) { float fiG = iG / (float)(resG.Value - 1); for (int iB = 0; iB < (int)resB.Value; ++iB) { float fiB = iB / (float)(resB.Value - 1); for (int iA = 0; iA < (int)resA.Value; ++iA) { float fiA = iA / (float)(resA.Value - 1); HlaeColor xx = new HlaeColor(fiR, fiG, fiB, fiA); bool hasBest = false; KeyValuePair <HlaeCommentedColor, HlaeWeightedColor> best = m_Colors.First(); float bestDist = 0; float dist; foreach (KeyValuePair <HlaeCommentedColor, HlaeWeightedColor> kvp in m_Colors) { dist = xx.CalcDistanceSquared(kvp.Key.Color); if (!hasBest || dist < bestDist) { hasBest = true; bestDist = dist; best = kvp; } } if (!hasBest) { outR = best.Value.Color.R; outG = best.Value.Color.G; outB = best.Value.Color.B; outA = best.Value.Color.A; return(false); } dist = xx.CalcDistanceSquared(x); if (dist < bestDist) { y = y + best.Value.Color * best.Value.Weight; s = s + best.Value.Weight; } } } } } if (0 != s) { y = y / s; } outR = y.R; outG = y.G; outB = y.B; outA = y.A; previewBitmap.SetPixel(Math.Max(Math.Min(previewBitmap.Width - 1, (int)(r * (float)(previewBitmap.Width - 1) + 0.5f)), 0), Math.Max(Math.Min(previewBitmap.Height - 1, (int)(g * (float)(previewBitmap.Height - 1) + 0.5f)), 0), new HlaeColorUc(y).ToColor()); int curTick = System.Environment.TickCount; if (1000 <= Math.Abs(curTick - lastTick)) { lastTick = curTick; using (Graphics gfx = Graphics.FromImage(preview.Image)) { gfx.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; gfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; gfx.DrawImage(previewBitmap, 0, 0, preview.Width, preview.Height); } trackZ.Value = Math.Max(Math.Min(trackZ.Maximum, (int)(b * (float)(trackZ.Maximum))), 0); trackW.Value = Math.Max(Math.Min(trackW.Maximum, (int)(a * (float)(trackW.Maximum))), 0); preview.Refresh(); } return(true); }
public HlaeWeightedColor(float r, float g, float b, float a, float weight = 1) { Color = new HlaeColor(r, g, b, a); Weight = weight; }
public HlaeWeightedColor(byte r, byte g, byte b, byte a, float weight = 1) { Color = new HlaeColor(r, g, b, a); Weight = weight; }
public HlaeCommentedColor(float r, float g, float b, float a, string comment = "") { Color = new HlaeColor(r, g, b, a); Comment = comment; }
public HlaeCommentedColor(byte r, byte g, byte b, byte a, string comment = "") { Color = new HlaeColor(r, g, b, a); Comment = comment; }