Expand description
A no-std compatible library for fast color math, intended for use in programming
addressable LEDs.
Currently this library is geared toward use in embedded systems, but does contain useful APIs that are more generally useful.
-
Fast
u8andu16math — Cichlid includes functions for scaling, dimming, and brightening single and double byte values. Basic trigonometric functions (sine, cosine) are implemented as well. -
HSV and RGB support — Full control over each color is provided by the
HSVandColorRGBstructures. Different means of converting fromHSVtoColorRGBare also implemented. -
Axial (Two Point) Color Gradients — Create smooth transitions between any two colors for any number of steps.
-
Power Consumption Estimating — Estimating power requirements can be done with structs implementing the
PowerEstimatortrait.
This Library is still in its infancy, and as such there may be a lack of documentation and vigorous testing.
§Examples
General Color operations:
use cichlid::ColorRGB;
let red = ColorRGB::Red;
let blue = ColorRGB::Blue;
let mut purple = red + blue;
assert_eq!(purple, ColorRGB::new(255, 0, 255));
purple.scale(128); // Scale by half
assert_eq!(purple, ColorRGB::new(128, 0, 128));
purple *= 2; // Multiple all components by two
assert_eq!(purple, red + blue);Using HSV (Hue, Saturation, Value) and converting to ColorRGB:
use cichlid::{HSV, ColorRGB, prelude::*};
let red_hsv = HSV::new(0, 255, 255);
let red_rgb = ColorRGB::from(red_hsv);
assert_eq!(red_rgb, ColorRGB::Red);Creating a gradient is very easy, simply import the trait and call the method:
use cichlid::{HSV, ColorRGB, GradientDirection, prelude::*};
let mut colors = [ColorRGB::Black; 100];
let start = HSV::new(0, 255, 255);
let end = HSV::new(100, 255, 180);
colors.gradient_fill(start, end, GradientDirection::Longest);We can also create rainbows from both a step size, as well as a forming a complete rainbow.
use cichlid::{HSV, ColorRGB, GradientDirection, prelude::*};
let mut colors = [ColorRGB::Black; 256];
let start_hue: u8 = 0;
let hue_delta: u16 = (1 << 8);
colors.rainbow_fill(start_hue, hue_delta); // From step size
colors.rainbow_fill_single_cycle(start_hue); // Complete rainbow§no-std
To use in a no-std environment, simply add the following to your project’s cargo.toml:
[dependencies.cichlid]
version = "*"
features = ["no-std"]§Low memory usage
The low-mem feature creates a binary that is smaller due to relying less on in memory
tables, preferring direct computation instead. The only drawback of this is a slight
speed decrease.
§Nightly features
To use some unstable nightly features and optimizations, use the nightly feature flag.
§Acknowledgements
This library takes heavy inspiration and code-reuse from FastLED, an Arduino library for talking to addressable LEDs.
Modules§
- color_
codes - Raw Color codes.
- math
- Collection of math Traits and functions for manipulating integers.
- prelude
- Easy importing of integer and color auto traits.
Structs§
- ColorRGB
- Object representing a color through the standard single byte red, green, and blue values.
- Default
Power Estimator - Default estimator.
- HSV
- Represents a color encoded in
(hue, saturation, value)format.
Enums§
- Gradient
Direction - Possible Directions around the color wheel a gradient can go.
Traits§
- Power
Estimator - Trait for estimating the power consumption of a strand of
ColorRGBs.
Functions§
- hsv_
gradient - Creates a axial (two-color) gradient from the HSV values
startto (exclusive)end. - rgb_
gradient - Creates a two-color gradient from two RGB values.