Skip to content

Commit

Permalink
Merge pull request #389 from Mr-Auto/color
Browse files Browse the repository at this point in the history
Allow creating color from html color names
  • Loading branch information
Dregu authored Jul 11, 2024
2 parents 3f0963e + ec12d92 commit 0c8dc9c
Show file tree
Hide file tree
Showing 12 changed files with 1,240 additions and 82 deletions.
80 changes: 46 additions & 34 deletions docs/game_data/spel2.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/parse_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
"../src/game_api/script/usertypes/steam_lua.cpp",
"../src/game_api/script/usertypes/logic_lua.cpp",
"../src/game_api/script/usertypes/bucket_lua.cpp",
"../src/game_api/script/usertypes/color_lua.cpp",
]
vtable_api_files = [
"../src/game_api/script/usertypes/vtables_lua.cpp",
Expand Down
10 changes: 10 additions & 0 deletions docs/src/includes/_globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,16 @@ Returns the [Bucket](#Bucket) of data stored in shared memory between [Overlunky

Same as `Player.get_heart_color`

### get_color


> Search script examples for [get_color](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=get_color)
#### [uColor](#Aliases) get_color(string color_name, optional<int> alpha = nullopt)

Convert a string to a color, you can use the HTML color names, or even HTML color codes, just prefix them with '#' symbol You can also convert hex string into a color, prefix it with '0x', but use it only if you need to since lua allows for hex values directly too. Default apha value will be 0xFF, unless it's specified Format: [name], #RRGGBB, #RRGGBBAA, 0xBBGGRR, 0xAABBGGRR


### get_current_money


Expand Down
1 change: 1 addition & 0 deletions docs/src/includes/_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ Type | Name | Description
[Color](#Color) | [new(Color other)](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=Color) |
[Color](#Color) | [new(Color color)](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=Color) |
[Color](#Color) | [new(float r_, float g_, float b_, float a_)](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=Color) | Create a new color by specifying its values
[Color](#Color) | [new(string color_name, optional<int> alpha = nullopt)](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=Color) |
float | [r](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=r) |
float | [g](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=g) |
float | [b](https://github.com/spelunky-fyi/overlunky/search?l=Lua&q=b) |
Expand Down
44 changes: 28 additions & 16 deletions src/game_api/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
#include "aliases.hpp"

#include <cmath>
#include <optional>
#include <string>
#include <tuple>

/// Convert a string to a color, you can use the HTML color names, or even HTML color codes, just prefix them with '#' symbol
/// You can also convert hex string into a color, prefix it with '0x', but use it only if you need to since lua allows for hex values directly too.
/// Default apha value will be 0xFF, unless it's specified
/// Format: [name], #RRGGBB, #RRGGBBAA, 0xBBGGRR, 0xAABBGGRR
uColor get_color(const std::string& color_name, std::optional<uint8_t> alpha = std::nullopt);

struct Color
{
/// Create a new color - defaults to black
Expand All @@ -14,17 +22,6 @@ struct Color
constexpr Color& operator=(const Color&) = default;
constexpr Color& operator=(Color&&) = default;

/// Comparison using RGB to avoid non-precise float value
bool operator==(const Color& col) const noexcept
{
const auto current = get_rgba();
const auto compare = col.get_rgba();
return std::get<0>(current) == std::get<0>(compare) &&
std::get<1>(current) == std::get<1>(compare) &&
std::get<2>(current) == std::get<2>(compare) &&
std::get<3>(current) == std::get<3>(compare);
}

/// Create a new color by specifying its values
constexpr Color(float r_, float g_, float b_, float a_) noexcept
: r(r_), g(g_), b(b_), a(a_){};
Expand All @@ -33,12 +30,9 @@ struct Color
constexpr Color(const float (&c)[4]) noexcept
: r(c[0]), g(c[1]), b(c[2]), a(c[3]){};

constexpr void to_float(float (&c)[4]) const noexcept
Color(const std::string& color_name, std::optional<uint8_t> alpha = std::nullopt)
{
c[0] = r;
c[1] = g;
c[2] = b;
c[3] = a;
set_ucolor(get_color(color_name, alpha));
}

static constexpr Color white() noexcept
Expand Down Expand Up @@ -121,6 +115,14 @@ struct Color
return Color(0.5f, 0.0f, 0.5f, 1.0f);
}

constexpr void to_float(float (&c)[4]) const noexcept
{
c[0] = r;
c[1] = g;
c[2] = b;
c[3] = a;
}

/// Returns RGBA colors in 0..255 range
std::tuple<uint8_t, uint8_t, uint8_t, uint8_t> get_rgba() const noexcept
{
Expand Down Expand Up @@ -149,13 +151,22 @@ struct Color
uint8_t alpha = (color >> 24U) & 0xFF;
return set_rgba(red, green, blue, alpha);
}

/// Copies the values of different Color to this one
Color& set(Color& other) noexcept
{
*this = other;
return *this;
}

/// Comparison using RGB to avoid non-precise float value
bool operator==(const Color& col) const noexcept
{
const auto current = get_rgba();
const auto compare = col.get_rgba();
return current == compare;
}

float r{0.0f};
float g{0.0f};
float b{0.0f};
Expand All @@ -164,6 +175,7 @@ struct Color
private:
uint8_t toRGB(const float c) const noexcept
{
// this is not how the game does it, but not sure how to make it 1 : 1
return static_cast<uint8_t>(std::round(255 * std::min(std::max(c, 0.0f), 1.0f)));
}
};
Loading

0 comments on commit 0c8dc9c

Please sign in to comment.