A fast and flexible command-line tool and Rust library for converting hex color codes to RGB values.
- π Blazing fast - Optimized with LTO and minimal allocations
- π― Multiple input formats - Support for
#FF0000,0xAABBCC,FFAABBCC - π Alpha channel support - Handle 8-digit hex codes with transparency
- π Multiple output formats - Standard, CSS, JSON, Hex, and Compact
- π₯οΈ Terminal color preview - See colors directly in your terminal
- β‘ Interactive mode - REPL-style interface for multiple conversions
- π Built-in benchmarking - Performance testing included
- π Library support - Use as a dependency in your Rust projects
git clone https://github.com/luhann/hextorgb.git
cd hextorgb
cargo install --path . --features clicargo install --git https://github.com/luhann/hextorgb --features cli[dependencies]
hextorgb = { git = "https://github.com/luhann/hextorgb" }
# No CLI dependencies included - zero external deps!# Different input formats
hextorgb "#FF0000" # RGB(255, 0, 0)
hextorgb "0x00FF00" # RGB(0, 255, 0)
hextorgb "0000FF" # RGB(0, 0, 255)
# With alpha channel
hextorgb "#FF0000AA" # RGB(255, 0, 0, 0.67)# CSS format
hextorgb "#FF0000" --format css # rgb(255, 0, 0)
# JSON format
hextorgb "#FF0000" --format json # {"r": 255, "g": 0, "b": 0}
# Hex format (normalize)
hextorgb "ff0000" --format hex # #FF0000
# Compact format
hextorgb "#FF0000" --format compact # 255,0,0hextorgb --interactiveπ¨ Hex to RGB Converter - Interactive Mode
Enter hex colors (type 'quit' to exit)
hex> #FF0000
RGB(255, 0, 0)
hex> 0x00FF00
RGB(0, 255, 0)
hex> quit
Goodbye! π
hextorgb "#FF0000" --preview # Shows color block in terminalhextorgb --benchmarkπ Running Performance Benchmark...
4000000 conversions in 263.808756ms
65ns per conversion
15162499 conversions/sec
hextorgb --helpAdd to your Cargo.toml:
[dependencies]
hextorgb = { git = "https://github.com/luhann/hextorgb" }
# Zero external dependencies for library usage!use hextorgb::{hextorgb, parse_hex};
fn main() {
// Simple conversion
let result = hextorgb("#FF0000");
println!("{}", result); // RGB(255, 0, 0)
// Parse hex to raw values
let (rgb, alpha) = parse_hex("#FF0000AA").unwrap();
println!("RGB: {:?}, Alpha: {:?}", rgb, alpha); // RGB: [255, 0, 0], Alpha: Some(170)
}use hextorgb::{RGB, parse_hex};
fn process_color(hex: &str) -> Result<RGB, String> {
let (rgb, alpha) = parse_hex(hex).map_err(|e| e.to_string())?;
Ok(RGB {
r: rgb[0],
g: rgb[1],
b: rgb[2],
a: alpha.map(|a| a as f64 / 255.0).unwrap_or(1.0),
})
}| Format | Example | Description |
|---|---|---|
| Hash prefix | #FF0000 |
Standard web format |
| Hex prefix | 0xFF0000 |
Programming style |
| Raw hex | FF0000 |
No prefix |
| With alpha | #FF0000AA |
8-digit with transparency |
| Case insensitive | #ff0000 |
Lowercase works too |
| Format | Example | Use Case |
|---|---|---|
| Standard | RGB(255, 0, 0) |
Human readable |
| CSS | rgb(255, 0, 0) |
Web development |
| JSON | {"r": 255, "g": 0, "b": 0} |
APIs and data exchange |
| Hex | #FF0000 |
Normalize format |
| Compact | 255,0,0 |
CSV or minimal output |
Typical performance: ~60ns per conversion on modern hardware.
git clone https://github.com/luhann/hextorgb.git
cd hextorgb
# Build library only
cargo build --release
# Build CLI tool
cargo build --release --features cli
# Run CLI tool
cargo run --features cli -- "#FF0000"# Run all tests
cargo test
# Run with output
cargo test -- --nocapture
# Run integration tests
cargo test --test conversions
# Test CLI features
cargo test --features cliContributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with clap for CLI argument parsing
- Colored output powered by colored
- Inspired by the need for fast, reliable color conversion tools
Made with β€οΈ and π¦ Rust