GithubHelp home page GithubHelp logo

jiatln / color-art Goto Github PK

View Code? Open in Web Editor NEW
17.0 1.0 1.0 284 KB

A rust crate for working with colors and color spaces.

Home Page: https://color-art.netlify.app

License: MIT License

Rust 100.00%
color rust colors rust-library color-art colorspaces

color-art's Introduction


ColorArt - A rust crate for working with colors and color spaces.

Color Art

A rust crate for working with colors and color spaces.

github crates.io docs.rs


Documentation

See Color Art.

Usage

Add Dependency

[dependencies]
color-art = "0.3"

Color generator

Create color from string

You can use the from_str method to construct a color from a string.

Currently supported color formats
  • rgb / rgba
  • hex
  • hsl / hsla
  • hsv
  • hsi
  • hwb
  • cmyk
  • xyz
  • yiq
  • yuv
  • YCbCr
  • lab
  • named color
For example
use color_art::Color;
use std::str::FromStr;

let color = Color::from_str("rgb(255, 255, 0)").unwrap();
let color = Color::from_str("rgba(255, 255, 0, 0.5)").unwrap();
let color = Color::from_str("#ffff00").unwrap();
let color = Color::from_str("hsl(60, 100%, 50%)").unwrap();
let color = Color::from_str("hsla(60, 100%, 50%, 0.6)").unwrap();
let color = Color::from_str("hsv(60, 100%, 100%)").unwrap();
let color = Color::from_str("hsi(60, 100%, 66.67%)").unwrap();
let color = Color::from_str("hwb(60, 0%, 0%)").unwrap();
let color = Color::from_str("cmyk(0%, 0%, 100%, 0%)").unwrap();
let color = Color::from_str("xyz(0.769975, 0.927808, 0.138526)").unwrap();
let color = Color::from_str("yiq(0.886, 0.32126, -0.31114)").unwrap();
let color = Color::from_str("yuv(0.886, -0.4359, 0.1)").unwrap();
let color = Color::from_str("YCbCr(225.93, 0.5755, 148.7269)").unwrap();
let color = Color::from_str("lab(97.14, -21.55, 94.48)").unwrap();
let color = Color::from_str("yellow").unwrap();

Create color from number

You can use the from_num method to construct a color from a number.

For example:

use color_art::Color;

let color = Color::from_num(16776960).unwrap();
let color = Color::from_num(0xffff00).unwrap();

Create color from name

You can use the from_name method to construct a color from a name.

For example:

use color_art::Color;

let color = Color::from_name("yellow").unwrap();

Create color from color space

You can use the from_<color_space> method to construct a color from a color space.

Currently supported color spaces:

  • rgb
  • rgba
  • hsl
  • hsv
  • cmyk
  • hex

More color spaces will be supported in the future.

For example:

use color_art::Color;

let color = Color::from_rgb(255, 255, 0).unwrap();
let color = Color::from_rgba(255, 255, 0, 0.5).unwrap();
let color = Color::from_hsl(60.0, 1.0, 0.5).unwrap();
let color = Color::from_hsv(60.0, 1.0, 1.0).unwrap();
let color = Color::from_cmyk(0.0, 0.0, 1.0, 0.0).unwrap();
let color = Color::from_hex("#ffff00").unwrap();

More examples can be found in Construct from color spaces.

Other color generator methods

  • random - Generate a random color.
  • mix - Mix two colors.
  • blend - Blend two colors with a blending mode.
  • average - Average a list of colors.

Color conversion

Stringify a color

Stringify a color to a string.

Refer to Construct from string.

Color Channels

Extract the color channels.

Refer to Color Channels.

Color Operations

Color operation functions.

Refer to Color Operations.


Made with ❤️ by JiatLn.

License

MIT License © 2022-Present JiatLn

color-art's People

Contributors

jiatln avatar lilyistrans avatar szabgab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

lilyistrans

color-art's Issues

The results of shade and tint is oposite

In describtion:
"0.0 is no white, 1.0 is all white" for tint
"0.0 is no black, 1.0 is all black" for shade
But when I want to add some black with 0.1 shade the color became all black. The same for tint.

color conversion to hsl format causes panic

Describe the bug
In case of color conversion to hsl, the saturation can be wrongly calculated to more than 1.0f64 and then cause panic on unwrap

To Reproduce

use color_art::Color;

let color = Color::from_rgba(
   41,
   121,
   255,
   1.0
); // color with saturation of 1.0

dbg!(&color);
/*
Color {
    rgb: [
        41.0,
        121.0,
        255.0,
    ],
    alpha: 1.0,
}
*/

color.darken(0.1); // this calls `Color::from_hsl(h, s, l).unwrap()`
// panics `ColorParserError("Saturation must be between 0.0 and 1.0, got 1.0000000000000002")`

Expected behavior
the call of darken should not panic and behave normaly

Version

  • color-art = "0.3.6"
  • rustc = "1.72.0" (5680fa18f 2023-08-23)

Take Alpha into account when doing Color Blending

Is your feature request related to a problem? Please describe.
When using any of the blend modes, alpha is ignored and set to 1.0 when the color is returned.

Describe the solution you'd like
It would be nice to be able to have the blend modes apply "less intensely" if the opacity of the source color is lower, i.e. when using Normal, if the source's opacity is 0, the backdrop's color would be unchanged.

After dwelling on this for a bit, I think an acceptable solution could be to use Color::mix_with on the source color, mixing with the backdrop color with the weight being based on the differences between their alpha channels (i.e. source alpha - backdrop alpha). I'm open to different ideas though!

Move to no_std?

Hi there,

Is there any chance of transitioning this to a no_std crate? I haven't scoured the sources enough to see whether or not this would be a trivial change, but I would imagine at least the core color structures and model conversion algorithms would be no_std-able at the very least.

Would be happy to put in a PR if it is given the green light.

Thanks!

color! marco support hsl space

use color! marco to create a hsl color like

let color = color!(hsl(30, 54%, 66%))

assert_eq!(color.hue(), 30.0);
assert_eq!(color.saturation(), 0.54);
assert_eq!(color.lightness(), 0.66);

I need help for this feature.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.