From 047146975df4795edd5c68a50ad746f79224130d Mon Sep 17 00:00:00 2001 From: Michal Cieslar Date: Sun, 26 May 2024 20:37:34 +0200 Subject: [PATCH] refactor color module data --- CMakeLists.txt | 1 + include/faker-cxx/Color.h | 2 +- src/modules/color/Color.cpp | 102 +++++++----------- .../color/{data/Colors.h => ColorData.cpp} | 7 +- src/modules/color/ColorData.h | 7 ++ tests/modules/color/ColorTest.cpp | 4 +- 6 files changed, 51 insertions(+), 72 deletions(-) rename src/modules/color/{data/Colors.h => ColorData.cpp} (80%) create mode 100644 src/modules/color/ColorData.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7316a98e3..fe3289c17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ set(FAKER_SOURCES src/modules/book/Book.cpp src/modules/book/BookData.cpp src/modules/color/Color.cpp + src/modules/color/ColorData.cpp src/modules/commerce/Commerce.cpp src/modules/company/Company.cpp src/modules/computer/Computer.cpp diff --git a/include/faker-cxx/Color.h b/include/faker-cxx/Color.h index 7ce62dd55..c4617b2c8 100644 --- a/include/faker-cxx/Color.h +++ b/include/faker-cxx/Color.h @@ -18,7 +18,7 @@ class Color * Color::name() // "Blue" * @endcode */ - static std::string name(); + static std::string_view name(); /** * @brief Returns an RGB color. diff --git a/src/modules/color/Color.cpp b/src/modules/color/Color.cpp index 61cfc32a4..686b2c015 100644 --- a/src/modules/color/Color.cpp +++ b/src/modules/color/Color.cpp @@ -1,18 +1,16 @@ #include "faker-cxx/Color.h" -#include - #include "../../common/FormatHelper.h" -#include "data/Colors.h" +#include "ColorData.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" #include "faker-cxx/String.h" namespace faker { -std::string Color::name() +std::string_view Color::name() { - return Helper::arrayElement(colors); + return Helper::arrayElement(colors); } std::string Color::rgb(bool includeAlpha) @@ -26,116 +24,92 @@ std::string Color::rgb(bool includeAlpha) return FormatHelper::format("rgb({}, {}, {})", red, green, blue); } - const std::floating_point auto alpha = Number::decimal(1); - - std::stringstream ss; - ss << std::fixed; - ss.precision(2); - ss << alpha; - - const auto formattedAlpha = ss.str(); + std::floating_point auto alpha = Number::decimal(1.0); - return FormatHelper::format("rgba({}, {}, {}, {})", red, green, blue, formattedAlpha); + return FormatHelper::format("rgba({}, {}, {}, {:.2f})", red, green, blue, alpha); } std::string Color::hex(HexCasing casing, HexPrefix prefix, bool includeAlpha) { - if (includeAlpha) - { - return String::hexadecimal(8, casing, prefix); - } - - return String::hexadecimal(6, casing, prefix); + return String::hexadecimal(includeAlpha ? 8 : 6, casing, prefix); } -std::string Color::hsl(bool includeAlpha) +std::string Color::hsl(bool include_alpha) { - const std::integral auto hue = Number::integer(360); - const std::integral auto saturation = Number::integer(100); - const std::integral auto lightness = Number::integer(100); + std::integral auto hue = Number::integer(360); + std::integral auto saturation = Number::integer(100); + std::integral auto lightness = Number::integer(100); - if (!includeAlpha) + if (!include_alpha) { return FormatHelper::format("hsl({}, {}, {})", hue, saturation, lightness); } - const std::floating_point auto alpha = Number::decimal(1); - - std::stringstream ss; - ss << std::fixed; - ss.precision(2); - ss << alpha; - const auto formattedAlpha = ss.str(); + std::floating_point auto alpha = Number::decimal(1.0); - return FormatHelper::format("hsla({}, {}, {}, {})", hue, saturation, lightness, formattedAlpha); + return FormatHelper::format("hsla({}, {}, {}, {:.2f})", hue, saturation, lightness, alpha); } -std::string Color::lch(bool includeAlpha) +std::string Color::lch(bool include_alpha) { - const std::integral auto luminance = Number::integer(100); - const std::integral auto chroma = Number::integer(100); - const std::integral auto hue = Number::integer(360); + std::integral auto luminance = Number::integer(100); + std::integral auto chroma = Number::integer(100); + std::integral auto hue = Number::integer(360); - if (!includeAlpha) + if (!include_alpha) { return FormatHelper::format("lch({}, {}, {})", luminance, chroma, hue); } - const std::floating_point auto alpha = Number::decimal(1); - - std::stringstream ss; - ss << std::fixed; - ss.precision(2); - ss << alpha; - const auto formattedAlpha = ss.str(); + std::floating_point auto alpha = Number::decimal(1.0); - return FormatHelper::format("lcha({}, {}, {}, {})", luminance, chroma, hue, formattedAlpha); + return FormatHelper::format("lcha({}, {}, {}, {:.2f})", luminance, chroma, hue, alpha); } std::string Color::cmyk() { - const std::floating_point auto cyan = Number::decimal(1.); - const std::floating_point auto magenta = Number::decimal(1.); - const std::floating_point auto yellow = Number::decimal(1.); - const std::floating_point auto key = Number::decimal(1.); + std::floating_point auto cyan = Number::decimal(1.); + std::floating_point auto magenta = Number::decimal(1.); + std::floating_point auto yellow = Number::decimal(1.); + std::floating_point auto key = Number::decimal(1.); return FormatHelper::format("cmyk({:.2f}, {:.2f}, {:.2f}, {:.2f})", cyan, magenta, yellow, key); } std::string Color::lab() { - const std::floating_point auto lightness = Number::decimal(100.0); - const std::floating_point auto redGreenValue = Number::decimal(-128.0, 128.0); - const std::floating_point auto blueYellowValue = Number::decimal(-128.0, 128.0); + std::floating_point auto lightness = Number::decimal(100.0); + std::floating_point auto red_green = Number::decimal(-128.0, 128.0); + std::floating_point auto blue_yellow = Number::decimal(-128.0, 128.0); - return FormatHelper::format("lab({:.2f}, {:.2f}, {:.2f})", lightness, redGreenValue, blueYellowValue); + return FormatHelper::format("lab({:.2f}, {:.2f}, {:.2f})", lightness, red_green, blue_yellow); } 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); + std::integral auto hue = Number::integer(360); + std::integral auto saturation = Number::integer(100); + 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); + std::integral auto hue = Number::integer(360); + std::integral auto saturation = Number::integer(100); + std::integral auto value = Number::integer(100); 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); + std::integral auto luminance = Number::integer(255); + std::integral auto chrominance_blue = Number::integer(255); + std::integral auto chrominance_red = Number::integer(255); - return FormatHelper::format("yuv({}, {}, {})", luminance, chrominanceBlueColor, chrominanceRedColor); + return FormatHelper::format("yuv({}, {}, {})", luminance, chrominance_blue, chrominance_red); } } diff --git a/src/modules/color/data/Colors.h b/src/modules/color/ColorData.cpp similarity index 80% rename from src/modules/color/data/Colors.h rename to src/modules/color/ColorData.cpp index 666cbc70b..683b9066a 100644 --- a/src/modules/color/data/Colors.h +++ b/src/modules/color/ColorData.cpp @@ -1,11 +1,8 @@ -#pragma once - -#include -#include +#include "ColorData.h" namespace faker { -const std::vector colors = { +const std::array colors = { "red", "green", "blue", "yellow", "purple", "mint green", "teal", "white", "black", "orange", "pink", "grey", "maroon", "violet", "turquoise", "tan", "sky blue", "salmon", "plum", "orchid", "olive", "magenta", "lime", "ivory", diff --git a/src/modules/color/ColorData.h b/src/modules/color/ColorData.h new file mode 100644 index 000000000..26da00e6b --- /dev/null +++ b/src/modules/color/ColorData.h @@ -0,0 +1,7 @@ +#include +#include + +namespace faker +{ +extern const std::array colors; +} diff --git a/tests/modules/color/ColorTest.cpp b/tests/modules/color/ColorTest.cpp index 208da5886..bdccebe80 100644 --- a/tests/modules/color/ColorTest.cpp +++ b/tests/modules/color/ColorTest.cpp @@ -5,9 +5,9 @@ #include "gtest/gtest.h" +#include "color/ColorData.h" #include "common/StringHelper.h" #include "string/data/Characters.h" -#include "color/data/Colors.h" using namespace ::testing; using namespace faker; @@ -21,7 +21,7 @@ TEST_F(ColorTest, shouldGenerateColorName) { const auto generatedColorName = Color::name(); - ASSERT_TRUE(std::ranges::any_of(colors, [generatedColorName](const std::string& colorName) + ASSERT_TRUE(std::ranges::any_of(colors, [generatedColorName](const std::string_view& colorName) { return colorName == generatedColorName; })); }