3 releases

0.0.3 Jun 17, 2021
0.0.2 Jun 17, 2021
0.0.1 Jun 16, 2021

#692 in Data structures

AGPL-3.0

83KB
1.5K SLoC

Color Operators

Color data structures, converters, comparators, and arithmetic operators

Byte size of Color Operators Open Issues Open Pull Requests Latest commits Build Status



Requirements

This repository requires Rust language/compiler to build from source

As of last update to this ReadMe file, the recommended method of installing Rust is via the installer script...

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Quick Start

This repository is a Rust library, define it as a dependency within a project Cargo.toml file...

Cargo.toml (snip)

[dependencies]
color-operators = "0.0.3"

Check Rust -- Doc -- Specifying Dependencies for details about defining dependencies.

Then include within a source file via use statement...

src/main.rs (snip)

extern crate color_operators;

use color_operators::hsl::HSL;
use color_operators::hsv::HSV;
use color_operators::rgb::RGB;

Usage

Create a project if one does not already exist...

cargo init hsl-to-rgb

cd hsl-to-rgb

Add this crate as a dependency...

Cargo.toml (snip)

[dependencies]
color-operators = "0.0.3"
argparse = "0.2.2"

Note, argparse is optional and only included to ensure following example functions as shown

Write code that utilizes this crate...

src/main.rs

extern crate argparse;
use argparse::{ ArgumentParser, StoreOption };


extern crate json;


extern crate color_operators;
use color_operators::rgb::RGB;
use color_operators::hsl::HSL;


fn main() {
    let mut red_argument: Option<u8> = None;
    let mut green_argument: Option<u8> = None;
    let mut blue_argument: Option<u8> = None;

    {
        let mut ap = ArgumentParser::new();
        ap.set_description("Convert RGB to HSL representation");

        ap.refer(&mut red_argument).add_option(
            &["--red", "-r"],
            StoreOption,
            "Red component"
        );

        ap.refer(&mut green_argument).add_option(
            &["--green", "-g"],
            StoreOption,
            "Green component"
        );

        ap.refer(&mut blue_argument).add_option(
            &["--blue", "-b"],
            StoreOption,
            "Blue component"
        );

        ap.parse_args_or_exit();
    }

    let red = red_argument.unwrap_or_default();
    let green = green_argument.unwrap_or_default();
    let blue = blue_argument.unwrap_or_default();

    let rgb = RGB::new(red, green, blue);
    let hsl = HSL::from(rgb);
    println!("{:?}", hsl);
}

Test conversion with various options...

cargo run -- -r 255
#> HSL { hue: 0.0, saturation: 1.0, lightness: 0.5 }

cargo run -- -g 255
#> HSL { hue: 120.0, saturation: 1.0, lightness: 0.5 }

cargo run -- -b 255
#> HSL { hue: 240.0, saturation: 1.0, lightness: 0.5 }

cargo run -- -r 42 -b 42
#> HSL { hue: 300.0, saturation: 1.0, lightness: 0.08235294117647059 }

Notes

This repository may not be feature complete and/or fully functional, Pull Requests that add features or fix bugs are certainly welcomed.


Warning color conversion is not guaranteed to be fully reversible! For example 25 + 25 may not result in 50 after conversions, where as 24 + 24 will usually equal 48... This seems to often be because of binary and decimal conversions and arithmetic.


Examples may be found within the examples/ directory and run to test conversions and other features;

cargo run --example rgb-to-hsl -- -r 255
#> HSL { hue: 0.0, saturation: 1.0, lightness: 0.5 }

cargo run --example rgb-to-hsv -- -g 255
#> HSV { hue: 120.0, saturation: 1.0, value: 1.0 }

For regular operations this project depends;

  • hex crate provides hexadecimal string to decimal parsing

  • json crate allows for string serialization and de-serialization

Examples depend on;

  • argparse carte provides command line argument parsing and type coercion

Lents depend on;

  • clippy crate throws errors and warnings for common code-smells

Contributing

Options for contributing to color-operators and rust-utilities


Forking

Start making a Fork of this repository to an account that you have write permissions for.

  • Add remote for fork URL. The URL syntax is git@github.com:<NAME>/<REPO>.git...
cd ~/git/hub/rust-utilities/color-operators

git remote add fork git@github.com:<NAME>/color-operators.git
  • Commit your changes and push to your fork, eg. to fix an issue...
cd ~/git/hub/rust-utilities/color-operators


git commit -F- <<'EOF'
:bug: Fixes #42 Issue


**Edits**


- `<SCRIPT-NAME>` script, fixes some bug reported in issue
EOF


git push fork main

Note, the -u option may be used to set fork as the default remote, eg. git push -u fork main however, this will also default the fork remote for pulling from too! Meaning that pulling updates from origin must be done explicitly, eg. git pull origin main

  • Then on GitHub submit a Pull Request through the Web-UI, the URL syntax is https://github.com/<NAME>/<REPO>/pull/new/<BRANCH>

Note; to decrease the chances of your Pull Request needing modifications before being accepted, please check the dot-github repository for detailed contributing guidelines.

Tip check for TODO lines for known arias that may cause issues, as well as portions of code that are excellent targets for new Pull Requests...

grep -C3 -ri 'todo' src/**/*.rs

Sponsor

Thanks for even considering it!

Via Liberapay you may sponsor__shields_io__liberapay on a repeating basis.

Regardless of if you're able to financially support projects such as color-operators that rust-utilities maintains, please consider sharing projects that are useful with others, because one of the goals of maintaining Open Source repositories is to provide value to the community.


Attribution


License

Color data structures, converters, comparators, and arithmetic operators
Copyright (C) 2021 S0AndS0

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, version 3 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

For further details review full length version of AGPL-3.0 License.

Dependencies

~165KB