#rgb #color #roguelike #hsv #gamedev #graphics

bracket-color

RGB and HSV color handling and utilities, including lerp and W3C named colors. Part of the bracket-lib family.

6 releases

0.8.7 Oct 4, 2022
0.8.2 Feb 10, 2021
0.8.1 Apr 29, 2020
0.7.0 Feb 22, 2020
0.1.0 Feb 21, 2020

#216 in Images

Download history 1167/week @ 2023-11-20 771/week @ 2023-11-27 794/week @ 2023-12-04 964/week @ 2023-12-11 1031/week @ 2023-12-18 692/week @ 2023-12-25 534/week @ 2024-01-01 969/week @ 2024-01-08 864/week @ 2024-01-15 985/week @ 2024-01-22 630/week @ 2024-01-29 918/week @ 2024-02-05 1005/week @ 2024-02-12 1234/week @ 2024-02-19 1090/week @ 2024-02-26 1166/week @ 2024-03-04

4,722 downloads per month
Used in 11 crates (5 directly)

MIT license

135KB
3.5K SLoC

bracket-color

This crate provides a color system for use in the bracket-terminal system. It is part of the overall bracket-lib system.

Using bracket-color

To obtain bracket-color, include the following in your Cargo.toml file:

[dependencies]
bracket-color = "~0.8"

RGB

The basic structure is RGB, which represents a color as red/green/blue components. You can construct a color in a number of ways:

  • new() provides a black (all zeroes) entry.
  • from_f32 takes three floats, from 0.0 to 1.0 as a starting point.
  • from_u8 takes there bytes, from 0 to 255.
  • named takes a tuple of floats, with a LOT of predefined colors available. (e.g. RGB::named(RED)).
  • from_hex takes an HTML/CSS style hex number and converts it (e.g. RGB::from_hex("#aabbcc"))).

You can also convert RGB structures:

  • Add, subtract, multiply and divide operations are supported both on a single float and against another RGB structure.
  • to_hsv makes a Hue-Saturation-Value color.
  • to_greyscale uses a standard grayscale operation to make a greyscale approximation of a color.
  • desaturate makes a better greyscale conversion by converting to HSV and lowering the saturation.
  • lerp lets you smoothly transition between two colors, in RGB space.

HSV

The HSV system provides color support in the HSV space. You can construct an HSV color as follows:

  • new() makes an all-zero HSV color.
  • from_f32 lets you specify HSV as floats.
  • RGB::to_hsv converts an RGB color into an HSV color.

You can also go back to RGB with to_rgb.

ColorPair

A ColorPair is simply a helper structure holding both a foreground and a background.

Exports

Everything is exported via the bracket_color::prelude namespace.

Feature Flags

  • If you enable the serde feature flag, the RGB, HSV and ColorPair structures are derived as Serde serializable/de-serializable.
  • The rex feature flag enables RexPaint support.
  • The palette feature flag enables a static (thread-safe) palette map, linking named colors to colors. It's empty by default (the add_named_colors_to_palette adds all of the constant named colors for you, in lower case).
  • If you enable the bevy feature, conversions between Bevy's Color type and the bracket-color types are enabled.

Examples

There are a few examples to help get you going. They use crossterm for terminal output. You may run the examples with cargo run --example <name>.

  • lerp is a simple color lerp.
  • lerpit is an iterator-based lerp.
  • lerpit_hsv is an HSV lerp.
  • named_colors demonstrates how to access named colors.
  • shades_of_grey demonstrates greyscale and desaturate functions.
  • colors demonstrates various ways to acquire colors.

Dependencies

~0–19MB
~226K SLoC