#css-color #rgb #css

no-std color

A library for representing and manipulating colors

2 unstable releases

new 0.1.0 Nov 20, 2024
0.0.1 Nov 21, 2014

#3 in #css-color

Download history 4/week @ 2024-08-08 1/week @ 2024-08-15 1/week @ 2024-08-29 4/week @ 2024-09-19 5/week @ 2024-09-26 8/week @ 2024-10-03 17/week @ 2024-10-10 6/week @ 2024-10-17 2/week @ 2024-10-24 10/week @ 2024-10-31 6/week @ 2024-11-07 51/week @ 2024-11-14

72 downloads per month

Apache-2.0 OR MIT

195KB
3K SLoC

Color

A library for representing and manipulating colors

Linebender Zulip, #color channel dependency status MIT/Apache 2.0 Build status Crates.io Docs

Color is a Rust crate which implements color space conversions, targeting at least CSS Color Level 4.

Scope and goals

Color in its entirety is an extremely deep and complex topic. It is completely impractical for a single crate to meet all color needs. The goal of this one is to strike a balance, providing color capabilities while also keeping things simple and efficient.

The main purpose of this crate is to provide a good set of types for representing colors, along with conversions between them and basic manipulations, especially interpolation. A major inspiration is the CSS Color Level 4 draft spec; we implement most of the operations and strive for correctness.

A primary use case is rendering, including color conversions and methods for preparing gradients. The crate should also be suitable for document authoring and editing, as it contains methods for parsing and serializing colors with CSS Color 4 compatible syntax.

Simplifications include:

  • Always using f32 to represent component values.
  • Only handling 3-component color spaces (plus optional alpha).
  • Choosing a fixed, curated set of color spaces for dynamic color types.
  • Choosing linear sRGB as the central color space.
  • Keeping white point implicit.

A number of other tasks are out of scope for this crate:

  • Print color spaces (CMYK).
  • Spectral colors.
  • Color spaces with more than 3 components generally.
  • ICC color profiles.
  • ACES color transforms.
  • Appearance models and other color science not needed for rendering.
  • Quantizing and packing to lower bit depths.

The Rgba8 type is a partial exception to this last item, as that representation is ubiquitous and requires special logic for serializing to maximize compatibility.

Some of these capabilities may be added as other crates within the color repository, and we will also facilitate interoperability with other color crates in the Rust ecosystem as needed.

Main types

The crate has two approaches to representing color in the Rust type system: a set of types with static color space as part of the types, and DynamicColor in which the color space is represented at runtime.

The static color types come in three variants: OpaqueColor without an alpha channel, AlphaColor with a separate alpha channel, and PremulColor with premultiplied alpha. The last type is particularly useful for making interpolation and compositing more efficient. These have a marker type parameter, indicating which ColorSpace they are in. Conversion to another color space uses the convert method on each of these types. The static types are open-ended, as it's possible to implement this trait for new color spaces.

Features

  • std (enabled by default): Get floating point functions from the standard library (likely using your target's libc).
  • libm: Use floating point implementations from libm.
  • bytemuck: Implement traits from bytemuck on AlphaColor, OpaqueColor, PremulColor, and Rgba8.

At least one of std and libm is required; std overrides libm.

Minimum supported Rust Version (MSRV)

This version of Color has been verified to compile with Rust 1.82 and later.

Future versions of Color might increase the Rust version requirement. It will not be treated as a breaking change and as such can even happen with small patch releases.

Click here if compiling fails.

As time has passed, some of Color's dependencies could have released versions with a higher Rust requirement. If you encounter a compilation issue due to a dependency and don't want to upgrade your Rust toolchain, then you could downgrade the dependency.

# Use the problematic dependency's name and version
cargo update -p package_name --precise 0.1.1

Community

Linebender Zulip

Discussion of Color development happens in the Linebender Zulip, specifically the #color channel. All public content can be read without logging in.

License

Licensed under either of

at your option.

Contribution

Contributions are welcome by pull request. The Rust code of conduct applies. Please feel free to add your name to the AUTHORS file in any substantive pull request.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.

Dependencies

~135KB