Colors & Variants
Relevant source files
The following files were used as context for generating this wiki page:
The Colors & Variants system in SwiftDitherKit defines the visual identity and textural representation of chart data. Because the library relies on ordered dithering to render images, colors are not merely flat fills but are transformed into specific pixel patterns through a dither kernel.
This system provides a set of portable color definitions and texture "variants" that allow multiple data series to remain distinguishable even in a low-resolution, dithered environment. These elements are used across all chart types, including area, line, bar, pie, and radar charts.
Sources: README.md, Sources/SwiftDitherKit/DitherTypes.swift:1-10
Color System
The library uses a custom DitherColor structure to manage RGB components. This structure ensures that colors are portable across different rendering contexts within the dither engine.
DitherColor Specification
DitherColor stores red, green, and blue components as Double values clamped between 0.0 and 1.0. It supports initialization via explicit components or six-digit hex values.
Sources: Sources/SwiftDitherKit/DitherTypes.swift:8-33
| Property | Type | Description |
|---|---|---|
red | Double | Red component (0-1) |
green | Double | Green component (0-1) |
blue | Double | Blue component (0-1) |
Built-in Palette
SwiftDitherKit includes a standard seven-color palette derived from the original Dither Kit.
| Constant | Hex Value |
|---|---|
.green | 0x28D26E |
.blue | 0x358FF3 |
.purple | 0x966EFF |
.pink | 0xF05ABE |
.orange | 0xFF9632 |
.red | 0xF04646 |
.grey | 0x5C5C64 |
Sources: Sources/SwiftDitherKit/DitherTypes.swift:35-41
Color Processing Flow
Colors undergo a transformation from high-precision DitherColor to a 24-bit DitherRGB representation used by the pixel-painting kernel.
The quantization process rounds the double components multiplied by 255 to the nearest integer.
Sources: Sources/SwiftDitherKit/DitherTypes.swift:144-162, Tests/SwiftDitherKitTests/DitherValueAndAccessibilityTests.swift:8-14
Texture Variants
The DitherVariant enumeration defines how occupied cells are selected inside a chart shape. These variants provide visual contrast between series when they overlap or appear adjacent.
Supported Variants
| Variant | Logic / Visual Appearance |
|---|---|
gradient | Uses a standard Bayer matrix threshold for a smooth transition. |
dotted | Produces a sparse, dot-like occupancy pattern. |
hatched | Produces a diagonal line or hatched pattern. |
solid | Fills every pixel within the shape boundary. |
Sources: Sources/SwiftDitherKit/DitherTypes.swift:51-56, Tests/SwiftDitherKitTests/DitherRasterTests.swift:45-56
Implementation in the Kernel
Variants are applied during the paintColumn or polygon filling process. The kernel checks the variant type to determine if a specific pixel at coordinates (x, y) should be drawn based on the threshold provided by the Bayer matrix (the DitherKernel).
Sources: Tests/SwiftDitherKitTests/DitherRasterTests.swift:23-28, Tests/SwiftDitherKitTests/DitherRasterTests.swift:58-71
Visual Effects: Bloom
The DitherBloom system adds a post-processing glow to dithered shapes. This effect is achieved by creating a blurred, colorized copy of the low-resolution raster.
Bloom Configuration
Each bloom level is defined by a DitherBloomStyle containing blur radius, brightness, opacity, and saturation adjustments.
| Bloom Level | Blur Radius | Opacity | Description |
|---|---|---|---|
off | N/A | N/A | No glow effect. |
low | 3 | 0.70 | Subtle glow. |
high | 5 | 0.78 | Pronounced glow. |
aura | 15 | 0.10 | Wide, soft atmospheric glow. |
Sources: Sources/SwiftDitherKit/DitherTypes.swift:59-64, Sources/SwiftDitherKit/DitherTypes.swift:171-185
Integration with Data Structures
Colors and variants are assigned at the series or slice level. This allows for individual styling of different data categories.
Series and Slice Definition
// Sources/SwiftDitherKit/DitherTypes.swift:108-114
public struct DitherSeries: Identifiable, Hashable, Sendable {
public let id: String
public let label: String
public let color: DitherColor
public let variant: DitherVariant
}
// Sources/SwiftDitherKit/DitherTypes.swift:125-131
public struct DitherSlice: Identifiable, Hashable, Sendable {
public let id: String
public let label: String
public let value: Double
public let color: DitherColor
public let variant: DitherVariant
}
In DitherSeries (used for Area, Line, Bar, and Radar charts) and DitherSlice (used for Pie/Donut charts), the variant defaults to .gradient if not specified.
Sources: Sources/SwiftDitherKit/DitherTypes.swift:108-142
Legend Representation
The DitherLegend uses a DitherLegendSwatch to represent these colors and textures in the UI. The swatch itself is a miniature 4x4 dithered canvas that applies the DitherKernel thresholds to accurately preview the color's dithered appearance.
Sources: Sources/SwiftDitherKit/DitherChartChrome.swift:68-88
Summary
The Colors & Variants system enables a distinct visual language within SwiftDitherKit's pixel-based architecture. By combining a fixed palette of DitherColor presets with four DitherVariant textures and optional DitherBloom effects, the library ensures that data remains legible and aesthetically consistent with the "ordered dither" retro aesthetic. This architecture separates high-level color intent from the low-level pixel quantization performed by the rendering engine.