srctree

Robin Linden parent fee9d306 f777b8c7
gfx: Add a Color constructor that accepts a packed rgb int

inlinesplit
gfx/BUILD added: 44, removed: 4, total 40
@@ -1,4 +1,4 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
 
cc_library(
name = "gfx",
@@ -11,3 +11,13 @@ cc_library(
visibility = ["//visibility:public"],
deps = ["//geom"],
)
 
cc_test(
name = "gfx_test",
size = "small",
srcs = ["gfx_test.cpp"],
deps = [
":gfx",
"//etest",
],
)
 
gfx/gfx.h added: 44, removed: 4, total 40
@@ -12,7 +12,17 @@
namespace gfx {
 
struct Color {
constexpr static Color from_rgb(std::int32_t rgb) {
return Color{
.r = static_cast<std::uint8_t>((rgb & 0xFF0000) >> 16),
.g = static_cast<std::uint8_t>((rgb & 0x00FF00) >> 8),
.b = static_cast<std::uint8_t>(rgb & 0x0000FF),
};
}
 
std::uint8_t r, g, b;
 
[[nodiscard]] constexpr bool operator==(Color const &) const = default;
};
 
class IPainter {
 
filename was Deleted added: 44, removed: 4, total 40
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2021 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "gfx/gfx.h"
 
#include "etest/etest.h"
 
using etest::expect;
using gfx::Color;
 
int main() {
etest::test("Color::from_rgb", [] {
expect(Color{0x12, 0x34, 0x56} == Color::from_rgb(0x12'34'56));
expect(Color{} == Color::from_rgb(0));
expect(Color{0xFF, 0xFF, 0xFF} == Color::from_rgb(0xFF'FF'FF));
});
 
return etest::run_all_tests();
}