From 89f33af5b21c82d49f9d6cfeb577a46febaf1f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blar?= Date: Tue, 18 Jun 2024 18:48:54 +0100 Subject: [PATCH] refactor: change color class to namespace (#695) --- include/faker-cxx/Color.h | 217 +++++++++++++++--------------- src/modules/color/Color.cpp | 22 +-- src/modules/color/ColorData.cpp | 2 +- src/modules/color/ColorData.h | 2 +- tests/modules/color/ColorTest.cpp | 48 +++---- 5 files changed, 143 insertions(+), 148 deletions(-) diff --git a/include/faker-cxx/Color.h b/include/faker-cxx/Color.h index c4617b2c8..bec751fc1 100644 --- a/include/faker-cxx/Color.h +++ b/include/faker-cxx/Color.h @@ -4,125 +4,120 @@ #include "types/Hex.h" -namespace faker +namespace faker::color { -class Color -{ -public: - /** - * @brief Returns a random color. - * - * @returns Human readable color name. - * - * @code - * Color::name() // "Blue" - * @endcode - */ - static std::string_view name(); +/** + * @brief Returns a random color. + * + * @returns Human readable color name. + * + * @code + * color::name() // "Blue" + * @endcode + */ +std::string_view name(); - /** - * @brief Returns an RGB color. - * - * @param includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. - * - * @returns RGB color formatted with rgb(X,X,X) or rgba(X,X,X,X). - * - * @code - * Color::rgb() // "rgb(67, 28, 240)" - * Color::rgb(true) // "rgba(220, 28, 79, 0.50)" - * @endcode - */ - static std::string rgb(bool includeAlpha = false); +/** + * @brief Returns an RGB color. + * + * @param includeAlpha Adds an alpha value to the color (RGBA). Defaults to `false`. + * + * @returns RGB color formatted with rgb(X,X,X) or rgba(X,X,X,X). + * + * @code + * color::rgb() // "rgb(67, 28, 240)" + * color::rgb(true) // "rgba(220, 28, 79, 0.50)" + * @endcode + */ +std::string rgb(bool includeAlpha = false); - /** - * @brief Returns a hex color. - * - * @param casing Casing of the generated string. Defaults to `HexCasing::Lower`. - * @param prefix Prefix for the generated string. Defaults to `HexPrefix::ZeroX`. - * @param includeAlpha Adds an alpha value to the color. Defaults to `false`. - * - * @returns Hex color formatted that starts with `0x` or `#`. - * - * @code - * Color::hex() // "#e3f380" - * Color::hex(HexCasing::Upper, HexPrefix::ZeroX, true) // "0xE3F3801A" - * @endcode - */ - static std::string hex(HexCasing casing = HexCasing::Lower, HexPrefix prefix = HexPrefix::Hash, - bool includeAlpha = false); +/** + * @brief Returns a hex color. + * + * @param casing Casing of the generated string. Defaults to `HexCasing::Lower`. + * @param prefix Prefix for the generated string. Defaults to `HexPrefix::ZeroX`. + * @param includeAlpha Adds an alpha value to the color. Defaults to `false`. + * + * @returns Hex color formatted that starts with `0x` or `#`. + * + * @code + * color::hex() // "#e3f380" + * color::hex(HexCasing::Upper, HexPrefix::ZeroX, true) // "0xE3F3801A" + * @endcode + */ +std::string hex(HexCasing casing = HexCasing::Lower, HexPrefix prefix = HexPrefix::Hash, bool includeAlpha = false); - /** - * @brief Returns an HSL color. - * - * @param includeAlpha Adds an alpha value to the color (HSLA). Defaults to `false`. - * @returns HSL color formatted with hsl(X,X,X) or hsla(X,X,X,X). - * @code - * Color::hsl() // "hsl(0, 20, 100)" - * Color::hsl(true) // "hsla(0, 0, 100, 0.50)" - * @endcode - */ - static std::string hsl(bool includeAlpha = false); +/** + * @brief Returns an HSL color. + * + * @param includeAlpha Adds an alpha value to the color (HSLA). Defaults to `false`. + * @returns HSL color formatted with hsl(X,X,X) or hsla(X,X,X,X). + * @code + * color::hsl() // "hsl(0, 20, 100)" + * color::hsl(true) // "hsla(0, 0, 100, 0.50)" + * @endcode + */ +std::string hsl(bool includeAlpha = false); - /** - * @brief Returns an LCH color. - * - * @param includeAlpha Adds an alpha value to the color (LCHA). Defaults to `false`. - * @returns LCH color formatted with lch(X,X,X) or lcha(X,X,X,X). - * @code - * Color::lch() // "lch(0, 20, 100)" - * Color::lch(true) // "lcha(0, 0, 100, 0.50)" - * @endcode - */ - static std::string lch(bool includeAlpha = false); +/** + * @brief Returns an LCH color. + * + * @param includeAlpha Adds an alpha value to the color (LCHA). Defaults to `false`. + * @returns LCH color formatted with lch(X,X,X) or lcha(X,X,X,X). + * @code + * color::lch() // "lch(0, 20, 100)" + * color::lch(true) // "lcha(0, 0, 100, 0.50)" + * @endcode + */ +std::string lch(bool includeAlpha = false); - /** - * @brief Return a CMYK color - * - * @returns CMYK color formatted with cmyk(X,X,X,X) - * @code - * Color::cmyk() // "cmyk(0.72, 0.88, 0.00, 0.06)" - * @endcode - */ - static std::string cmyk(); +/** + * @brief Return a CMYK color + * + * @returns CMYK color formatted with cmyk(X,X,X,X) + * @code + * color::cmyk() // "cmyk(0.72, 0.88, 0.00, 0.06)" + * @endcode + */ +std::string cmyk(); - /** - * @brief Return a LAB color - * - * @returns LAB color formatted with lab(X,X,X) - * @code - * Color::lab() // "lab(98.74, 2.18, -2.35)" - * @endcode - */ - static std::string lab(); +/** + * @brief Return a LAB color + * + * @returns LAB color formatted with lab(X,X,X) + * @code + * color::lab() // "lab(98.74, 2.18, -2.35)" + * @endcode + */ +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 HSB color + * + * @returns HSB color formatted with hsb(X,X,X) + * @code + * color::hsb() // "hsb(37, 82, 50)" + * @endcode + */ +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(); +/** + * @brief Return a HSV color + * + * @returns HSV color formatted with hsv(X,X,X) + * @code + * color::hsv() // "hsv(21, 67, 39)" + * @endcode + */ +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(); -}; +/** + * @brief Return a YUV color + * + * @returns YUV color formatted with yuv(X,X,X) + * @code + * color::yuv() // "yuv(42, 234, 109)" + * @endcode + */ +std::string yuv(); } diff --git a/src/modules/color/Color.cpp b/src/modules/color/Color.cpp index faf5204da..edf2cf1cf 100644 --- a/src/modules/color/Color.cpp +++ b/src/modules/color/Color.cpp @@ -10,14 +10,14 @@ #include "faker-cxx/String.h" #include "faker-cxx/types/Hex.h" -namespace faker +namespace faker::color { -std::string_view Color::name() +std::string_view name() { return Helper::arrayElement(colors); } -std::string Color::rgb(bool includeAlpha) +std::string rgb(bool includeAlpha) { const std::integral auto red = Number::integer(255); const std::integral auto green = Number::integer(255); @@ -33,12 +33,12 @@ std::string Color::rgb(bool includeAlpha) return FormatHelper::format("rgba({}, {}, {}, {:.2f})", red, green, blue, alpha); } -std::string Color::hex(HexCasing casing, HexPrefix prefix, bool includeAlpha) +std::string hex(HexCasing casing, HexPrefix prefix, bool includeAlpha) { return String::hexadecimal(includeAlpha ? 8 : 6, casing, prefix); } -std::string Color::hsl(bool include_alpha) +std::string hsl(bool include_alpha) { std::integral auto hue = Number::integer(360); std::integral auto saturation = Number::integer(100); @@ -54,7 +54,7 @@ std::string Color::hsl(bool include_alpha) return FormatHelper::format("hsla({}, {}, {}, {:.2f})", hue, saturation, lightness, alpha); } -std::string Color::lch(bool include_alpha) +std::string lch(bool include_alpha) { std::integral auto luminance = Number::integer(100); std::integral auto chroma = Number::integer(100); @@ -70,7 +70,7 @@ std::string Color::lch(bool include_alpha) return FormatHelper::format("lcha({}, {}, {}, {:.2f})", luminance, chroma, hue, alpha); } -std::string Color::cmyk() +std::string cmyk() { std::floating_point auto cyan = Number::decimal(1.); std::floating_point auto magenta = Number::decimal(1.); @@ -80,7 +80,7 @@ std::string Color::cmyk() return FormatHelper::format("cmyk({:.2f}, {:.2f}, {:.2f}, {:.2f})", cyan, magenta, yellow, key); } -std::string Color::lab() +std::string lab() { std::floating_point auto lightness = Number::decimal(100.0); std::floating_point auto red_green = Number::decimal(-128.0, 128.0); @@ -89,7 +89,7 @@ std::string Color::lab() return FormatHelper::format("lab({:.2f}, {:.2f}, {:.2f})", lightness, red_green, blue_yellow); } -std::string Color::hsb() +std::string hsb() { std::integral auto hue = Number::integer(360); std::integral auto saturation = Number::integer(100); @@ -98,7 +98,7 @@ std::string Color::hsb() return FormatHelper::format("hsb({}, {}, {})", hue, saturation, brightness); } -std::string Color::hsv() +std::string hsv() { std::integral auto hue = Number::integer(360); std::integral auto saturation = Number::integer(100); @@ -107,7 +107,7 @@ std::string Color::hsv() return FormatHelper::format("hsv({}, {}, {})", hue, saturation, value); } -std::string Color::yuv() +std::string yuv() { std::integral auto luminance = Number::integer(255); std::integral auto chrominance_blue = Number::integer(255); diff --git a/src/modules/color/ColorData.cpp b/src/modules/color/ColorData.cpp index 6db823242..361e69e68 100644 --- a/src/modules/color/ColorData.cpp +++ b/src/modules/color/ColorData.cpp @@ -3,7 +3,7 @@ #include #include -namespace faker +namespace faker::color { const std::array colors = { "red", "green", "blue", "yellow", "purple", "mint green", "teal", "white", diff --git a/src/modules/color/ColorData.h b/src/modules/color/ColorData.h index 2e1394bf7..a742f58ad 100644 --- a/src/modules/color/ColorData.h +++ b/src/modules/color/ColorData.h @@ -3,7 +3,7 @@ #include #include -namespace faker +namespace faker::color { extern const std::array colors; } diff --git a/tests/modules/color/ColorTest.cpp b/tests/modules/color/ColorTest.cpp index f0af487c6..93a2c9fe4 100644 --- a/tests/modules/color/ColorTest.cpp +++ b/tests/modules/color/ColorTest.cpp @@ -14,6 +14,7 @@ using namespace ::testing; using namespace faker; +using namespace color; class ColorTest : public Test { @@ -22,7 +23,7 @@ class ColorTest : public Test TEST_F(ColorTest, shouldGenerateColorName) { - const auto generatedColorName = Color::name(); + const auto generatedColorName = name(); ASSERT_TRUE(std::ranges::any_of(colors, [generatedColorName](const std::string_view& colorName) { return colorName == generatedColorName; })); @@ -30,7 +31,7 @@ TEST_F(ColorTest, shouldGenerateColorName) TEST_F(ColorTest, shouldGenerateRgbColorWithoutAlpha) { - const auto generatedRgbColor = Color::rgb(); + const auto generatedRgbColor = rgb(); const auto rgbNumbers = StringHelper::split(generatedRgbColor.substr(4, generatedRgbColor.size() - 1), " "); @@ -48,7 +49,7 @@ TEST_F(ColorTest, shouldGenerateRgbColorWithoutAlpha) TEST_F(ColorTest, shouldGenerateRgbColorWithAlpha) { - const auto generatedRgbaColor = Color::rgb(true); + const auto generatedRgbaColor = rgb(true); const auto rgbaNumbers = StringHelper::split(generatedRgbaColor.substr(5, generatedRgbaColor.size() - 1), " "); @@ -68,7 +69,7 @@ TEST_F(ColorTest, shouldGenerateRgbColorWithAlpha) TEST_F(ColorTest, shouldGenerateHexColorWithoutAlpha) { - const auto hexadecimal = Color::hex(); + const auto hexadecimal = hex(); const auto prefix = hexadecimal.substr(0, 1); const auto hexNumber = hexadecimal.substr(1); @@ -81,7 +82,7 @@ TEST_F(ColorTest, shouldGenerateHexColorWithoutAlpha) TEST_F(ColorTest, shouldGenerateHexColorWithAlpha) { - const auto hexadecimal = Color::hex(HexCasing::Upper, HexPrefix::ZeroX, true); + const auto hexadecimal = hex(HexCasing::Upper, HexPrefix::ZeroX, true); const auto prefix = hexadecimal.substr(0, 2); const auto hexNumber = hexadecimal.substr(2); @@ -94,8 +95,8 @@ TEST_F(ColorTest, shouldGenerateHexColorWithAlpha) TEST_F(ColorTest, shouldGenerateHslWithoutAlpha) { - const auto generatedHslColor = faker::Color::hsl(); - const auto hslValues = faker::StringHelper::split(generatedHslColor.substr(4, generatedHslColor.size() - 1), " "); + const auto generatedHslColor = hsl(); + const auto hslValues = StringHelper::split(generatedHslColor.substr(4, generatedHslColor.size() - 1), " "); int hue, staturation, lightness; @@ -112,8 +113,8 @@ TEST_F(ColorTest, shouldGenerateHslWithoutAlpha) TEST_F(ColorTest, shouldGenerateHslWithAlpha) { - const auto generatedHslaColor = faker::Color::hsl(true); - const auto hslValues = faker::StringHelper::split(generatedHslaColor.substr(5, generatedHslaColor.size() - 1), " "); + const auto generatedHslaColor = hsl(true); + const auto hslValues = StringHelper::split(generatedHslaColor.substr(5, generatedHslaColor.size() - 1), " "); int hue, saturation, lightness; @@ -134,8 +135,8 @@ TEST_F(ColorTest, shouldGenerateHslWithAlpha) TEST_F(ColorTest, shouldGenerateLchWithoutAlpha) { - const auto generatedLchColor = faker::Color::lch(); - const auto lchValues = faker::StringHelper::split(generatedLchColor.substr(4, generatedLchColor.size() - 1), " "); + const auto generatedLchColor = lch(); + const auto lchValues = StringHelper::split(generatedLchColor.substr(4, generatedLchColor.size() - 1), " "); int luminance, chroma, hue; @@ -152,8 +153,8 @@ TEST_F(ColorTest, shouldGenerateLchWithoutAlpha) TEST_F(ColorTest, shouldGenerateLchWithAlpha) { - const auto generatedLchaColor = faker::Color::lch(true); - const auto lchValues = faker::StringHelper::split(generatedLchaColor.substr(5, generatedLchaColor.size() - 1), " "); + const auto generatedLchaColor = lch(true); + const auto lchValues = StringHelper::split(generatedLchaColor.substr(5, generatedLchaColor.size() - 1), " "); int luminance, chroma, hue; @@ -174,9 +175,8 @@ TEST_F(ColorTest, shouldGenerateLchWithAlpha) TEST_F(ColorTest, shouldGenerateCmykColor) { - const auto generatedCmykColor = faker::Color::cmyk(); - const auto cmykValues = - faker::StringHelper::split(generatedCmykColor.substr(5, generatedCmykColor.size() - 1), " "); + const auto generatedCmykColor = cmyk(); + const auto cmykValues = StringHelper::split(generatedCmykColor.substr(5, generatedCmykColor.size() - 1), " "); auto offset = cmykValues[0].size(); const auto cyan = std::stod(cmykValues[0], &offset); @@ -197,8 +197,8 @@ TEST_F(ColorTest, shouldGenerateCmykColor) TEST_F(ColorTest, shouldGenerateLabColor) { - const auto generatedLabColor = faker::Color::lab(); - const auto labValues = faker::StringHelper::split(generatedLabColor.substr(4, generatedLabColor.size() - 1), " "); + const auto generatedLabColor = lab(); + const auto labValues = StringHelper::split(generatedLabColor.substr(4, generatedLabColor.size() - 1), " "); auto offset = labValues[0].size(); const auto lightness = std::stod(labValues[0], &offset); @@ -216,8 +216,8 @@ TEST_F(ColorTest, shouldGenerateLabColor) TEST_F(ColorTest, shouldGenerateHsb) { - const auto generatedHsbColor = faker::Color::hsb(); - const auto hsbValues = faker::StringHelper::split(generatedHsbColor.substr(4, generatedHsbColor.size() - 1), " "); + const auto generatedHsbColor = hsb(); + const auto hsbValues = StringHelper::split(generatedHsbColor.substr(4, generatedHsbColor.size() - 1), " "); int hue, saturation, brightness; @@ -234,8 +234,8 @@ TEST_F(ColorTest, shouldGenerateHsb) TEST_F(ColorTest, shouldGenerateHsv) { - const auto generatedHsvColor = faker::Color::hsv(); - const auto hsvValues = faker::StringHelper::split(generatedHsvColor.substr(4, generatedHsvColor.size() - 1), " "); + const auto generatedHsvColor = hsv(); + const auto hsvValues = StringHelper::split(generatedHsvColor.substr(4, generatedHsvColor.size() - 1), " "); int hue, saturation, brightness; @@ -252,8 +252,8 @@ TEST_F(ColorTest, shouldGenerateHsv) TEST_F(ColorTest, shouldGenerateYuv) { - const auto generatedYuvColor = faker::Color::yuv(); - const auto yuvValues = faker::StringHelper::split(generatedYuvColor.substr(4, generatedYuvColor.size() - 1), " "); + const auto generatedYuvColor = yuv(); + const auto yuvValues = StringHelper::split(generatedYuvColor.substr(4, generatedYuvColor.size() - 1), " "); int luminance, chrominanceBlueColor, chrominanceRedColor;