Skip to content

Commit 511b292

Browse files
committedJun 18, 2024
Move DEFAULT const into separate function
Much cleaner. This is the way.
1 parent 01cd2f1 commit 511b292

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed
 

‎src/lib.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ pub fn str2col(mut s: &str) -> Option<(Space, [f32; 3])> {
939939
let mut result = [f32::NAN; 3];
940940

941941
// Return hex if valid
942-
if let Ok(irgb) = hex_to_irgb::<3, 255>(s) {
942+
if let Ok(irgb) = hex_to_irgb(s) {
943943
return Some((space, irgb_to_srgb(irgb)));
944944
}
945945

@@ -1201,8 +1201,7 @@ where
12011201

12021202
/// Create integer RGB set from hex string.
12031203
/// `DEFAULT` is only used when 4 channels are requested but 3 is given.
1204-
/// < 3 pairs or > 4 pairs will `Err`
1205-
pub fn hex_to_irgb<const N: usize, const DEFAULT: u8>(hex: &str) -> Result<[u8; N], String>
1204+
pub fn hex_to_irgb_default<const N: usize, const DEFAULT: u8>(hex: &str) -> Result<[u8; N], String>
12061205
where
12071206
Channels<N>: ValidChannels,
12081207
{
@@ -1242,6 +1241,16 @@ where
12421241
Ok(result)
12431242
}
12441243

1244+
/// Create integer RGB set from hex string.
1245+
/// Will default to 255 for alpha if 4 channels requested but hex length is 6.
1246+
/// Use `hex_to_irgb_default` to customize this.
1247+
pub fn hex_to_irgb<const N: usize>(hex: &str) -> Result<[u8; N], String>
1248+
where
1249+
Channels<N>: ValidChannels,
1250+
{
1251+
hex_to_irgb_default::<N, 255>(hex)
1252+
}
1253+
12451254
/// Convert from HSV to sRGB.
12461255
pub fn hsv_to_srgb<T: DType, const N: usize>(pixel: &mut [T; N])
12471256
where

‎src/tests.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,17 @@ fn hex_to_alpha() {
249249

250250
#[test]
251251
fn hex_from() {
252-
assert_eq!(IRGB, hex_to_irgb::<3, 255>(HEX).unwrap());
253-
assert_eq!(IRGB, hex_to_irgb::<3, 255>(HEXA).unwrap());
252+
assert_eq!(IRGB, hex_to_irgb(HEX).unwrap());
253+
assert_eq!(IRGB, hex_to_irgb(HEXA).unwrap());
254254
}
255255

256256
#[test]
257257
fn hex_from_alpha() {
258-
assert_eq!([IRGB[0], IRGB[1], IRGB[2], 123], hex_to_irgb::<4, 123>(HEX).unwrap());
259-
assert_eq!(IRGBA, hex_to_irgb::<4, 255>(HEXA).unwrap());
258+
assert_eq!(
259+
[IRGB[0], IRGB[1], IRGB[2], 123],
260+
hex_to_irgb_default::<4, 123>(HEX).unwrap()
261+
);
262+
assert_eq!(IRGBA, hex_to_irgb(HEXA).unwrap());
260263
}
261264

262265
#[test]
@@ -271,14 +274,14 @@ fn hex_validations() {
271274
" ABCDEF ",
272275
" #ABCDEF ",
273276
] {
274-
assert!(hex_to_irgb::<3, 255>(hex).is_ok(), "NOT VALID 3: '{}'", hex);
275-
assert!(hex_to_irgb::<4, 255>(hex).is_ok(), "NOT VALID 4: '{}'", hex);
277+
assert!(hex_to_irgb::<3>(hex).is_ok(), "NOT VALID 3: '{}'", hex);
278+
assert!(hex_to_irgb::<4>(hex).is_ok(), "NOT VALID 4: '{}'", hex);
276279
}
277280
for hex in [
278281
"", "#", "#5F", "#ABCDEG", "#abcdeg", "#ABCDEFF", "#abcdeg", "##ABCDEF", "ABCDEF#",
279282
] {
280-
assert!(hex_to_irgb::<3, 255>(hex).is_err(), "NOT INVALID 3: '{}'", hex);
281-
assert!(hex_to_irgb::<4, 255>(hex).is_err(), "NOT INVALID 4: '{}'", hex);
283+
assert!(hex_to_irgb::<3>(hex).is_err(), "NOT INVALID 3: '{}'", hex);
284+
assert!(hex_to_irgb::<4>(hex).is_err(), "NOT INVALID 4: '{}'", hex);
282285
}
283286
}
284287

0 commit comments

Comments
 (0)