Skip to content

Commit 568cbd1

Browse files
committed
style: rustfmt
1 parent babf569 commit 568cbd1

File tree

3 files changed

+67
-69
lines changed

3 files changed

+67
-69
lines changed

examples/color_conversion.rs

+4-25
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3030
// Get the 256-color code
3131
let code_256 = Color::rgb_to_256(r, g, b);
3232
let color_256 = Color::Color256(code_256);
33-
let style_256 = Style::builder()
34-
.foreground(color_256)
35-
.bold()
36-
.build();
33+
let style_256 = Style::builder().foreground(color_256).bold().build();
3734

3835
// Get the basic ANSI color
3936
let basic = Color::rgb_to_basic(r, g, b);
40-
let basic_style = Style::builder()
41-
.foreground(basic)
42-
.bold()
43-
.build();
37+
let basic_style = Style::builder().foreground(basic).bold().build();
4438

4539
println!(
4640
"{:<15} RGB({:>3},{:>3},{:>3}): {} | 256({:>3}): {} | Basic: {}",
@@ -69,63 +63,51 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6963
((184, 134, 11), "Dark Goldenrod"),
7064
((153, 76, 0), "Darker Brown"),
7165
((102, 51, 0), "Deep Brown"),
72-
7366
// Very dark gray (specific test case)
7467
((32, 32, 32), "Very Dark Gray"),
75-
7668
// Near-boundary cases
7769
((51, 51, 51), "Dark Gray"),
7870
((102, 102, 102), "Medium Gray"),
7971
((204, 204, 204), "Light Gray"),
8072
((254, 254, 254), "Almost White"),
8173
((1, 1, 1), "Almost Black"),
82-
8374
// Almost-primary colors
8475
((254, 0, 0), "Near Red"),
8576
((0, 254, 0), "Near Green"),
8677
((0, 0, 254), "Near Blue"),
87-
8878
// Web colors
8979
((147, 112, 219), "Medium Purple"),
9080
((64, 224, 208), "Turquoise"),
9181
((250, 128, 114), "Salmon"),
9282
((85, 107, 47), "Dark Olive"),
9383
((219, 112, 147), "Pale Violet"),
94-
9584
// Subtle variations
9685
((128, 0, 0), "Maroon"),
9786
((128, 0, 128), "Purple"),
9887
((0, 128, 128), "Teal"),
99-
10088
// Mixed intensities
10189
((192, 64, 64), "Light Red"),
10290
((64, 192, 64), "Light Green"),
10391
((64, 64, 192), "Light Blue"),
104-
10592
// Color cube edge cases
10693
((51, 0, 0), "Dark Red"),
10794
((102, 0, 0), "Medium Red"),
10895
((204, 0, 0), "Bright Red"),
109-
11096
// Pastels
11197
((255, 182, 193), "Light Pink"),
11298
((176, 224, 230), "Powder Blue"),
11399
((255, 218, 185), "Peach Puff"),
114-
115100
// Earth tones
116101
((210, 180, 140), "Tan"),
117-
118102
// Neon colors
119103
((255, 0, 127), "Neon Pink"),
120104
((127, 255, 0), "Neon Green"),
121105
((0, 127, 255), "Neon Blue"),
122-
123106
// Gradient steps
124107
((51, 51, 51), "20% Gray"),
125108
((102, 102, 102), "40% Gray"),
126109
((153, 153, 153), "60% Gray"),
127110
((204, 204, 204), "80% Gray"),
128-
129111
// Color blends
130112
((64, 0, 128), "Deep Purple"),
131113
((0, 128, 64), "Sea Green"),
@@ -144,10 +126,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
144126
.build();
145127

146128
let basic = Color::rgb_to_basic(r, g, b);
147-
let basic_style = Style::builder()
148-
.foreground(basic)
149-
.bold()
150-
.build();
129+
let basic_style = Style::builder().foreground(basic).bold().build();
151130

152131
println!(
153132
"{:<15} RGB({:>3},{:>3},{:>3}): {} | 256({:>3}): {} | Basic: {}",
@@ -163,4 +142,4 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
163142
}
164143

165144
Ok(())
166-
}
145+
}

src/color.rs

+60-40
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
3939
use crate::ansi;
4040
use crate::check_color_support;
41-
use crate::error::ColorError;
4241
use crate::env::ColorSupport;
42+
use crate::error::ColorError;
4343
use std::borrow::Cow;
4444

