Gradients

Creating gradients in Unity via code is often unintuitive and cumbersome. The Color Pro package includes a few helper methods in the static class Gradients to simplify the process of creating and working with gradients.


🍭 Creating Gradients

Methods for easily creating gradients.

// Create a gradient between two colors
Gradient gradient = Color.red.ToGradient(Color.blue);

// Interpolate a linear amount of stops
Gradient gradient = Color.red.ToGradient(Color.blue, 5);

// Pass an array of custom stops
float[] stops = new float[] { 0f, 0.1f, 0.3f, 0.6f, 1f };
Gradient gradient = Color.red.ToGradient(Color.blue, stops);

// Create a gradient from an array of colors
Gradient gradient = colors.ToGradient();

// Use params to create a gradient with multiple colors
Gradient gradient = Color.red.ToGradient(Color.blue, Color.green);

🌈 Extracting Colors

Methods for extracting colors from a gradient.

// Get the colors of the gradient
Color[] colors = gradient.ToColors();

// Interpolate a linear amount of stops within the gradient
Color[] colors = gradient.ToColors(5);

// Interpolate a set of custom stops
float[] stops = new float[] { 0f, 0.1f, 0.3f, 0.6f, 1f };
Color[] colors = gradient.ToColors(stops);

🎨 Color Palettes

Methods for creating gradient color palettes. This would be the same as extracting colors from a gradient but without needing to create a gradient first. Unity gradients are also limited to 8 colors whereas these methods can support any amount of colors.

// Interpolate 5 colors between red and blue
Color[] palette = Color.red.ToGradientPalette(Color.blue, 5);

// Interpolate a set of custom stops
float[] stops = new float[] { 0f, 0.1f, 0.3f, 0.6f, 1f };
Color[] palette = Color.red.ToGradientPalette(Color.blue, stops);

// Prevent heap allocations
Color.red.ToGradientPaletteNonAlloc(Color.blue, outputArray);
Color.red.ToGradientPaletteNonAlloc(Color.blue, stops, outputArray);