Files
hcie-rust-v3.05/hcie-color/README.md
T
2026-07-09 02:59:53 +03:00

2.3 KiB

hcie-color

A Rust library for color space conversions, gamma correction, and helpers.

Features

  • Gamma Correction: Encode and decode linear RGB values to/from sRGB using the standard gamma curve.
  • Color Space Conversions: Convert between RGB and HSL color spaces.
  • Clamping: Ensures RGB values are clamped to the valid range [0, 1] before encoding.

Usage

Add this to your Cargo.toml:

[dependencies]
hcie-color = "0.1.0"

Example

use hcie_color::{gamma_encode, gamma_decode, srgb_to_linear, linear_to_srgb, rgb_to_hsl, hsl_to_rgb};

// Gamma encoding
let linear = 0.5;
let encoded = gamma_encode(linear);
println!("Encoded: {}", encoded);

// Gamma decoding
let decoded = gamma_decode(encoded);
println!("Decoded: {}", decoded);

// sRGB to linear conversion
let srgb = [128, 64, 32];
let linear_rgb = srgb_to_linear(srgb);
println!("Linear RGB: {:?}", linear_rgb);

// Linear to sRGB conversion
let back_to_srgb = linear_to_srgb(linear_rgb[0], linear_rgb[1], linear_rgb[2]);
println!("Back to sRGB: {:?}", back_to_srgb);

// RGB to HSL conversion
let (h, s, l) = rgb_to_hsl(1.0, 0.0, 0.0);
println!("HSL: h={}, s={}, l={}", h, s, l);

// HSL to RGB conversion
let (r, g, b) = hsl_to_rgb(0.0, 1.0, 0.5);
println!("RGB: r={}, g={}, b={}", r, g, b);

API Reference

gamma_encode(linear: f32) -> f32

Encodes a linear RGB value to sRGB using the gamma curve.

gamma_decode(srgb: f32) -> f32

Decodes an sRGB value to linear RGB using the gamma curve.

srgb_to_linear(rgb: [u8; 3]) -> [f32; 3]

Converts an sRGB color (as u8 values 0-255) to linear RGB (as f32 values 0.0-1.0).

linear_to_srgb(r: f32, g: f32, b: f32) -> [u8; 3]

Converts linear RGB values (as f32 0.0-1.0) to sRGB (as u8 0-255). Values are clamped to [0, 1] before encoding.

rgb_to_hsl(r: f32, g: f32, b: f32) -> (f32, f32, f32)

Converts RGB values (as f32 0.0-1.0) to HSL (hue, saturation, lightness).

hsl_to_rgb(h: f32, s: f32, l: f32) -> (f32, f32, f32)

Converts HSL values (hue, saturation, lightness) to RGB (as f32 0.0-1.0).

Building and Testing

Build

cargo build

Run Tests

cargo test

Run Clippy (Linting)

cargo clippy

Format Code

cargo fmt

Check Formatting

cargo fmt --check

License

This project is licensed under the MIT License.