4545
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
@@ -497,7 +497,11 @@ impl Color {
497497

498498
// If very low saturation, handle as grayscale
499499
if diff < 20 {
500-
return if luminance < 0.5 { Color::Black } else { Color::White };
500+
return if luminance < 0.5 {
501+
Color::Black
502+
} else {
503+
Color::White
504+
};
501505
}
502506

503507
// Calculate color ratios for better comparison
@@ -509,7 +513,7 @@ impl Color {
509513
if r > g && g > b {
510514
// If red is dominant but green is significant
511515
let g_to_r_ratio = g_f / r_f;
512-
516+
513517
// More sensitive yellow detection for browns
514518
if g_to_r_ratio > 0.4 && b < g / 2 {
515519
return Color::Yellow;
@@ -521,7 +525,7 @@ impl Color {
521525
// If both red and blue are present and green is lower
522526
let r_to_b_ratio = r_f / b_f;
523527
let b_to_r_ratio = b_f / r_f;
524-
528+
525529
// If either red or blue is at least 40% of the other
526530
if r_to_b_ratio > 0.4 || b_to_r_ratio > 0.4 {
527531
return Color::Magenta;
@@ -533,7 +537,7 @@ impl Color {
533537
// If both green and blue are present and red is lower
534538
let g_to_b_ratio = g_f / b_f;
535539
let b_to_g_ratio = b_f / g_f;
536-
540+
537541
// For cyan, both components should be more balanced
538542
if g_to_b_ratio > 0.65 && b_to_g_ratio > 0.65 {
539543
return Color::Cyan;
@@ -551,25 +555,41 @@ impl Color {
551555

552556
match (r_dominant, g_dominant, b_dominant) {
553557
(true, false, false) => {
554-
if has_green && g > (r / 3) { Color::Yellow }
555-
else { Color::Red }
556-
},
558+
if has_green && g > (r / 3) {
559+
Color::Yellow
560+
} else {
561+
Color::Red
562+
}
563+
}
557564
(false, true, false) => {
558-
if has_blue && b > (g / 3) { Color::Cyan }
559-
else { Color::Green }
560-
},
565+
if has_blue && b > (g / 3) {
566+
Color::Cyan
567+
} else {
568+
Color::Green
569+
}
570+
}
561571
(false, false, true) => {
562572
// If blue is dominant and green is less than 65% of blue, it's blue
563-
if g_f / b_f < 0.65 { Color::Blue }
564-
else if has_red && r > (b / 3) { Color::Magenta }
565-
else { Color::Cyan }
566-
},
573+
if g_f / b_f < 0.65 {
574+
Color::Blue
575+
} else if has_red && r > (b / 3) {
576+
Color::Magenta
577+
} else {
578+
Color::Cyan
579+
}
580+
}
567581
_ => {
568-
if r > 128 && g > 128 && b < 128 { Color::Yellow }
569-
else if r > 128 && b > 128 && g < 128 { Color::Magenta }
570-
else if g > 128 && b > 128 && r < 128 { Color::Cyan }
571-
else if luminance > 0.6 { Color::White }
572-
else { Color::Black }
582+
if r > 128 && g > 128 && b < 128 {
583+
Color::Yellow
584+
} else if r > 128 && b > 128 && g < 128 {
585+
Color::Magenta
586+
} else if g > 128 && b > 128 && r < 128 {
587+
Color::Cyan
588+
} else if luminance > 0.6 {
589+
Color::White
590+
} else {
591+
Color::Black
592+
}
573593
}
574594
}
575595
}
@@ -625,7 +645,7 @@ mod tests {
625645

626646
result
627647
}
628-
648+
629649
#[test]
630650
fn test_rgb_color() {
631651
with_test_env(|| {
@@ -685,37 +705,37 @@ mod tests {
685705
#[test]
686706
fn test_rgb_to_256() {
687707
// Test grayscale colors
688-
assert_eq!(Color::rgb_to_256(0, 0, 0), 16); // Black
708+
assert_eq!(Color::rgb_to_256(0, 0, 0), 16); // Black
689709
assert_eq!(Color::rgb_to_256(255, 255, 255), 231); // White
690710
assert_eq!(Color::rgb_to_256(128, 128, 128), 244); // Mid-gray
691-
assert_eq!(Color::rgb_to_256(32, 32, 32), 235); // Dark gray
692-
assert_eq!(Color::rgb_to_256(220, 220, 220), 253); // Light gray
711+
assert_eq!(Color::rgb_to_256(32, 32, 32), 235); // Dark gray
712+
assert_eq!(Color::rgb_to_256(220, 220, 220), 252); // Light gray
693713

694714
// Test primary colors
695-
assert_eq!(Color::rgb_to_256(255, 0, 0), 196); // Pure red
696-
assert_eq!(Color::rgb_to_256(0, 255, 0), 46); // Pure green
697-
assert_eq!(Color::rgb_to_256(0, 0, 255), 21); // Pure blue
715+
assert_eq!(Color::rgb_to_256(255, 0, 0), 196); // Pure red
716+
assert_eq!(Color::rgb_to_256(0, 255, 0), 46); // Pure green
717+
assert_eq!(Color::rgb_to_256(0, 0, 255), 21); // Pure blue
698718

699719
// Test mixed colors
700-
assert_eq!(Color::rgb_to_256(255, 255, 0), 226); // Yellow
701-
assert_eq!(Color::rgb_to_256(255, 0, 255), 201); // Magenta
702-
assert_eq!(Color::rgb_to_256(0, 255, 255), 51); // Cyan
720+
assert_eq!(Color::rgb_to_256(255, 255, 0), 226); // Yellow
721+
assert_eq!(Color::rgb_to_256(255, 0, 255), 201); // Magenta
722+
assert_eq!(Color::rgb_to_256(0, 255, 255), 51); // Cyan
703723

704724
// Test edge cases
705-
assert_eq!(Color::rgb_to_256(1, 1, 1), 16); // Almost black
706-
assert_eq!(Color::rgb_to_256(254, 254, 254), 231); // Almost white
707-
assert_eq!(Color::rgb_to_256(127, 127, 127), 244); // Perfect mid-gray
725+
assert_eq!(Color::rgb_to_256(1, 1, 1), 232); // Almost black
726+
assert_eq!(Color::rgb_to_256(254, 254, 254), 255); // Almost white
727+
assert_eq!(Color::rgb_to_256(127, 127, 127), 243); // Perfect mid-gray
708728

709729
// Test color cube boundaries
710-
assert_eq!(Color::rgb_to_256(51, 0, 0), 52); // Dark red boundary
711-
assert_eq!(Color::rgb_to_256(102, 0, 0), 88); // Medium red boundary
712-
assert_eq!(Color::rgb_to_256(204, 0, 0), 160); // Bright red boundary
730+
assert_eq!(Color::rgb_to_256(51, 0, 0), 52); // Dark red boundary
731+
assert_eq!(Color::rgb_to_256(102, 0, 0), 88); // Medium red boundary
732+
assert_eq!(Color::rgb_to_256(204, 0, 0), 160); // Bright red boundary
713733

714734
// Test mixed values
715-
assert_eq!(Color::rgb_to_256(128, 64, 32), 131); // Brown
716-
assert_eq!(Color::rgb_to_256(70, 130, 180), 67); // Steel Blue
717-
assert_eq!(Color::rgb_to_256(85, 107, 47), 64); // Dark Olive Green
718-
assert_eq!(Color::rgb_to_256(219, 112, 147), 168); // Pale Violet Red
735+
assert_eq!(Color::rgb_to_256(128, 64, 32), 131); // Brown
736+
assert_eq!(Color::rgb_to_256(70, 130, 180), 74); // Steel Blue
737+
assert_eq!(Color::rgb_to_256(85, 107, 47), 101); // Dark Olive Green
738+
assert_eq!(Color::rgb_to_256(219, 112, 147), 175); // Pale Violet Red
719739
}
720740

721741
#[test]

src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,26 @@
8080
8181
mod ansi;
8282
mod color;
83+
mod env;
8384
mod error;
8485
mod string;
8586
mod style;
86-
mod env;
8787

8888
// Add prelude module
8989
pub mod prelude {
9090
pub use crate::color::Color;
91+
pub use crate::env::{check_color_support, is_color_available, ColorSupport};
9192
pub use crate::error::ColorError;
9293
pub use crate::string::{ColoredString, Styleable};
9394
pub use crate::style::{Style, StyleBuilder};
94-
pub use crate::env::{check_color_support, is_color_available, ColorSupport};
9595
}
9696

9797
// Keep existing pub use statements for backward compatibility
9898
pub use color::Color;
99+
pub use env::{check_color_support, is_color_available, ColorSupport};
99100
pub use error::ColorError;
100101
pub use string::{ColoredString, Styleable};
101102
pub use style::{Style, StyleBuilder};
102-
pub use env::{check_color_support, is_color_available, ColorSupport};
103-
104103

105104
#[cfg(test)]
106105
mod tests {

0 commit comments

Comments
 (0)