Scripting Support (C#)

advanced scripting

Color Studio 10 · Scripting Support (C#)

Namespace: ColorStudio. This page documents the main runtime classes: CSPalette (color palette), Recolor (palette applicator component), and ColorConversion (color-space utilities).
Tip: Check the demo scene scripts for runtime usage examples.

Getting Started

Add using ColorStudio; at the top of your script. The main classes are CSPalette (color palette) and Recolor (component that applies palettes to renderers).

using ColorStudio;

// Create a palette and apply it
CSPalette palette = ScriptableObject.CreateInstance<CSPalette>();
palette.ConfigurePalette(ColorScheme.SplitComplementary, Color.red);
palette.BuildHueColors();

Recolor recolor = GetComponent<Recolor>();
recolor.palette = palette;
recolor.Refresh();

CSPalette (ScriptableObject)

A ScriptableObject that defines a color palette with a color scheme, hue colors, shade variations, and custom colors. Create palettes in the Palette Manager window or at runtime via scripting.

Properties

ColorScheme scheme

Color harmony scheme (e.g. Complementary, Triadic, Custom, etc.).

Color primaryColor { get; }

The primary color of the palette.

Color complementary1Color { get; }

First complementary color derived from the scheme.

Color complementary2Color { get; }

Second complementary color (for Triadic, Tetradic, and similar schemes).

Color complementary3Color { get; }

Third complementary color (for Tetradic and Square schemes).

int hueCount

Number of hue colors in the palette. Default: 5.

int shades

Number of shade variations per hue. Default: 3.

float saturation

Color saturation (0–1). Default: 1.

float lightness

Base lightness value (0–1). Default: 0.5.

float minBrightness

Minimum brightness for shade generation. Default: 0.

float maxBrightness

Maximum brightness for shade generation. Default: 1.

int kelvin

Color temperature in Kelvin (1000–40000). Default: 4000.

float colorTempStrength

Strength of the color temperature effect (0–1). Default: 0.

float splitAmount

Split amount for SplitComplementary scheme (0–1). Default: 0.6.

int customColorsCount { get; }

Number of visible custom colors in the palette.

Color[] colors

Array of hue colors (with temperature applied). Populated after calling BuildHueColors().

int colorsCount

Number of hue colors in the colors array.

KeyColor[] keyColors

Core palette data array containing all key colors and their settings. Each KeyColor defines a hue angle, type, and enabled state.

float primaryAngle { get; }

Angle of the primary key color (read-only).

float primaryHue { get; }

Hue of the primary key color (read-only).

float primarySaturation { get; }

Saturation of the palette (read-only).

float primaryLightness { get; }

Lightness of the palette (read-only).

field keyColors KeyColor[]

Core palette data array containing all key colors and their configuration (hue, angle, range, etc.).

property primaryAngle float — read-only

Angle of the primary key color on the color wheel.

property primaryHue float — read-only

Hue value of the primary key color (0-1).

property primarySaturation float — read-only

Saturation value of the palette.

property primaryLightness float — read-only

Lightness value of the palette.

Methods

static CSPalette CreateEmptyPalette()

Create a new empty palette with no colors.

void ConfigurePalette(ColorScheme scheme, Color primaryColor, float splitAmount = 0.6f)

Configure the palette with a color harmony scheme and primary color.

void BuildHueColors()

Build the hue colors based on the current scheme. Call after changing palette settings.

Color[] BuildPaletteColors()

Build the complete palette including all hues and shade variations using OKLCH-based shade generation. Returns the full color array.

void Clear()

Clear all key colors from the palette.

void SetPrimaryAngle(float angle)

Set the primary hue angle in radians.

void SetKeyColor(int index, KeyColorType keyColorType, float angle)

Set a key color by index, type, and angle (in radians).

void Load(CSPalette otherPalette)

Copy all settings from another palette into this one.

void DeleteKeyColor(int index)

Delete the key color at the given index.

Color GetNearestColor(Color color, ColorMatchMode colorMatchMode, bool interpolate)

Find the nearest color in the palette to the input color.

colorMatchMode
Matching algorithm: RGB, HSL, OKLCH, OKLab, or individual components.
interpolate
Interpolate luminance during matching for smoother results.
Color GetNearestColor(Color color, ColorMatchMode colorMatchMode, bool interpolate, bool enablePerColorOperations, float threshold, ColorEntry[] colorOperations, bool enableColorAdjustments, ColorAdjustments colorAdjustments)

Find the nearest color with per-color operations and color adjustments applied.

Color[] GetNearestColors(Color[] originalColors)

Find nearest palette colors for an array of input colors. Uses multi-threaded processing.

Color[] GetNearestColors(Texture2D tex, ColorMatchMode colorMatchMode, bool interpolate, bool enablePerColorOperations, float threshold, ColorEntry[] colorOperations, bool enableColorAdjustments, ColorAdjustments colorAdjustments, Texture2D mask = null)

Find nearest palette colors for all pixels in a texture. Supports per-color operations, color adjustments, and an optional mask. Uses multi-threaded processing.

Texture2D GetNearestTexture(Texture2D tex, ColorMatchMode colorMatchMode, bool interpolate, Texture2D mask = null)

Return a recolored copy of the input texture using this palette.

Texture2D GetNearestTexture(Texture2D tex, ColorMatchMode colorMatchMode, bool interpolate, bool enablePerColorOperations, float threshold, ColorEntry[] colorOperations, bool enableColorAdjustments, ColorAdjustments colorAdjustments, Texture2D mask = null)

Return a recolored copy of the input texture with full options (per-color operations, color adjustments, mask).

Texture2D ExportLUT()

Export the palette as a 1024×32 LUT texture using OKLab perceptual distance for color mapping.

Texture2D ExportTexture()

Export the palette as a compact texture image.

void SortCustomColors(ColorSortingCriteria sortChoice)

Sort custom colors by the given criteria (e.g. ByHue, ByLuminance, BySaturation, ByOKLCHLightness).

Recolor (Component)

A MonoBehavior component you attach to any GameObject with a Renderer to recolor it at runtime using a CSPalette. Original textures and materials are not modified — they are instantiated before receiving changes. Disabling or removing the component restores the originals.

Properties

CSPalette palette

The color palette to apply to this renderer.

bool applyPalette

Whether the palette is applied. Default: true.

RecolorMode mode

What to recolor: MainColorOnly, Texture, MainColorAndTexture, or VertexColors.

Texture2D mask

Optional mask texture. Only pixels where the mask is non-black will be recolored.

int materialIndex

Index of the material to recolor when the renderer has multiple materials.

ColorMatchMode colorMatch

Color matching algorithm: RGB, HSL, OKLCH, OKLab, or individual channels.

bool interpolate

Interpolate luminance during color matching for smoother results. Default: true.

float threshold

Color matching threshold for per-color operations (0–1).

bool enablePerColorOperations

Enable per-color replacement rules via colorOperations.

ColorEntry[] colorOperations { get; set; }

Per-color replacement rules. Each entry specifies a source color, an operation (Preserve or Replace), and a replacement color.

bool enableColorAdjustments

Enable post-processing color adjustments (brightness, contrast, vibrance, tint, LUT).

ColorAdjustments colorAdjustments

Color adjustment settings: brightness, contrast, vibrance, tintAmount, tintColor, applyLUT, LUT.

bool enableOptimization

Enable LUT-based optimization for faster runtime recoloring.

Texture2D optimizedLUT

The optimized LUT texture generated by Optimize().

Methods

void Refresh(bool updateOptimization = false)

Refresh the recoloring with current settings. Set updateOptimization to true to regenerate the optimized LUT.

void Optimize(CSPalette palette)

Generate an optimized LUT for fast real-time recoloring. Recommended for production use.

void ReleaseOptimizationLUT()

Release the optimization LUT texture from memory.

void Bake(bool saveToExternalFiles)

Permanently bake the recolored result into the geometry/texture, then destroy the Recolor component.

Material GetOriginalMaterial()

Get the original material before recoloring was applied.

Texture2D GetOriginalTexture()

Get the original texture before recoloring was applied.

Mesh GetOriginalMesh()

Get the original mesh before vertex color recoloring was applied.

List<Color> GetOriginalUniqueColors()

Get the unique colors from the original texture.

List<Color> GetOriginalTexturePrincipalColors()

Get the unique colors at UV-mapped coordinates — the principal colors actually used by the mesh.

List<Color> GetOriginalVertexColors()

Get the unique vertex colors from the original mesh.

bool GetOriginalMainColor(out Color color)

Get the original main color from the material (_Color, _BaseColor, or _MainColor property). Returns true if a main color was found.

ColorConversion (Static Utility)

Static class with methods for color-space conversions and perceptual distance calculations. Useful when working with HSL, OKLab, or OKLch color spaces in your scripts.

HSL Conversions

static Color GetColorFromHSL(float H, float S, float L)

Convert HSL values (0–1 each) to an RGB Color.

static HSLColor GetHSLFromRGB(Color color)

Convert an RGB Color to an HSLColor (with .h, .s, .l fields).

static float GetHue(Color color)

Get the hue component (0–1) of a color.

static float GetSaturation(float r, float g, float b)

Get the saturation component (0–1) from RGB values.

static float GetLightness(float r, float g, float b)

Get the lightness component (0–1) from RGB values.

OKLab / OKLch Conversions

static OKLabColor RGBToOKLab(Color color)

Convert an RGB Color to OKLab color space (with .L, .a, .b fields).

static Color OKLabToRGB(OKLabColor lab)

Convert from OKLab color space back to RGB.

static OKLchColor GetOKLchFromRGB(Color color)

Convert an RGB Color to OKLch (perceptual lightness, chroma, hue). Fields: .L, .C, .h.

static Color GetRGBFromOKLch(OKLchColor lch)

Convert from OKLch back to RGB, with sRGB gamut clamping.

static OKLchColor OKLabToOKLch(OKLabColor lab)

Convert an OKLab color to OKLch (lightness, chroma, hue) representation.

static OKLabColor OKLchToOKLab(OKLchColor lch)

Convert an OKLch color to OKLab (lightness, a, b) representation.

static method OKLabToOKLch OKLchColor (OKLabColor lab)

Converts an OKLab color to OKLch (cylindrical) representation.

static method OKLchToOKLab OKLabColor (OKLchColor lch)

Converts an OKLch color back to OKLab representation.

static method LinearFromSRGB float (float x)

Converts an sRGB gamma-encoded value to linear color space.

static method SRGBFromLinear float (float x)

Converts a linear color space value to sRGB gamma-encoded.

Color Distance

static float OKLabDistance(Color a, Color b)

Calculate the perceptual distance between two colors in OKLab space (Euclidean).

static float OKLabDistanceSqr(Color a, Color b)

Squared perceptual distance (faster for comparisons, avoids square root).

Utilities

static float LinearFromSRGB(float x)

Convert an sRGB gamma-encoded value to linear. Applies the inverse sRGB transfer function.

static float SRGBFromLinear(float x)

Convert a linear value to sRGB gamma-encoded. Applies the sRGB transfer function.

static bool GetColorFromHex(string hex, out Color color)

Parse a hex color string (e.g. "#FF8800") to a Color. Returns true on success.

static bool IsInSRGBGamut(OKLabColor lab)

Check whether an OKLab color falls within the sRGB gamut.

static Color GamutClampOKLch(OKLchColor lch)

Clamp an OKLch color to fit within the sRGB gamut by reducing chroma.

Extension Methods

float color.GetLuma()

Get the perceptual luminance of a Color (ITU-R BT.709 standard).

void color.ApplyTemperature(int kelvin, float strength)

Apply a correlated color temperature adjustment to a Color.

Code Examples

Create and Apply a Palette

using ColorStudio;

// Create a triadic palette from blue
CSPalette palette = ScriptableObject.CreateInstance<CSPalette>();
palette.ConfigurePalette(ColorScheme.Triadic, Color.blue);
palette.shades = 4;
palette.BuildHueColors();

// Apply to a renderer
Recolor recolor = GetComponent<Recolor>();
recolor.palette = palette;
recolor.mode = RecolorMode.Texture;
recolor.colorMatch = ColorMatchMode.OKLCH;
recolor.Refresh();

LUT Optimization for Performance

using ColorStudio;

// Generate an optimized LUT for faster runtime recoloring
Recolor recolor = GetComponent<Recolor>();
recolor.enableOptimization = true;
recolor.Optimize(recolor.palette);
recolor.Refresh();

Per-Color Replacement

using ColorStudio;

// Replace specific colors: turn reds into greens
Recolor recolor = GetComponent<Recolor>();
recolor.enablePerColorOperations = true;
recolor.colorOperations = new ColorEntry[] {
    new ColorEntry {
        color = Color.red,
        operation = ColorOperation.Replace,
        replaceColor = Color.green
    }
};
recolor.threshold = 0.15f;
recolor.Refresh();

Color-Space Conversion

using ColorStudio;

// Convert between color spaces
OKLchColor lch = ColorConversion.GetOKLchFromRGB(Color.red);
Debug.Log($"Red in OKLch: L={lch.L:F2}, C={lch.C:F2}, h={lch.h:F2}");

// Measure perceptual distance between two colors
float dist = ColorConversion.OKLabDistance(Color.red, Color.blue);
Debug.Log($"Perceptual distance: {dist:F3}");

// Apply color temperature
Color warm = Color.white;
warm.ApplyTemperature(3000, 0.5f); // warm white
Was this page helpful?