예제 #1
0
        /// <summary>
        /// creates color blend out of color point data
        /// </summary>
        /// <returns></returns>
        private ColorBlend CreateColorBlend()
        {
            //sort all points
            ColorPoint[] colpoints   = Colors.SortedArray();
            AlphaPoint[] alphapoints = Alphas.SortedArray();
            //init out vars
            SortedList <float, Color> positions = new SortedList <float, Color>();

            //add color points
            for (int c = 0; c < colpoints.Length; c++)
            {
                if (c > 0 && colpoints[c].Focus != .5)                //focus
                {
                    AddPosition(colpoints, alphapoints, positions,
                                colpoints[c - 1].Position + (colpoints[c].Position - colpoints[c - 1].Position) * colpoints[c].Focus);
                }
                //color
                AddPosition(colpoints, alphapoints, positions, colpoints[c].Position);
            }
            //add alpha points
            for (int a = 0; a < alphapoints.Length; a++)
            {
                if (a > 0 && alphapoints[a].Focus != .5)                //focus
                {
                    AddPosition(colpoints, alphapoints, positions,
                                alphapoints[a - 1].Position + (alphapoints[a].Position - alphapoints[a - 1].Position) * alphapoints[a].Focus);
                }
                //alpha
                AddPosition(colpoints, alphapoints, positions, alphapoints[a].Position);
            }

            //add first/last point
            if (positions.Count < 1 || !positions.ContainsKey(0f))
            {
                positions.Add(0f, positions.Count < 1 ?
                              Color.Transparent : positions.Values[0]);
            }
            if (positions.Count < 2 || !positions.ContainsKey(1f))
            {
                positions.Add(1f, positions.Count < 2 ?
                              Color.Transparent : positions.Values[positions.Count - 1]);
            }
            //
            ColorBlend ret = new ColorBlend();

            Color[] col = new Color[positions.Count];
            positions.Values.CopyTo(col, 0);
            ret.Colors = col;
            float[] pos = new float[positions.Count];
            positions.Keys.CopyTo(pos, 0);
            ret.Positions = pos;
            return(ret);
        }
예제 #2
0
        /// <summary>
        /// creates color blend out of color point data
        /// </summary>
        /// <returns></returns>
        private ColorBlend CreateColorBlend(Color?filterColor = null)
        {
            //sort all points
            ColorPoint[] colpoints   = Colors.SortedArray();
            AlphaPoint[] alphapoints = Alphas.SortedArray();
            //init out vars
            SortedList <float, Color> positions = new SortedList <float, Color>();

            // if we're filtering the colors, the iterate through the color list, setting all non-matching colors to black
            if (filterColor != null)
            {
                List <ColorPoint> newColorPoints = new List <ColorPoint>();
                foreach (ColorPoint colorPoint in colpoints)
                {
                    ColorPoint newPoint = (ColorPoint)colorPoint.Clone();
                    if (newPoint.Color.ToRGB().ToArgb() != filterColor)
                    {
                        // it's not the desired color. Make it black and add it, but only if there's
                        // not an entry in the list with this position already
                        if (newColorPoints.Where(x => x.Position == newPoint.Position).Count() == 0)
                        {
                            newPoint.Color = XYZ.FromRGB(Color.Black);
                            newColorPoints.Add(newPoint);
                        }
                    }
                    else
                    {
                        // it's the desired color. Find any others in the list that are in this position
                        // and black, remove them, and then replace it with this one
                        newColorPoints.RemoveAll(
                            x => x.Position == newPoint.Position && x.Color.ToRGB().ToArgb().ToArgb() == Color.Black.ToArgb());
                        newColorPoints.Add(newPoint);
                    }
                }
                colpoints = newColorPoints.ToArray();
            }
            //add color points
            for (int c = 0; c < colpoints.Length; c++)
            {
                // focus point; if filtered, non-standard focus points aren't supported.
                if (c > 0 && colpoints[c].Focus != .5 && filterColor == null)
                {
                    AddPosition(colpoints, alphapoints, positions,
                                colpoints[c - 1].Position + (colpoints[c].Position - colpoints[c - 1].Position) * colpoints[c].Focus);
                }
                //color
                AddPosition(colpoints, alphapoints, positions, colpoints[c].Position);
            }

            // We aren't using alpha points, and first/last points get added below

            ////add alpha points
            //for (int a = 0; a < alphapoints.Length; a++)
            //{
            //    if (a > 0 && alphapoints[a].Focus != .5)//focus
            //        AddPosition(colpoints, alphapoints, positions,
            //            alphapoints[a - 1].Position + (alphapoints[a].Position - alphapoints[a - 1].Position) * alphapoints[a].Focus);
            //    //alpha
            //    AddPosition(colpoints, alphapoints, positions, alphapoints[a].Position);
            //}

            //add first/last point
            if (positions.Count < 1)
            {
                positions.Add(0f, Color.Transparent);
            }
            if (!positions.ContainsKey(0f))
            {
                if (filterColor != null)
                {
                    float earliest = positions.Keys[0];
                    Color c        = Color.Black;
                    for (int i = 0; i < positions.Count && positions.Keys[i] == earliest; i++)
                    {
                        if (positions.Values[i].ToArgb() != Color.Black.ToArgb())
                        {
                            c = positions.Values[i];
                            break;
                        }
                    }
                    positions.Add(0f, c);
                }
                else
                {
                    positions.Add(0f, positions.Values[0]);
                }
            }

            if (positions.Count < 2)
            {
                Color c = positions.Values[0];
                if (filterColor != null && c.ToArgb() != ((Color)filterColor).ToArgb())
                {
                    c = Color.Black;
                }
                positions.Add(1f, c);
            }

            if (!positions.ContainsKey(1f))
            {
                if (filterColor != null)
                {
                    float latest = positions.Keys[positions.Count - 1];
                    Color c      = Color.Black;
                    for (int i = positions.Count - 1; i >= 0 && positions.Keys[i] == latest; i--)
                    {
                        if (positions.Values[i].ToArgb() != Color.Black.ToArgb())
                        {
                            c = positions.Values[i];
                            break;
                        }
                    }
                    positions.Add(1f, c);
                }
                else
                {
                    positions.Add(1f, positions.Values[positions.Count - 1]);
                }
            }

            ColorBlend ret = new ColorBlend();

            Color[] col = new Color[positions.Count];
            positions.Values.CopyTo(col, 0);
            ret.Colors = col;
            float[] pos = new float[positions.Count];
            positions.Keys.CopyTo(pos, 0);
            ret.Positions = pos;
            return(ret);
        }