Skip to content

Commit e1c0daa

Browse files
committed
test: Update doctests
1 parent e75703a commit e1c0daa

File tree

2 files changed

+81
-31
lines changed

2 files changed

+81
-31
lines changed

CHANGELOG.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ All notable changes to this project will be documented in this file.
1414
The format is based on https://keepachangelog.com/[Keep a Changelog], and this
1515
project adheres to https://semver.org/[Semantic Versioning].
1616

17+
== {compare-url}/v0.1.0\...HEAD[Unreleased]
18+
19+
=== Fixed
20+
21+
* Change `Color.name()` to return `null` if the given color is not fully opaque
22+
({pull-request-url}/4[#4])
23+
1724
== {project-url}/releases/tag/v0.1.0[0.1.0] - 2025-04-28
1825

1926
=== Added

src/color.zig

Lines changed: 74 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,11 @@ pub fn Color(comptime T: type) type {
101101
}
102102

103103
test fromRgba8 {
104-
const color = Color(f64).fromRgba8(255, 0, 0, 255);
105-
try testing.expectEqual(.{ 255, 0, 0, 255 }, color.toRgba8());
104+
const color = Color(f64).fromRgba8(165, 42, 42, 255);
105+
try testing.expectEqualStrings("brown", color.name().?);
106+
var buf: [7]u8 = undefined;
107+
const hex = try color.toHexString(&buf);
108+
try testing.expectEqualStrings("#a52a2a", hex);
106109
}
107110

108111
/// Creates a `Color` from linear-light RGB values.
@@ -141,8 +144,11 @@ pub fn Color(comptime T: type) type {
141144
}
142145

143146
test fromHsl {
144-
const color = Color(f64).fromHsl(360.0, 1.0, 0.5, 1.0);
145-
try testing.expectEqual(.{ 255, 0, 0, 255 }, color.toRgba8());
147+
const color = Color(f64).fromHsl(248.0, 0.39, 0.392, 1.0);
148+
try testing.expectEqual(null, color.name());
149+
var buf: [7]u8 = undefined;
150+
const hex = try color.toHexString(&buf);
151+
try testing.expectEqualStrings("#473d8b", hex);
146152
}
147153

148154
/// Creates a `Color` from
@@ -172,8 +178,11 @@ pub fn Color(comptime T: type) type {
172178
}
173179

174180
test fromHwb {
175-
const color = Color(f64).fromHwb(0.0, 0.0, 0.0, 1.0);
176-
try testing.expectEqual(.{ 255, 0, 0, 255 }, color.toRgba8());
181+
const color = Color(f64).fromHwb(50.6, 0.0, 0.0, 1.0);
182+
try testing.expectEqualStrings("gold", color.name().?);
183+
var buf: [7]u8 = undefined;
184+
const hex = try color.toHexString(&buf);
185+
try testing.expectEqualStrings("#ffd700", hex);
177186
}
178187

179188
/// Creates a `Color` from
@@ -190,16 +199,6 @@ pub fn Color(comptime T: type) type {
190199
return Self.fromLinearRgb(r, g, ib, alpha);
191200
}
192201

193-
test fromOklab {
194-
const color = Color(f64).fromOklab(
195-
0.627_915_193_996_980_9,
196-
0.224_903_230_866_107_1,
197-
0.125_802_870_124_518_02,
198-
1.0,
199-
);
200-
try testing.expectEqual(.{ 255, 0, 0, 255 }, color.toRgba8());
201-
}
202-
203202
/// Creates a `Color` from
204203
/// [OKLCh color](https://www.w3.org/TR/css-color-4/#ok-lab) values.
205204
pub fn fromOklch(lightness: T, chroma: T, hue: T, alpha: T) Self {
@@ -536,31 +535,70 @@ pub fn Color(comptime T: type) type {
536535
}
537536

538537
test parse {
539-
const color = try Color(f64).parse("#ff0");
540-
try testing.expectEqual(
541-
.{ 1.0, 1.0, 0.0, 1.0 },
542-
.{ color.red, color.green, color.blue, color.alpha },
543-
);
544-
try testing.expectEqual(.{ 255, 255, 0, 255 }, color.toRgba8());
545-
var buf: [7]u8 = undefined;
546-
const hex = try color.toHexString(&buf);
547-
try testing.expectEqualStrings("#ffff00", hex);
538+
var buf: [9]u8 = undefined;
539+
540+
{
541+
const color = try Color(f64).parse("#ff0");
542+
try testing.expectEqual(
543+
.{ 1.0, 1.0, 0.0, 1.0 },
544+
.{ color.red, color.green, color.blue, color.alpha },
545+
);
546+
try testing.expectEqual(.{ 255, 255, 0, 255 }, color.toRgba8());
547+
const hex = try color.toHexString(&buf);
548+
try testing.expectEqualStrings("#ffff00", hex);
549+
}
550+
551+
{
552+
const color = try Color(f64).parse("hsl(360 100% 50%)");
553+
try testing.expectEqual(
554+
.{ 1.0, 0.0, 0.0, 1.0 },
555+
.{ color.red, color.green, color.blue, color.alpha },
556+
);
557+
try testing.expectEqual(.{ 255, 0, 0, 255 }, color.toRgba8());
558+
const hex = try color.toHexString(&buf);
559+
try testing.expectEqualStrings("#ff0000", hex);
560+
}
561+
562+
{
563+
const color = try Color(f64).parse("rgb(255 0 0)");
564+
try testing.expectEqual(
565+
.{ 1.0, 0.0, 0.0, 1.0 },
566+
.{ color.red, color.green, color.blue, color.alpha },
567+
);
568+
try testing.expectEqual(.{ 255, 0, 0, 255 }, color.toRgba8());
569+
const hex = try color.toHexString(&buf);
570+
try testing.expectEqualStrings("#ff0000", hex);
571+
}
572+
573+
{
574+
const color = try Color(f64).parse("#ff00007f");
575+
try testing.expectEqual(.{ 255, 0, 0, 127 }, color.toRgba8());
576+
const hex = try color.toHexString(&buf);
577+
try testing.expectEqualStrings("#ff00007f", hex);
578+
}
548579
}
549580

550581
/// Returns the
551582
/// [color name](https://www.w3.org/TR/css-color-4/#named-colors) of
552583
/// this `Color`, returning `null` if it is not available.
553584
pub fn name(self: Self) ?[]const u8 {
554-
const rgb = self.toRgba8()[0..3];
585+
const rgba = self.toRgba8();
586+
const rgb = if (rgba[3] == math.maxInt(u8)) rgba[0..3] else return null;
555587
for (named_colors.named_colors.keys()) |key| {
556588
if (mem.eql(u8, &named_colors.named_colors.get(key).?, rgb)) return key;
557589
}
558590
return null;
559591
}
560592

561593
test name {
562-
const color = try Color(f64).parse("#f0f8ff");
563-
try testing.expectEqualStrings("aliceblue", color.name().?);
594+
const color = try Color(f64).parse("#008080");
595+
try testing.expectEqualStrings("teal", color.name().?);
596+
597+
const color_with_alpha = try Color(f64).parse("#0080807f");
598+
try testing.expectEqual(null, color_with_alpha.name());
599+
600+
const color_without_name = try Color(f64).parse("#7f7f7f");
601+
try testing.expectEqual(null, color_without_name.name());
564602
}
565603

566604
/// Returns an array of
@@ -660,10 +698,15 @@ pub fn Color(comptime T: type) type {
660698
}
661699

662700
test toHexString {
663-
const color = Color(f64).init(1.0, 0.0, 0.0, 1.0);
664-
var buf: [7]u8 = undefined;
701+
var buf: [9]u8 = undefined;
702+
703+
const color = try Color(f64).parse("mediumpurple");
665704
const hex = try color.toHexString(&buf);
666-
try testing.expectEqualStrings("#ff0000", hex);
705+
try testing.expectEqualStrings("#9370db", hex);
706+
707+
const color_with_alpha = try Color(f64).parse("rgb(147 112 219 / 49.8%)");
708+
const hex_with_alpha = try color_with_alpha.toHexString(&buf);
709+
try testing.expectEqualStrings("#9370db7f", hex_with_alpha);
667710
}
668711

669712
fn hslToRgb(h: T, s: T, l: T) [3]T {

0 commit comments

Comments
 (0)