SuperDex Physics C++ API
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19// PLEASE DO NOT ADD OTHER INCLUDES HERE. This header is included in the mochi_physics public API.
22
23namespace superdex {
24
25/**************************************************************************************************
26 Color RGBA as 4 bytes. Used debug visualization.
27*/
28
29/**
30 * @brief RGBA color representation using 4 bytes (0-255 per channel) in RGBA order.
31 * Used primarily for debug visualization.
32 */
34
35/**
36 * @brief Creates a Color from 4 unsigned bytes in RGBA order. This is a convenience function to
37 * relax the requirements of the NdArray constructor (which disallows implicit conversion from int
38 * to uint8_t).
39 */
40[[nodiscard]] constexpr Color MakeColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) {
41 return Color{r, g, b, a};
42}
43
44/**
45 * @brief Creates a Color from a packed 32-bit RGBA value.
46 *
47 * @details Unpacks a 32-bit unsigned integer into RGBA components where:
48 * - Bits 24-31: Red channel
49 * - Bits 16-23: Green channel
50 * - Bits 8-15: Blue channel
51 * - Bits 0-7: Alpha channel
52 *
53 * @param rgba Packed RGBA value as a 32-bit unsigned integer
54 * @return Color object with extracted RGBA components
55 */
56[[nodiscard]] constexpr Color MakeColor(uint32_t rgba) {
57 auto const r = static_cast<uint8_t>(rgba >> 24);
58 auto const g = static_cast<uint8_t>(rgba >> 16);
59 auto const b = static_cast<uint8_t>(rgba >> 8);
60 auto const a = static_cast<uint8_t>(rgba);
61 return Color{r, g, b, a};
62}
63
64/**
65 * @brief Converts a Color to a normalized Float3 vector (RGB only).
66 *
67 * @details Converts the RGB channels of a Color to a Float3 vector with values normalized to the
68 * range [0.0f, 1.0f]. The alpha channel is discarded.
69 *
70 * @param color Input color to convert
71 * @return Float3 vector with normalized RGB values (0.0f - 1.0f)
72 */
73[[nodiscard]] constexpr Float3 ToFloat3(Color const& color) {
74 return Float3{color[0] / 255.0f, color[1] / 255.0f, color[2] / 255.0f};
75}
76
77/**
78 * @brief Converts a Color to a normalized Float4 vector (RGBA).
79 *
80 * @details Converts all four channels of a Color to a Float4 vector with values normalized to the
81 * range [0.0f, 1.0f].
82 *
83 * @param color Input color to convert
84 * @return Float4 vector with normalized RGBA values (0.0f - 1.0f)
85 */
86[[nodiscard]] constexpr Float4 ToFloat4(Color const& color) {
87 return Float4{color[0] / 255.0f, color[1] / 255.0f, color[2] / 255.0f, color[3] / 255.0f};
88}
89
90/**************************************************************************************************/
91
92/**
93 * @namespace superdex::colors
94 * @brief Predefined color constants for common use cases.
95 *
96 * @details This namespace contains a collection of commonly used colors in RGBA format. All colors
97 * have full opacity (alpha = 255). Use these constants for convenience, but feel free to define
98 * custom colors as needed.
99 */
100namespace colors {
101constexpr static Color kBlack = MakeColor(0x000000FF); ///< Pure black color
102constexpr static Color kWhite = MakeColor(0xFFFFFFFF); ///< Pure white color
103constexpr static Color kRed = MakeColor(0xFF0000FF); ///< Pure red color
104constexpr static Color kGreen = MakeColor(0x00FF00FF); ///< Pure green color
105constexpr static Color kBlue = MakeColor(0x0000FFFF); ///< Pure blue color
106constexpr static Color kYellow = MakeColor(0xFFFF00FF); ///< Yellow color (red + green)
107constexpr static Color kCyan = MakeColor(0x00FFFFFF); ///< Cyan color (green + blue)
108constexpr static Color kMagenta = MakeColor(0xFF00FFFF); ///< Magenta color (red + blue)
109constexpr static Color kSilver = MakeColor(0xC0C0C0FF); ///< Light gray color
110constexpr static Color kGray = MakeColor(0x808080FF); ///< Medium gray color (US spelling)
111constexpr static Color kGrey = MakeColor(0x808080FF); ///< Medium gray color (UK spelling)
112constexpr static Color kMaroon = MakeColor(0x800000FF); ///< Dark red color
113constexpr static Color kOlive = MakeColor(0x808000FF); ///< Dark yellow-green color
114constexpr static Color kPurple = MakeColor(0x800080FF); ///< Dark magenta color
115constexpr static Color kTeal = MakeColor(0x008080FF); ///< Dark cyan color
116constexpr static Color kNavy = MakeColor(0x000080FF); ///< Dark blue color
117constexpr static Color kOrange = MakeColor(0xFFA500FF); ///< Orange color
118} // namespace colors
119
120} // namespace superdex
Predefined color constants for common use cases.
static constexpr Color kCyan
Cyan color (green + blue).
Definition color.h:107
static constexpr Color kMaroon
Dark red color.
Definition color.h:112
static constexpr Color kTeal
Dark cyan color.
Definition color.h:115
static constexpr Color kGreen
Pure green color.
Definition color.h:104
static constexpr Color kGray
Medium gray color (US spelling).
Definition color.h:110
static constexpr Color kOrange
Orange color.
Definition color.h:117
static constexpr Color kPurple
Dark magenta color.
Definition color.h:114
static constexpr Color kYellow
Yellow color (red + green).
Definition color.h:106
static constexpr Color kNavy
Dark blue color.
Definition color.h:116
static constexpr Color kGrey
Medium gray color (UK spelling).
Definition color.h:111
static constexpr Color kWhite
Pure white color.
Definition color.h:102
static constexpr Color kBlack
Pure black color.
Definition color.h:101
static constexpr Color kRed
Pure red color.
Definition color.h:103
static constexpr Color kSilver
Light gray color.
Definition color.h:109
static constexpr Color kBlue
Pure blue color.
Definition color.h:105
static constexpr Color kOlive
Dark yellow-green color.
Definition color.h:113
static constexpr Color kMagenta
Magenta color (red + blue).
Definition color.h:108
NdArray< uint8_t, 4 > Color
RGBA color representation using 4 bytes (0-255 per channel) in RGBA order.
Definition color.h:33
NdArray< float, 4 > Float4
Definition nd_array.h:114
constexpr Color MakeColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Creates a Color from 4 unsigned bytes in RGBA order.
Definition color.h:40
constexpr Float4 ToFloat4(Color const &color)
Converts a Color to a normalized Float4 vector (RGBA).
Definition color.h:86
constexpr Float3 ToFloat3(Color const &color)
Converts a Color to a normalized Float3 vector (RGB only).
Definition color.h:73
NdArray< float, 3 > Float3
Definition nd_array.h:113