Skip to content

Commit

Permalink
Added hsb and hsv functions to Color module
Browse files Browse the repository at this point in the history
  • Loading branch information
PlungedInCode committed Feb 5, 2024
1 parent 447b3a9 commit 0e4428c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
20 changes: 20 additions & 0 deletions include/faker-cxx/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,25 @@ class Color
* @endcode
*/
static std::string lab();

/**
* @brief Return a HSB color
*
* @returns HSB color formatted with hsb(X,X,X)
* @code
* Color::hsb() // "hsb(37, 82, 50)"
* @endcode
*/
static std::string hsb();

/**
* @brief Return a HSV color
*
* @returns HSV color formatted with hsv(X,X,X)
* @code
* Color::hsv() // "hsv(21, 67, 39)"
* @endcode
*/
static std::string hsv();
};
}
18 changes: 18 additions & 0 deletions src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,22 @@ std::string Color::lab()
return FormatHelper::format("lab({:.2f}, {:.2f}, {:.2f})", lightness, redGreenValue, blueYellowValue);
}

std::string Color::hsb()
{
const std::integral auto hue = Number::integer(360);
const std::integral auto saturation = Number::integer(100);
const std::integral auto brightness = Number::integer(100);

return FormatHelper::format("hsb({}, {}, {})", hue, saturation, brightness);
}

std::string Color::hsv()
{
const std::integral auto hue = Number::integer(360);
const std::integral auto saturation = Number::integer(100);
const std::integral auto value = Number::integer(100);

return FormatHelper::format("hsv({}, {}, {})", hue, saturation, value);
}

}
36 changes: 36 additions & 0 deletions src/modules/color/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,39 @@ TEST_F(ColorTest, shouldGenerateLabColor)
ASSERT_TRUE(redGreenValue >= -128. && redGreenValue <= 128.);
ASSERT_TRUE(blueYellowValue >= -128. && blueYellowValue <= 128.);
}

TEST_F(ColorTest, shouldGenerateHsb)
{
const auto generatedHsbColor = faker::Color::hsb();
const auto hsbValues = faker::StringHelper::split(generatedHsbColor.substr(4, generatedHsbColor.size() - 1), " ");

int hue, staturation, brightness;

std::from_chars(hsbValues[0].data(), hsbValues[0].data() + hsbValues[0].size(), hue);
std::from_chars(hsbValues[1].data(), hsbValues[1].data() + hsbValues[1].size(), staturation);
std::from_chars(hsbValues[2].data(), hsbValues[2].data() + hsbValues[2].size(), brightness);

ASSERT_TRUE(generatedHsbColor.starts_with("hsb("));
ASSERT_TRUE(generatedHsbColor.ends_with(")"));
ASSERT_TRUE(hue >= 0 && hue <= 360);
ASSERT_TRUE(staturation >= 0 && staturation <= 100);
ASSERT_TRUE(brightness >= 0 && brightness <= 100);
}

TEST_F(ColorTest, shouldGenerateHsv)
{
const auto generatedHsvColor = faker::Color::hsv();
const auto hsvValues = faker::StringHelper::split(generatedHsvColor.substr(4, generatedHsvColor.size() - 1), " ");

int hue, staturation, brightness;

std::from_chars(hsvValues[0].data(), hsvValues[0].data() + hsvValues[0].size(), hue);
std::from_chars(hsvValues[1].data(), hsvValues[1].data() + hsvValues[1].size(), staturation);
std::from_chars(hsvValues[2].data(), hsvValues[2].data() + hsvValues[2].size(), brightness);

ASSERT_TRUE(generatedHsvColor.starts_with("hsv("));
ASSERT_TRUE(generatedHsvColor.ends_with(")"));
ASSERT_TRUE(hue >= 0 && hue <= 360);
ASSERT_TRUE(staturation >= 0 && staturation <= 100);
ASSERT_TRUE(brightness >= 0 && brightness <= 100);
}

0 comments on commit 0e4428c

Please sign in to comment.