Color Utilities =============== Simple color handling for RGB LEDs and displays. The Color class provides: - RGB color representation (0.0 to 1.0 floats) - Preset colors (RED, GREEN, BLUE, WHITE, PURPLE, CYAN, GOLD, OFF) - 8-bit RGB values (0-255) - Color blending and scaling - Arithmetic operations (+, *) **Usage:** ```nim import nimphea_color # Create from preset var red = Color() red.init(COLOR_RED) # Create from RGB values (0.0 to 1.0) var purple = Color() purple.init(0.5, 0.0, 0.5) # Direct construction var cyan = createColor(0.0, 1.0, 1.0) # Get values let r = red.red() # 0.0 to 1.0 let r8 = red.red8() # 0 to 255 # Modify purple.setRed(0.8) # Blend two colors var blended = colorBlend(red, purple, 0.5) # 50% mix # Scale brightness var dimRed = red * 0.5 # 50% brightness # Add colors var combined = red + purple ```
Types
Color {.importcpp: "daisy::Color", header: "util/color.h", bycopy.} = object
-
RGB color representation
Stores RGB values as floats from 0.0 to 1.0
Source Edit PresetColor {.importcpp: "daisy::Color::PresetColor", header: "util/color.h".} = enum COLOR_RED = 0, ## Pure red (1.0, 0.0, 0.0) COLOR_GREEN, ## Pure green (0.0, 1.0, 0.0) COLOR_BLUE, ## Pure blue (0.0, 0.0, 1.0) COLOR_WHITE, ## White (1.0, 1.0, 1.0) COLOR_PURPLE, ## Purple (0.5, 0.0, 0.5) COLOR_CYAN, ## Cyan (0.0, 1.0, 1.0) COLOR_GOLD, ## Gold/yellow (1.0, 0.84, 0.0) COLOR_OFF ## Off/black (0.0, 0.0, 0.0)
- Preset color values Source Edit
Procs
proc `*`(this: Color; scale: cfloat): Color {.importcpp: "(# * #)", ...raises: [], tags: [], forbids: [].}
-
Scale color brightness
Parameters:
- scale - Scaling factor (typically 0.0 to 1.0, but can be > 1.0)
Returns: New scaled color
Example:
var bright = createColor(1.0, 0.0, 0.0) # Bright red var dim = bright * 0.5 # Dim red (50%) var veryDim = bright * 0.1 # Very dim (10%)
Source Edit proc `+`(this: Color; rhs: Color): Color {.importcpp: "(# + #)", ...raises: [], tags: [], forbids: [].}
-
Add two colors together (saturating at 1.0)
Parameters:
- rhs - Color to add
Returns: New combined color (components clamped to 1.0)
Example:
var red = createColor(1.0, 0.0, 0.0) var green = createColor(0.0, 1.0, 0.0) var yellow = red + green # (1.0, 1.0, 0.0)
Source Edit proc colorBlend(a, b: Color; amt: cfloat): Color {. importcpp: "daisy::Color::Blend(#, #, #)", ...raises: [], tags: [], forbids: [].}
-
Blend between two colors
Parameters:
- a - First color
- b - Second color
- amt - Blend amount (0.0 = all A, 1.0 = all B, 0.5 = 50/50 mix)
Returns: Blended color
Example:
var red = createColor(1.0, 0.0, 0.0) var blue = createColor(0.0, 0.0, 1.0) var purple = colorBlend(red, blue, 0.5) # 50/50 purple var reddish = colorBlend(red, blue, 0.25) # More red var blueish = colorBlend(red, blue, 0.75) # More blue # Animate between colors for i in 0..100: let t = i.float / 100.0 let color = colorBlend(red, blue, t) # Set LED to color
Source Edit proc createColor(): Color {.importcpp: "daisy::Color()", ...raises: [], tags: [], forbids: [].}
- Create a color initialized to black (0, 0, 0) Source Edit
proc createColor(r, g, b: cfloat): Color {.importcpp: "daisy::Color(#, #, #)", ...raises: [], tags: [], forbids: [].}
-
Create a color with specific RGB values
Parameters:
- r - Red component (0.0 to 1.0)
- g - Green component (0.0 to 1.0)
- b - Blue component (0.0 to 1.0)
Example:
let purple = createColor(0.5, 0.0, 0.5) let white = createColor(1.0, 1.0, 1.0)
Source Edit proc init(this: var Color; preset: PresetColor) {.importcpp: "#.Init(#)", ...raises: [], tags: [], forbids: [].}
-
Initialize color with a preset value
Parameters:
- preset - One of the PresetColor enum values
Example:
var color = createColor() color.init(COLOR_RED)
Source Edit proc init(this: var Color; r, g, b: cfloat) {.importcpp: "#.Init(#, #, #)", ...raises: [], tags: [], forbids: [].}
-
Initialize color with specific RGB values
Parameters:
- r - Red component (0.0 to 1.0)
- g - Green component (0.0 to 1.0)
- b - Blue component (0.0 to 1.0)
Example:
var color = createColor() color.init(0.8, 0.2, 0.5)
Source Edit