#css-color #color #css #converter #colour

css-colors

A Rust 转换器,用于转换 CSS 颜色

5 个版本 (2 个稳定版)

使用旧的 Rust 2015

1.0.1 2018 年 11 月 16 日
0.1.0 2018 年 9 月 11 日
0.0.2 2018 年 9 月 11 日
0.0.1 2018 年 8 月 3 日

#css-color 中排名第 5

Download history 45/week @ 2023-11-20 24/week @ 2023-11-27 21/week @ 2023-12-04 26/week @ 2023-12-11 18/week @ 2023-12-18 19/week @ 2023-12-25 23/week @ 2024-01-08 15/week @ 2024-01-15 13/week @ 2024-01-22 4/week @ 2024-01-29 59/week @ 2024-02-05 61/week @ 2024-02-12 38/week @ 2024-02-19 45/week @ 2024-02-26 64/week @ 2024-03-04

每月下载 218
6crate(5 个直接使用)使用

ISC 许可

72KB
1.5K SLoC

css-colors

Build Status css-colors

A Rust 转换器,用于转换 CSS 颜色。 🎨

安装

css_colors crate 添加到您的 Cargo.toml 依赖列表中

[dependencies]
css_colors = "1.0"

然后通过将 extern crate 声明添加到根目录来告诉您的程序使用该crate

extern crate css_colors;

使用

该crate的目标是让您轻松地在常见的 CSS 颜色表示之间转换和转换,以最直观的方式对颜色执行操作。 🌈

什么是 css_colors?

此crate允许您创建和转换不同的颜色模型。目前,它处理 RGB 颜色模型和 CSS3 中使用的 HSL 颜色模型之间的转换。

RGB 颜色模型在您想用一定量的红色、绿色和蓝色来表示颜色时非常有用。

background-color: rgb(255, 99, 71); // tomato

但是,也可以使用 HSL 颜色模型来表示相同的颜色,该模型指定了颜色的色调、饱和度和亮度。

background-color: hsl(9, 100%, 64%); // also tomato!

您还可以使用如 Less 这样的 CSS 预处理器以有趣的方式操作这些颜色。

$tomato: hsl(9, 100%, 64%); // equivalent to rgb(255, 99, 71)
$dark-tomato: darken($tomato, 20%); // hsl(9, 100%, 44%)
$desaturated-tomato: desaturate($tomato, 40%); // hsl(9, 60%, 64%)

此crate允许您执行映射到 Less 的 颜色操作 API 的操作。这些操作可以应用于 RGB 和 HSL 颜色模型。

示例

将颜色表示为有效的 CSS 字符串

use css_colors::{Color, rgb, hsla};

let salmon = rgb(250, 128, 114);
let chartreuse = hsla(90, 100, 50, 1.0);

assert_eq!(salmon.to_css(), "rgb(250, 128, 114)");
assert_eq!(chartreuse.to_css(), "hsla(90, 100%, 50%, 1.00)");

在不同颜色模型表示之间进行转换

use css_colors::{Color, rgb, rgba, hsl, hsla};

let chartreuse = rgb(127, 255, 0);

assert_eq!(chartreuse.to_hsl(), hsl(90, 100, 50));
assert_eq!(chartreuse.to_hsla(), hsla(90, 100, 50, 1.0));
assert_eq!(chartreuse.to_rgba(), rgba(127, 255, 0, 1.0));

操作单个颜色以创建新的颜色模型表示

use css_colors::{Color, hsl, percent};

let chartreuse = hsl(90, 100, 50);

assert_eq!(chartreuse.darken(percent(20)), hsl(90, 100, 30));
assert_eq!(chartreuse.desaturate(percent(20)), hsl(90, 80, 50));
assert_eq!(chartreuse.greyscale(), hsl(90, 0, 50));

操作多个颜色以创建新的颜色模型表示

use css_colors::{Color, rgb, rgba, hsl, hsla, percent};

let chartreuse = hsl(90, 100, 50);
let red = rgba(100, 0, 0, 1.0);

assert_eq!(
    chartreuse.mix(red, percent(50)).to_css(),
    "hsla(67, 98%, 25%, 1.00)"
);
assert_eq!(chartreuse.tint(percent(50)).to_css(), "hsl(90, 100%, 75%)");
assert_eq!(chartreuse.shade(percent(50)).to_css(), "hsl(90, 98%, 25%)");

查看 文档 了解可用的颜色操作!

以下链接可能在您使用此crate时有所帮助。

  1. CSS3 的 RGB 颜色模型
  2. CSS3 的 HSL 颜色模型
  3. Less 的 颜色操作函数

贡献

安装

  • gitclone<repository-url>
  • cdcss-colors
  • rustupupdate
  • cargo构建

代码风格检查 + 插件

请使用以下插件以确保在贡献此软件包时代码一致性。

构建 + 测试

  • rustup update – 更新到最新版本的 Rust
  • cargo build – 构建软件包
  • cargo test – 运行测试套件

我们在 Travis CI 上对 Rust 稳定版、beta 版和夜间版运行测试套件。

许可协议

本项目采用ISC 许可协议

无运行时依赖