HyperChad Color
Color parsing, manipulation, and conversion utilities for HyperChad applications.
Overview
The HyperChad Color package provides:
- Color Parsing: Hex string to color conversion
- Color Constants: Predefined color constants (BLACK, WHITE)
- Format Support: RGB and RGBA color support
- Display Formatting: Color to hex string conversion
- Framework Integration: Optional egui integration
- Error Handling: Comprehensive hex parsing error handling
Features
Color Parsing
- Hex Strings: Parse 3, 4, 6, and 8 character hex strings
- Flexible Input: Support for #-prefixed and plain hex strings
- Whitespace Handling: Automatic whitespace trimming
- Case Insensitive: Support for both uppercase and lowercase hex
Color Formats
- RGB: Standard red, green, blue color values
- RGBA: RGB with alpha transparency channel
- Short Form: 3-character hex (#RGB) expansion
- Long Form: 6-character hex (#RRGGBB) support
Error Handling
- ParseHexError: Comprehensive error types for parsing failures
- Invalid Characters: Detailed error reporting for invalid hex characters
- Length Validation: Proper validation of hex string lengths
- Non-ASCII Detection: Detection and reporting of non-ASCII characters
Installation
Add this to your Cargo.toml:
[]
= { = "../hyperchad/color" }
# Enable additional features
= {
path = "../hyperchad/color",
= ["serde", "egui", "arb"]
}
Usage
Basic Color Creation
use Color;
// Create colors from hex strings
let red = from_hex;
let green = from_hex;
let blue = from_hex;
// Short form hex (automatically expanded)
let white = from_hex; // Expands to #FFFFFF
let black = from_hex; // Expands to #000000
// With alpha channel
let transparent_red = from_hex;
let transparent_blue = from_hex; // Short form with alpha
Error Handling
use ;
// Safe parsing with error handling
match try_from_hex
Color Constants
use Color;
// Use predefined constants
let black = BLACK;
let white = WHITE;
// Constants are equivalent to hex parsing
assert_eq!;
assert_eq!;
Color Display
use Color;
// Colors display as hex strings
let color = from_hex;
println!; // Output: "#FF5733"
// RGBA colors include alpha
let transparent = from_hex;
println!; // Output: "#FF573380"
String Conversion
use Color;
// From &str
let color1: Color = "#FF0000".into;
// From String
let hex_string = "#00FF00".to_string;
let color2: Color = hex_string.into;
// From &String
let color3: Color = .into;
Color Properties
use Color;
let color = from_hex;
// Access color components
println!; // 255
println!; // 87
println!; // 51
println!; // Some(128)
// Check for alpha channel
if color.a.is_some
egui Integration (with egui feature)
use Color;
use Color32;
let color = from_hex;
// Convert to egui Color32
let egui_color: Color32 = color.into;
let egui_color_ref: Color32 = .into;
// Use in egui applications
ui.colored_label;
Color Structure
Supported Hex Formats
- 3-character:
#RGB(e.g.,#F00→#FF0000) - 4-character:
#RGBA(e.g.,#F008→#FF000088) - 6-character:
#RRGGBB(e.g.,#FF0000) - 8-character:
#RRGGBBAA(e.g.,#FF000080)
All formats support:
- Optional
#prefix - Uppercase and lowercase hex digits
- Leading/trailing whitespace (automatically trimmed)
Error Types
Feature Flags
serde: Enable serialization/deserializationegui: Enable egui Color32 conversionarb: Enable arbitrary data generation for testing
Dependencies
- Color Hex: Core hex parsing functionality
- ThisError: Error handling and display
- Serde: Optional serialization support
- egui: Optional egui integration
Integration
This package is designed for:
- UI Frameworks: Color management in UI applications
- CSS Generation: Converting colors to CSS-compatible formats
- Theme Systems: Dynamic color scheme management
- Graphics Applications: Color manipulation and conversion
- Design Tools: Color parsing and validation utilities