-
Notifications
You must be signed in to change notification settings - Fork 515
Added color codes #2117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SMJSProductions
wants to merge
14
commits into
geode-sdk:main
Choose a base branch
from
SMJSProductions:color-codes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+285
−0
Open
Added color codes #2117
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1e49aa6
Added color codes
SMJSProductions 313b235
Some small corrections
SMJSProductions 26794a6
Consistent defaults incase the extraction fails
SMJSProductions f98e1bc
Typo fix
SMJSProductions 2715666
Made the parsing return a result and added HSV support
SMJSProductions 726921a
Made some corrections
SMJSProductions 03b3e11
Added some missing consts
SMJSProductions dbda137
Undo incorrect exports
SMJSProductions eba7415
Added a 4F conversion
SMJSProductions caacd57
Added a 4F ctor
SMJSProductions 4d58040
Added a missing method for the 4F conversion
SMJSProductions 51468c3
Fix PR comments
SMJSProductions 7b81717
Removed the getters and setters
SMJSProductions 79d78ca
Refactored isTransparent to isTranslucent
SMJSProductions File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #pragma once | ||
|
|
||
| #include <string_view> | ||
| #include <ccTypes.h> | ||
| #include <Geode/Result.hpp> | ||
|
|
||
| namespace geode { | ||
| template<typename T> | ||
| concept CanSetColor3B = requires(T object) { | ||
| object.setColor(cocos2d::ccColor3B()); | ||
| object.setOpacity(GLubyte()); | ||
| }; | ||
|
SMJSProductions marked this conversation as resolved.
|
||
| template<typename T> | ||
| concept CanSetColor4B = requires(T object) { object.setColor(cocos2d::ccColor4B()); }; | ||
| template<typename T> | ||
| concept CanSetColor4F = requires(T object) { object.setColor(cocos2d::ccColor4F()); }; | ||
|
|
||
| struct GEODE_DLL Color final { | ||
| /// Parses the given hex string using one of the following formats: | ||
| /// #R/G/B, #RR/GG/BB, #RGB, #RGBA, #RRGGBB or #RRGGBBAA | ||
| /// Where the # prefix is optional and / means it's 1 byte/nibble representing all channels | ||
| /// @returns The parsed result | ||
| static Result<Color> parse(std::string_view hex); | ||
| private: | ||
| static GLubyte getByte(uint32_t colorNum, size_t index, bool isShort); | ||
| public: | ||
| operator cocos2d::ccColor3B() const; | ||
| operator cocos2d::ccColor4B() const; | ||
| operator cocos2d::ccColor4F() const; | ||
| operator cocos2d::extension::HSV() const; | ||
| Color& operator=(Color&& other) noexcept = default; | ||
| Color& operator=(const Color& other) noexcept = default; | ||
|
|
||
| GLubyte r; | ||
| GLubyte g; | ||
| GLubyte b; | ||
| GLubyte a; | ||
|
|
||
| Color(Color&& other) noexcept = default; | ||
| Color(const Color& other) noexcept = default; | ||
| /// @returns The equivalent of #000000{a} | ||
| Color(GLubyte alpha = 0); | ||
| Color(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha = 0xFF); | ||
| Color(const cocos2d::ccColor3B& color, GLubyte alpha = 0xFF); | ||
| Color(const cocos2d::ccColor4B& color); | ||
| Color(const cocos2d::ccColor4F& color); | ||
| Color(const cocos2d::extension::HSV& hsv, GLubyte alpha = 0xFF); | ||
|
|
||
| void applyH(double hue); | ||
| void applyHue(double hue); | ||
| void applyS(double saturation); | ||
| void applySaturation(double saturation); | ||
| void applyV(double value); | ||
| void applyValue(double value); | ||
| void applyB(double brightness); | ||
| void applyBrightness(double brightness); | ||
| void applyHSV(const cocos2d::extension::HSV& hsv); | ||
| cocos2d::ccColor3B to3B() const; | ||
| cocos2d::ccColor4B to4B() const; | ||
| cocos2d::ccColor4F to4F() const; | ||
| cocos2d::extension::HSV toHSV() const; | ||
| /// @returns True if alpha is 0 | ||
| bool isInvisible() const; | ||
| /// @returns True if alpha is bigger than 0 and smaller than 255 | ||
| bool isTranslucent() const; | ||
| /// @returns True if alpha is 255 | ||
| bool isOpaque() const; | ||
| template<typename T> requires (CanSetColor3B<T> || CanSetColor4B<T> || CanSetColor4F<T>) | ||
| inline void applyTo(T* node) const { | ||
| if constexpr (CanSetColor3B<T>) { | ||
| node->setColor(this->to3B()); | ||
| node->setOpacity(a); | ||
| } else if constexpr (CanSetColor4B<T>) { | ||
| node->setColor(this->to4B()); | ||
| } else { | ||
| node->setColor(this->to4F()); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| #include <Geode/utils/ColorCode.hpp> | ||
|
|
||
| using namespace geode::prelude; | ||
|
|
||
| Result<Color> Color::parse(std::string_view hex) { | ||
| if (hex.starts_with("#")) { | ||
| hex.remove_prefix(1); | ||
| } | ||
|
|
||
| const size_t size = hex.size(); | ||
|
|
||
| // If the string is an exact size of 1, 2, 3, 4, 6 or 8 | ||
| if (!size || (size > 4 && size != 6 && size != 8)) { | ||
| return Err("Unsupported size"); | ||
| } | ||
|
|
||
| GEODE_UNWRAP_INTO(const uint32_t colorNum, utils::numFromString<uint32_t>(hex, 16)); | ||
| ccColor4B color; | ||
|
|
||
| if (size <= 2) { | ||
| color.r = color.g = color.b = Color::getByte(colorNum, 0, size == 1); | ||
| color.a = 0xFF; | ||
| } else if (size % 3 == 0) { | ||
| const bool isShort = size == 3; | ||
|
|
||
| color.r = Color::getByte(colorNum, 2, isShort); | ||
| color.g = Color::getByte(colorNum, 1, isShort); | ||
| color.b = Color::getByte(colorNum, 0, isShort); | ||
| color.a = 0xFF; | ||
| } else { | ||
| const bool isShort = size == 4; | ||
|
|
||
| color.r = Color::getByte(colorNum, 3, isShort); | ||
| color.g = Color::getByte(colorNum, 2, isShort); | ||
| color.b = Color::getByte(colorNum, 1, isShort); | ||
| color.a = Color::getByte(colorNum, 0, isShort); | ||
| } | ||
|
|
||
| return Ok(Color(color)); | ||
| } | ||
|
|
||
| GLubyte Color::getByte(uint32_t colorNum, size_t index, bool isShort) { | ||
| const uint32_t shiftSize = (isShort ? 4 : 8) * index; | ||
| const GLubyte byte = (colorNum >> shiftSize) & 0xFF; | ||
|
|
||
| // If it's short, mirror the nibble | ||
| return isShort ? (byte << 4) | byte : byte; | ||
| } | ||
|
|
||
|
|
||
| Color::operator ccColor3B() const { | ||
| return { r, g, b }; | ||
| } | ||
|
|
||
| Color::operator ccColor4B() const { | ||
| return { r, g, b, a }; | ||
| } | ||
|
|
||
| Color::operator ccColor4F() const { | ||
| return { r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f }; | ||
| } | ||
|
|
||
| Color::operator HSV() const { | ||
| const double red = r / 255.0; | ||
| const double green = g / 255.0; | ||
| const double blue = b / 255.0; | ||
| const double max = std::max({ red, green, blue }); | ||
| const double delta = max - std::min({ red, green, blue }); | ||
| HSV hsv; | ||
|
|
||
| if (delta == 0) { | ||
| hsv.h = 0; | ||
| } else if (max == red) { | ||
| hsv.h = 60 * std::fmod((green - blue) / delta, 6); | ||
| } else if (max == green) { | ||
| hsv.h = 60 * ((blue - red) / delta + 2); | ||
| } else { | ||
| hsv.h = 60 * ((red - green) / delta + 4); | ||
| } | ||
|
|
||
| if (hsv.h < 0) { | ||
| hsv.h += 360; | ||
| } | ||
|
|
||
| hsv.s = max == 0 ? 0 : delta / max; | ||
| hsv.v = max; | ||
|
|
||
| return hsv; | ||
| } | ||
|
|
||
| Color::Color(GLubyte alpha): r(0x00), g(0x00), b(0x00), a(alpha) { } | ||
|
|
||
| Color::Color(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha): r(red), g(green), b(blue), a(alpha) { } | ||
|
|
||
| Color::Color(const ccColor3B& color, GLubyte alpha): r(color.r), g(color.g), b(color.b), a(alpha) { } | ||
|
|
||
| Color::Color(const ccColor4B& color): r(color.r), g(color.g), b(color.b), a(color.a) { } | ||
|
|
||
| Color::Color(const ccColor4F& color): r(std::round(color.r * 255)), g(std::round(color.g * 255)), b(std::round(color.b * 255)), a(std::round(color.a * 255)) { } | ||
|
|
||
| Color::Color(const HSV& hsv, GLubyte alpha): a(alpha) { | ||
| double correctedHue = std::fmod(hsv.h, 360); | ||
|
|
||
| if (correctedHue < 0) { | ||
| correctedHue += 360; | ||
| } | ||
|
|
||
| const double c = hsv.v * hsv.s; | ||
| const double x = c * (1 - std::abs(std::fmod(correctedHue / 60, 2) - 1)); | ||
| const double m = hsv.v - c; | ||
| double rp, gp, bp; | ||
|
|
||
| if (correctedHue < 60) { | ||
| rp = c; gp = x; bp = 0; | ||
| } else if (correctedHue < 120) { | ||
| rp = x; gp = c; bp = 0; | ||
| } else if (correctedHue < 180) { | ||
| rp = 0; gp = c; bp = x; | ||
| } else if (correctedHue < 240) { | ||
| rp = 0; gp = x; bp = c; | ||
| } else if (correctedHue < 300) { | ||
| rp = x; gp = 0; bp = c; | ||
| } else { | ||
| rp = c; gp = 0; bp = x; | ||
| } | ||
|
|
||
| r = std::round((rp + m) * 255); | ||
| g = std::round((gp + m) * 255); | ||
| b = std::round((bp + m) * 255); | ||
| } | ||
|
|
||
| void Color::applyH(double hue) { | ||
| this->applyHSV({ hue, 1, 1 }); | ||
| } | ||
|
|
||
| void Color::applyHue(double hue) { | ||
| this->applyHSV({ hue, 1, 1 }); | ||
| } | ||
|
|
||
| void Color::applyS(double saturation) { | ||
| this->applyHSV({ 0, saturation, 1 }); | ||
| } | ||
|
|
||
| void Color::applySaturation(double saturation) { | ||
| this->applyHSV({ 0, saturation, 1 }); | ||
| } | ||
|
|
||
| void Color::applyV(double value) { | ||
| this->applyHSV({ 0, 1, value }); | ||
| } | ||
|
|
||
| void Color::applyValue(double value) { | ||
| this->applyHSV({ 0, 1, value }); | ||
| } | ||
|
|
||
| void Color::applyB(double brightness) { | ||
| this->applyHSV({ 0, 1, brightness }); | ||
| } | ||
|
|
||
| void Color::applyBrightness(double brightness) { | ||
| this->applyHSV({ 0, 1, brightness }); | ||
| } | ||
|
|
||
| void Color::applyHSV(const HSV& hsv) { | ||
| HSV currentHSV = *this; | ||
|
|
||
| currentHSV.h = std::fmod(currentHSV.h + hsv.h, 360); | ||
|
|
||
| if (currentHSV.h < 0) { | ||
| currentHSV.h += 360; | ||
| } | ||
|
|
||
| currentHSV.s = std::clamp<double>(currentHSV.s * hsv.s, 0, 1); | ||
| currentHSV.v = std::clamp<double>(currentHSV.v * hsv.v, 0, 1); | ||
|
|
||
| *this = currentHSV; | ||
| } | ||
|
|
||
| ccColor3B Color::to3B() const { | ||
| return *this; | ||
| } | ||
|
|
||
| ccColor4B Color::to4B() const { | ||
| return *this; | ||
| } | ||
|
|
||
| ccColor4F Color::to4F() const { | ||
| return *this; | ||
| } | ||
|
|
||
| HSV Color::toHSV() const { | ||
| return *this; | ||
| } | ||
|
|
||
| bool Color::isInvisible() const { | ||
| return a == 0x00; | ||
| } | ||
|
|
||
| bool Color::isTranslucent() const { | ||
| return a > 0x00 && a < 0xFF; | ||
| } | ||
|
|
||
| bool Color::isOpaque() const { | ||
| return a == 0xFF; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.