Implementation:Apache Druid ColorAssigner
| Knowledge Sources | |
|---|---|
| Domains | Web_Console, Data_Visualization |
| Last Updated | 2026-02-10 10:00 GMT |
Overview
ColorAssigner is a singleton class that deterministically assigns colors from a categorical palette to dimension values, ensuring consistent color mapping across visualizations.
Description
The class maintains a per-dimension registry of ColorAssigner instances, each tracking value-to-color assignments using a Joaat hash of the string representation. When a new value is encountered, it selects the least-used color from a 7-color categorical palette to maintain visual balance. Special values receive fixed colors: null maps to a dark gray, true to the first palette color, and false to the second. The palette and per-dimension registries are stored as static class members, functioning as application-wide singletons.
Usage
Used in the Druid web console's chart and visualization components to ensure that the same dimension value always receives the same color within a given dimension context, providing visual consistency across time-series charts, bar charts, and pie charts.
Code Reference
Source Location
- Repository: Apache Druid
- File: web-console/src/singletons/color-assigner.ts
- Lines: 1-93
Signature
export interface ColorPalette {
nullColor: string;
categorical: string[];
}
export class ColorAssigner {
static PALETTE: ColorPalette;
static byDimension: Map<string, ColorAssigner>;
static getColorForDimensionValue(dimensionName: string, value: any): string;
getColorFor(thing: any): string;
}
Import
import { ColorAssigner } from './singletons/color-assigner';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| dimensionName | string | Yes | The name of the dimension for which to assign colors (used as a namespace key) |
| value | any | Yes | The dimension value to assign a color to (null, boolean, string, or number) |
Outputs
| Name | Type | Description |
|---|---|---|
| color | string | A hex color string from the categorical palette assigned to the given value |
Usage Examples
Assigning colors to chart segments
const color = ColorAssigner.getColorForDimensionValue('browser', 'Chrome');
// Returns a consistent color like '#00BD84'
const nullColor = ColorAssigner.getColorForDimensionValue('browser', null);
// Returns '#303030' (the null color)