optic-color 0.0.1

Zero-dependency color library for Optic engine — RGBA, HSV, HSL, gradients, named constants
Documentation
  • Coverage
  • 34.86%
    38 out of 109 items documented9 out of 9 items with examples
  • Size
  • Source code size: 40.17 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.4 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 3s Average build duration of successful builds.
  • all releases: 3s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Kono-o/optic-engine
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Kono-o

Zero-dependency color library for the Optic engine.

This crate provides color types, conversions, channel arithmetic, and a gradient evaluator. It has no external dependencies and can be used independently of the rest of the engine.

Types

Type Components Channels Arithmetic
[RGBA] red, green, blue, alpha [ChannelArray<4>] Add, Sub, Mul, Div
[RGB] red, green, blue [ChannelArray<3>] Add, Sub, Mul, Div
[HSV] hue, saturation, value
[HSL] hue, saturation, lightness

HSV and HSL deliberately avoid arithmetic because hue wraparound makes componentwise operations produce wrong colors. Convert to [RGBA] first (via .into() or [ToRgba]), then operate, then convert back.

Conversions

Every color type implements [ToRgba] and [FromRgba], so you can use generics that accept "any color-like type":

use optic_color::*;

fn set_bg(color: impl ToRgba) {
    let rgba = color.to_rgba();
    // ...
}

Direct From impls exist for all pairs:

  • From<RGB/HSV/HSL> for RGBA
  • From<RGBA> for RGB/HSV/HSL

Gradients

[Gradient] supports multiple interpolation modes, color spaces, and wrap modes:

use optic_color::*;

let grad = Gradient::two_color(RED, BLUE)
    .set_color_space(GradientColorSpace::Hsv);

let mid = grad.sample(0.5); // RGBA

Named colors

The crate exports ~90 named [RGBA] constants (see [optic_color::constants]). Examples: [RED], [MIDNIGHT], [GOLD], [LAVENDER], [OBSIDIAN].