Skip to content

Commit

Permalink
feat: add hwb function to Color module
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel7722 committed Oct 8, 2024
1 parent 62d5a58 commit d8d5ffd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/faker-cxx/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ FAKER_CXX_EXPORT std::string hsb();
*/
FAKER_CXX_EXPORT std::string hsv();

/**
* @brief Return a HWB color
*
* @returns HWB color formatted with hwb(X,X,X)
* @code
* faker::color::hwb() // "hwb(34, 67, 90)"
* @endcode
*/
FAKER_CXX_EXPORT std::string hwb();

/**
* @brief Return a YUV color
*
Expand Down
9 changes: 9 additions & 0 deletions src/modules/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ std::string hsv()
return common::format("hsv({}, {}, {})", hue, saturation, value);
}

std::string hwb()
{
std::integral auto hue = number::integer(360);
std::integral auto whiteness = number::integer(100);
std::integral auto blackness = number::integer(100);

return common::format("hwb({}, {}, {})", hue, whiteness, blackness);
}

std::string yuv()
{
std::integral auto luminance = number::integer(255);
Expand Down
18 changes: 18 additions & 0 deletions tests/modules/color_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,24 @@ TEST_F(ColorTest, shouldGenerateHsv)
ASSERT_TRUE(brightness >= 0 && brightness <= 100);
}

TEST_F(ColorTest, shouldGenerateHwb)
{
const auto generatedHwbColor = hwb();
const auto hwbValues = common::split(generatedHwbColor.substr(4, generatedHwbColor.size() - 1), " ");

int hue, whiteness, blackness;

std::from_chars(hwbValues[0].data(), hwbValues[0].data() + hwbValues[0].size(), hue);
std::from_chars(hwbValues[1].data(), hwbValues[1].data() + hwbValues[1].size(), whiteness);
std::from_chars(hwbValues[2].data(), hwbValues[2].data() + hwbValues[2].size(), blackness);

ASSERT_TRUE(generatedHwbColor.starts_with("hwb("));
ASSERT_TRUE(generatedHwbColor.ends_with(")"));
ASSERT_TRUE(hue >= 0 && hue <= 360);
ASSERT_TRUE(whiteness >= 0 && whiteness <= 100);
ASSERT_TRUE(blackness >= 0 && blackness <= 100);
}

TEST_F(ColorTest, shouldGenerateYuv)
{
const auto generatedYuvColor = yuv();
Expand Down

0 comments on commit d8d5ffd

Please sign in to comment.