Code Examples
Practical implementation patterns and use cases for Chroma Palette.
Basic Integration
Simple Color Selection
using UnityEngine;
using XDPaint.ChromaPalette;
public class BasicColorSelection : MonoBehaviour
{
[SerializeField] private ColorPickerManager paletteManager;
[SerializeField] private Material targetMaterial;
void Start()
{
// Set initial color
paletteManager.SetColor(Color.white);
// Subscribe to color changes
paletteManager.OnColorChanged.AddListener(color =>
{
targetMaterial.color = color;
});
}
}
Mode Management
Dynamic Mode Switching
using UnityEngine;
using UnityEngine.UI;
using XDPaint.ChromaPalette;
using XDPaint.ChromaPalette.Core;
public class ModeSwitcher : MonoBehaviour
{
[SerializeField] private ColorPickerManager paletteManager;
[SerializeField] private Dropdown modeDropdown;
[SerializeField] private GameObject texturePanel;
[SerializeField] private GameObject palettePanel;
void Start()
{
// Populate dropdown
modeDropdown.options.Clear();
foreach (var mode in System.Enum.GetNames(typeof(ChromaPaletteMode)))
{
modeDropdown.options.Add(new Dropdown.OptionData(mode));
}
modeDropdown.onValueChanged.AddListener(OnModeChanged);
OnModeChanged(0);
}
void OnModeChanged(int index)
{
var mode = (ChromaPaletteMode)index;
paletteManager.SetMode(mode);
// Show/hide mode-specific UI
texturePanel.SetActive(mode == ChromaPaletteMode.Texture);
palettePanel.SetActive(mode == ChromaPaletteMode.Palette);
}
}
Palette Generation
Create Palette from Scene Colors
using UnityEngine;
using System.Linq;
using XDPaint.ChromaPalette;
using XDPaint.ChromaPalette.Core;
using XDPaint.ChromaPalette.ScriptableObjects;
public class ScenePaletteGenerator : MonoBehaviour
{
[SerializeField] private ColorPickerManager paletteManager;
[SerializeField] private ColorPalette paletteAsset; // Assign the same asset used by the manager
public void GeneratePaletteFromScene()
{
// Collect all materials in scene
var renderers = FindObjectsOfType<Renderer>();
var colors = renderers
.Where(r => r.material != null)
.Select(r => r.material.color)
.Distinct()
.Take(3)
.ToArray();
if (colors.Length == 0)
return;
// Update shared palette asset
paletteAsset.SetBaseColors(colors);
paletteAsset.SetPaletteType(PaletteType.Analogous);
paletteAsset.Settings.ColorCount = 8;
paletteAsset.GeneratePalette();
paletteManager.SetMode(ChromaPaletteMode.Palette);
}
}
Theme Profiles with Shared Palette Assets
Keep a single palette asset wired to ColorPickerManager and copy theme presets into it at runtime. This keeps the picker in sync without replacing references on the component.
using UnityEngine;
using XDPaint.ChromaPalette;
using XDPaint.ChromaPalette.Core;
using XDPaint.ChromaPalette.ScriptableObjects;
[System.Serializable]
public class Theme
{
public string name;
public ColorPalette sourcePalette;
public ChromaPaletteMode preferredMode = ChromaPaletteMode.Palette;
}
public class ThemeManager : MonoBehaviour
{
[SerializeField] private ColorPickerManager paletteManager;
[SerializeField] private ColorPalette runtimePalette; // Same asset assigned on the manager
[SerializeField] private Theme[] themes;
public void ApplyTheme(int index)
{
if (index < 0 || index >= themes.Length)
return;
var theme = themes[index];
if (theme.sourcePalette == null)
return;
runtimePalette.SetBaseColors(theme.sourcePalette.BaseColors);
runtimePalette.SetPaletteType(theme.sourcePalette.Type);
runtimePalette.Settings.ColorCount = theme.sourcePalette.Settings.ColorCount;
runtimePalette.Settings.Layout = theme.sourcePalette.Settings.Layout;
runtimePalette.GeneratePalette();
paletteManager.SetMode(theme.preferredMode);
}
}
Generate Palette from Texture Samples
Sample pixels from any texture (screenshots, concept art, thumbnails) and push the results into the shared palette asset.
using System.Collections.Generic;
using UnityEngine;
using XDPaint.ChromaPalette;
using XDPaint.ChromaPalette.Core;
using XDPaint.ChromaPalette.ScriptableObjects;
public class TexturePaletteGenerator : MonoBehaviour
{
[SerializeField] private ColorPickerManager paletteManager;
[SerializeField] private ColorPalette runtimePalette; // Asset referenced by the manager
public void ApplyTexture(Texture2D texture, int swatches = 6)
{
if (texture == null || runtimePalette == null)
return;
runtimePalette.SetBaseColors(ExtractSampledColors(texture, swatches));
runtimePalette.SetPaletteType(PaletteType.Analogous);
runtimePalette.Settings.ColorCount = swatches;
runtimePalette.GeneratePalette();
paletteManager.SetMode(ChromaPaletteMode.Palette);
}
Color[] ExtractSampledColors(Texture2D texture, int count)
{
var pixels = texture.GetPixels32();
int step = Mathf.Max(1, pixels.Length / Mathf.Max(1, count));
var result = new List<Color>(count);
for (int i = 0; i < pixels.Length && result.Count < count; i += step)
{
var color = (Color)pixels[i];
if (!result.Contains(color))
{
result.Add(color);
}
}
return result.ToArray();
}
}
Palette Workflows
Create Palette in the Inspector
- Right-click in the Project window.
- Choose
Create → XDPaint → Color Palette. - Adjust base colors, harmony type, and settings in the Inspector.
- The generated texture updates automatically as you tweak values.
Create Palette at Runtime
using XDPaint.ChromaPalette;
using XDPaint.ChromaPalette.Core;
using XDPaint.ChromaPalette.ScriptableObjects;
ColorPalette CreateRuntimePalette()
{
var palette = ScriptableObject.CreateInstance<ColorPalette>();
palette.SetBaseColors(Color.cyan, Color.magenta);
palette.SetPaletteType(PaletteType.Triadic);
palette.Settings.ColorCount = 12;
palette.Settings.Layout = TextureLayout.Grid;
palette.Settings.SaturationVariation = 0.3f;
palette.GeneratePalette();
return palette;
}
Quick Harmony Switches
palette.SetPaletteType(PaletteType.Complementary); // Base color + opposite
palette.SetPaletteType(PaletteType.Triadic); // Three colors spaced 120°
palette.SetPaletteType(PaletteType.Tetradic); // Four colors spaced 90°
palette.SetPaletteType(PaletteType.Analogous); // Neighbouring hues (±30°)
palette.SetPaletteType(PaletteType.SplitComplementary); // Base + two near complement
palette.SetPaletteType(PaletteType.Monochromatic); // Single hue variations
palette.SetPaletteType(PaletteType.Gradient); // Smooth ramp between base colors
palette.SetPaletteType(PaletteType.Custom); // Uses BaseColors directly
palette.Settings.HueShift = 15f; // Adjust spacing for analogous/split
palette.Settings.SaturationVariation = 0.5f; // Control saturation spread
palette.Settings.LightnessVariation = 0.4f; // Control value spread
Texture Sampling
Load and Sample Brand Colors
using UnityEngine;
using XDPaint.ChromaPalette;
public class BrandColorManager : MonoBehaviour
{
[SerializeField] private ColorPickerManager paletteManager;
[SerializeField] private string brandTexturePath = "Textures/BrandColors";
void Start()
{
LoadBrandColors();
}
void LoadBrandColors()
{
var brandTexture = Resources.Load<Texture2D>(brandTexturePath);
if (brandTexture != null)
{
// Apply texture to the picker and switch to Texture mode
paletteManager.SetPaletteTexture(brandTexture);
// Configure additional sampling options in the inspector (Texture Mode Settings)
}
}
}
Screenshot Color Sampling
using System.Collections;
using UnityEngine;
using XDPaint.ChromaPalette;
public class ScreenshotSampler : MonoBehaviour
{
[SerializeField] private ColorPickerManager paletteManager;
public void CaptureAndSampleScreen()
{
StartCoroutine(CaptureScreenshot());
}
IEnumerator CaptureScreenshot()
{
yield return new WaitForEndOfFrame();
var texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();
var scaledTexture = ScaleTexture(texture, 256, 256);
Destroy(texture);
paletteManager.SetPaletteTexture(scaledTexture);
}
Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight)
{
var result = new Texture2D(targetWidth, targetHeight, source.format, false);
var pixels = new Color[targetWidth * targetHeight];
for (int y = 0; y < targetHeight; y++)
{
for (int x = 0; x < targetWidth; x++)
{
float u = x / (float)targetWidth;
float v = y / (float)targetHeight;
pixels[y * targetWidth + x] = source.GetPixelBilinear(u, v);
}
}
result.SetPixels(pixels);
result.Apply();
return result;
}
}
Persistence
Save and Load Color Preferences
using UnityEngine;
using XDPaint.ChromaPalette;
using XDPaint.ChromaPalette.Core;
public class ColorPreferences : MonoBehaviour
{
[SerializeField] private ColorPickerManager paletteManager;
private const string COLOR_KEY = "SavedColor";
private const string MODE_KEY = "SavedMode";
private ChromaPaletteMode currentMode = ChromaPaletteMode.Rectangle;
void Awake()
{
LoadPreferences();
paletteManager.OnColorChanged.AddListener(_ => SaveColor());
}
public void SetPickerMode(ChromaPaletteMode mode)
{
currentMode = mode;
paletteManager.SetMode(currentMode);
PlayerPrefs.SetInt(MODE_KEY, (int)currentMode);
PlayerPrefs.Save();
}
void SaveColor()
{
string hex = ColorUtility.ToHtmlStringRGBA(paletteManager.CurrentColor);
PlayerPrefs.SetString(COLOR_KEY, hex);
PlayerPrefs.Save();
}
void LoadPreferences()
{
if (PlayerPrefs.HasKey(COLOR_KEY) &&
ColorUtility.TryParseHtmlString("#" + PlayerPrefs.GetString(COLOR_KEY), out Color color))
{
paletteManager.SetColor(color, false);
}
if (PlayerPrefs.HasKey(MODE_KEY))
{
currentMode = (ChromaPaletteMode)PlayerPrefs.GetInt(MODE_KEY);
paletteManager.SetMode(currentMode);
}
}
}