Skip to content

Commit

Permalink
refactor: move mce::Color into separate source file
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Sep 13, 2024
1 parent a49bb23 commit 08a4940
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
#pragma once

struct IntRange {
int min;
int max;
int range_min;
int range_max;
};
27 changes: 2 additions & 25 deletions include/bedrock/core/math/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,8 @@
namespace mce {
class Color {
public:
[[nodiscard]] std::string toHexString() const
{
std::stringstream stream;
stream << "#" << std::setfill('0') << std::hex //
<< std::setw(2) << static_cast<int>(r * 255) //
<< std::setw(2) << static_cast<int>(g * 255) //
<< std::setw(2) << static_cast<int>(b * 255) //
<< std::setw(2) << static_cast<int>(a * 255);
return stream.str();
}

static Color fromHexString(const std::string &hex_string)
{
Color color = {0};
if (hex_string.length() != 8) {
return color;
}

auto value = std::strtoul(hex_string.c_str(), nullptr, 16);
color.r = static_cast<float>(((value >> 24) & 0xFF)) / 255.0F;
color.g = static_cast<float>(((value >> 16) & 0xFF)) / 255.0F;
color.b = static_cast<float>(((value >> 8) & 0xFF)) / 255.0F;
color.a = static_cast<float>((value & 0xFF)) / 255.0F;
return color;
}
[[nodiscard]] std::string toHexString() const;
static Color fromHexString(const std::string &hex_string);

float r;
float g;
Expand Down
2 changes: 1 addition & 1 deletion include/bedrock/world/level/block/block_legacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#include <shared_mutex>

#include "bedrock/common/resources/base_game_version.h"
#include "bedrock/common/util/int_range.h"
#include "bedrock/core/hashed_string.h"
#include "bedrock/core/math/color.h"
#include "bedrock/core/math/int_range.h"
#include "bedrock/entity/entity_context.h"
#include "bedrock/forward.h"
#include "bedrock/world/direction.h"
Expand Down
41 changes: 41 additions & 0 deletions src/endstone_runtime/bedrock/core/math/color.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "bedrock/core/math/color.h"

std::string mce::Color::toHexString() const
{
std::stringstream stream;
stream << "#" << std::setfill('0') << std::hex //
<< std::setw(2) << static_cast<int>(r * 255) //
<< std::setw(2) << static_cast<int>(g * 255) //
<< std::setw(2) << static_cast<int>(b * 255) //
<< std::setw(2) << static_cast<int>(a * 255);
return stream.str();
}

mce::Color mce::Color::fromHexString(const std::string &hex_string)
{
Color color{};
if (hex_string.length() != 8) {
return color;
}

const auto value = std::strtoul(hex_string.c_str(), nullptr, 16);
color.r = static_cast<float>((value >> 24) & 0xFF) / 255.0F;
color.g = static_cast<float>((value >> 16) & 0xFF) / 255.0F;
color.b = static_cast<float>((value >> 8) & 0xFF) / 255.0F;
color.a = static_cast<float>(value & 0xFF) / 255.0F;
return color;
}

0 comments on commit 08a4940

Please sign in to comment.