コード例 #1
0
        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);
        }