示例#1
0
        void LoadGradient2D()
        {
            if (Gradient2D)
            {
                return;
            }

            Gradient2D            = Helper.CreateTempTeture2D(8, 1, TextureFormat.RGB24, false, true, false);
            Gradient2D.filterMode = FilterMode.Point;
            Gradient2D.wrapMode   = TextureWrapMode.Repeat;

            for (short i = 0; i < 8; i++)
            {
                var R = (GRADIENT2[i * 2 + 0] + 1.0f) * 0.5f;
                var G = (GRADIENT2[i * 2 + 1] + 1.0f) * 0.5f;

                Gradient2D.SetPixel(i, 0, new Color(R, G, 0, 1));
            }

            Gradient2D.Apply();
        }
        /// <summary>Gets the image.</summary>
        public override ImageFormat GetImage(Css.RenderableData context, CssProperty property)
        {
            if (Image == null)
            {
                // Get first value (either a colour stop, angle or 'to x'):
                int firstColour = 1;
                Angle = LoadAngle(this[0]);

                if (Angle == float.MaxValue)
                {
                    // It's the first colour. Angle is 180deg:
                    Angle       = (float)Math.PI;
                    firstColour = 0;
                }

                // Load the gradient:
                Gradient2D grad = LoadGradient(firstColour, this, false);

                // Render the gradient:
                Color32[] rendered = grad.Render32(Resolution, true).Pixels;
                Color32[] pixels   = null;

                // Create the image now.
                Texture2D image;

                // If angle is perfectly vertical, or perfectly horizontal..
                if (Angle == 0f || Angle == (float)Math.PI)
                {
                    // Vertical:
                    image = new Texture2D(1, Resolution);

                    if (Angle == (float)Math.PI)
                    {
                        // Set the pixels as-is:
                        pixels = rendered;
                    }
                    else
                    {
                        // Pixels is a new set:
                        pixels = new Color32[Resolution];

                        // Backwards:
                        for (int i = 0; i < Resolution; i++)
                        {
                            pixels[i] = rendered[Resolution - 1 - i];
                        }
                    }
                }
                else if (Angle == (Fourty5Deg * 6f) || Angle == (Fourty5Deg * 2f))
                {
                    // Horizontal:
                    image = new Texture2D(Resolution, 1);

                    if (Angle == (Fourty5Deg * 6f))
                    {
                        // Set the pixels as-is:
                        pixels = rendered;
                    }
                    else
                    {
                        // Pixels is a new set:
                        pixels = new Color32[Resolution];

                        // Backwards:
                        for (int i = 0; i < Resolution; i++)
                        {
                            pixels[i] = rendered[Resolution - 1 - i];
                        }
                    }
                }
                else
                {
                    // Any other angle:
                    image = new Texture2D(Resolution, Resolution);

                    pixels = new Color32[Resolution * Resolution];

                    float cos = (float)System.Math.Cos(-Angle);
                    float sin = (float)System.Math.Sin(-Angle);

                    // Other angles ignored for now:
                    int   index  = 0;
                    float max    = (float)Resolution - 1f;
                    float delta  = 1f / (max - 1f);
                    float yPoint = 0.5f - delta;

                    for (int y = Resolution - 1; y >= 0; y--)
                    {
                        float xPoint = -0.5f;

                        for (int x = 0; x < Resolution; x++)
                        {
                            // int rotatedX=(int)( (xPoint * cos) - (yPoint * sin) );
                            int rotatedY = (int)(((xPoint * sin) + (yPoint * cos)) * max);

                            if (rotatedY < 0)
                            {
                                rotatedY = 0;
                            }
                            else if (rotatedY >= Resolution)
                            {
                                rotatedY = Resolution - 1;
                            }

                            pixels[index] = rendered[rotatedY];
                            xPoint       += delta;
                            index++;
                        }

                        yPoint -= delta;
                    }
                }

                // Set:
                image.SetPixels32(pixels);

                // Flush:
                image.Apply();

                // Apply the image:
                Image       = new SparkSpecialImageFormat();
                Image.Image = image;
            }

            return(Image);
        }
        /// <summary>Loads a gradient from a set of stops in the given parameters.</summary>
        public static Gradient2D LoadGradient(int firstColour, Css.ValueSet parameters, bool autoRepeat)
        {
            // Next load the stops.
            int paramCount = parameters.Count;
            int stopCount  = paramCount - firstColour;
            int lastColour = paramCount - 1;

            // Create the gradient:
            Gradient2D grad = new Gradient2D(stopCount);

            int index           = 0;
            int undefinedPoints = 0;

            for (int i = firstColour; i < paramCount; i++)
            {
                // Get the stop value:
                Css.Value stop = parameters[i];

                // It's either a set (colour and stop) or just a colour.
                Color colour;
                float stopValue;

                if (stop is Css.Units.ColourUnit)
                {
                    // Pull the colour:
                    colour = stop.GetColour();

                    if (i == lastColour)
                    {
                        stopValue = 1f;
                    }
                    else if (i == firstColour)
                    {
                        stopValue = 0f;
                    }
                    else
                    {
                        stopValue = float.MaxValue;
                    }
                }
                else
                {
                    // Pull the colour:
                    colour = stop[0].GetColour();

                    // And the stop %:
                    stopValue = stop[1].GetRawDecimal();
                }

                // Drop in the colour:
                grad.Colours[index] = colour;

                if (stopValue == float.MaxValue)
                {
                    // This point doesn't have a stop value defined.
                    // We'll have to load the following ones first and come back to it.
                    undefinedPoints++;
                }
                else
                {
                    // Define any undefined points now:
                    if (undefinedPoints != 0)
                    {
                        // First undefined point is..
                        int firstUndef = index - undefinedPoints;

                        // Get the previously defined position:
                        float lastDefined = grad.Positions[firstUndef - 1];

                        // Delta is therefore..
                        float delta = (stopValue - lastDefined) / (float)(undefinedPoints + 1);

                        // For each undefined point..
                        for (int undef = 0; undef < undefinedPoints; undef++)
                        {
                            // Bump up the defined amount:
                            lastDefined += delta;

                            // Set the position:
                            grad.Positions[firstUndef + undef] = lastDefined;
                        }

                        // Clear:
                        undefinedPoints = 0;
                    }

                    // Set the position:
                    grad.Positions[index] = stopValue;
                }

                index++;
            }

            return(grad);
        }
示例#4
0
 public Gradient(Gradient2D gradient, GraphNode transition)
 {
     Colours    = gradient;
     Transition = transition;
 }