Skip to content

Game.UI.Widgets.ColorGradient

Assembly:
Namespace: Game.UI.Widgets

Type: struct

Base: System.ValueType, Colossal.UI.Binding.IJsonWritable

Summary:
ColorGradient is a lightweight, JSON-writable wrapper around an array of GradientStop entries that represents a color gradient. It is primarily used to serialize/deserialze gradient data for UI widgets and to convert from UnityEngine.Gradient into a simple, serializable representation. Note that this struct exposes a mutable public field and is implemented as a value type (struct).


Fields

  • public GradientStop[] stops
    An array of GradientStop entries describing the gradient. Each GradientStop typically contains a time (position 0..1) and a Color. This field may be null. When written with Write(IJsonWriter), a null stops array is serialized as an empty array (length 0).

Properties

  • (none)

Constructors

  • public ColorGradient(GradientStop[] stops)
    Creates a ColorGradient with the provided array of GradientStop. The caller retains ownership of the array reference (no defensive copy is made), so the struct remains mutable.

Methods

  • public static explicit operator ColorGradient(UnityEngine.Gradient gradient)
    Converts a UnityEngine.Gradient into a ColorGradient. The conversion iterates the gradient.colorKeys[] and constructs a GradientStop for each color key using the key's time and color. Note: alpha keys (gradient.alphaKeys) are not considered — alpha information will only be preserved if present in the color values of the color keys.

Behavior summary: - If the source Gradient has N colorKeys, the resulting ColorGradient.stops will have length N. - The ordering of stops follows the order of gradient.colorKeys.

  • public void Write(Colossal.UI.Binding.IJsonWriter writer)
    Implements IJsonWritable. Serializes the ColorGradient to an IJsonWriter. The method:
  • Begins a type block using the struct's runtime full type name (writer.TypeBegin(GetType().FullName)).
  • Writes a property named "stops".
  • Writes the stops array length (0 if stops is null).
  • Iterates and writes each GradientStop via writer.Write(stops[i]).
  • Ends the array and the type block.

Caveats: - The Write method depends on the writer being able to serialize GradientStop via writer.Write(GradientStop). - Null stops arrays are handled by writing an array of length 0 (no elements).

Usage Example

// Convert a Unity gradient into the serializable wrapper, then write it with an IJsonWriter.
UnityEngine.Gradient unityGradient = new UnityEngine.Gradient();
// configure gradient.colorKeys / gradient.alphaKeys as needed...

// Explicit conversion from Gradient to ColorGradient
ColorGradient cg = (ColorGradient)unityGradient;

// Serialize
IJsonWriter writer = /* obtain or implement an IJsonWriter */;
cg.Write(writer);

{{ Additional notes: - This struct is useful when you need to persist gradient configurations for UI widgets or export them to JSON. - Because it only uses Gradient.colorKeys, if you rely on detailed alpha key timing (separate from color channels), you will lose explicit alpha-key timing information. - The struct is mutable and contains a public array reference — be careful about unintended modifications if you share the same array across instances. }}