Skip to content

Commit

Permalink
Added yuv function to Color module (#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
PlungedInCode authored Feb 5, 2024
1 parent a66ca80 commit a6fa4c5
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 @@ -114,5 +114,15 @@ class Color
* @endcode
*/
static std::string hsv();

/**
* @brief Return a YUV color
*
* @returns YUV color formatted with yuv(X,X,X)
* @code
* Color::yuv() // "yuv(42, 234, 109)"
* @endcode
*/
static std::string yuv();
};
}
9 changes: 9 additions & 0 deletions src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,13 @@ std::string Color::hsv()
return FormatHelper::format("hsv({}, {}, {})", hue, saturation, value);
}

std::string Color::yuv()
{
const std::integral auto luminance = Number::integer(255);
const std::integral auto chrominanceBlueColor = Number::integer(255);
const std::integral auto chrominanceRedColor = Number::integer(255);

return FormatHelper::format("yuv({}, {}, {})", luminance, chrominanceBlueColor, chrominanceRedColor);
}

}
18 changes: 18 additions & 0 deletions src/modules/color/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,22 @@ TEST_F(ColorTest, shouldGenerateHsv)
ASSERT_TRUE(hue >= 0 && hue <= 360);
ASSERT_TRUE(staturation >= 0 && staturation <= 100);
ASSERT_TRUE(brightness >= 0 && brightness <= 100);
}

TEST_F(ColorTest, shouldGenerateYuv)
{
const auto generatedYuvColor = faker::Color::yuv();
const auto yuvValues = faker::StringHelper::split(generatedYuvColor.substr(4, generatedYuvColor.size() - 1), " ");

int luminance, chrominanceBlueColor, chrominanceRedColor;

std::from_chars(yuvValues[0].data(), yuvValues[0].data() + yuvValues[0].size(), luminance);
std::from_chars(yuvValues[1].data(), yuvValues[1].data() + yuvValues[1].size(), chrominanceBlueColor);
std::from_chars(yuvValues[2].data(), yuvValues[2].data() + yuvValues[2].size(), chrominanceRedColor);

ASSERT_TRUE(generatedYuvColor.starts_with("yuv("));
ASSERT_TRUE(generatedYuvColor.ends_with(")"));
ASSERT_TRUE(luminance >= 0 && luminance <= 255);
ASSERT_TRUE(chrominanceBlueColor >= 0 && chrominanceBlueColor <= 255);
ASSERT_TRUE(chrominanceRedColor >= 0 && chrominanceRedColor <= 255);
}

0 comments on commit a6fa4c5

Please sign in to comment.