Skip to content

Commit 3821cb0

Browse files
committed
Add Color::fromRGB()
Signed-off-by: falkTX <falktx@falktx.com>
1 parent ab25198 commit 3821cb0

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

dgl/Color.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* DISTRHO Plugin Framework (DPF)
3-
* Copyright (C) 2012-2022 Filipe Coelho <falktx@falktx.com>
3+
* Copyright (C) 2012-2025 Filipe Coelho <falktx@falktx.com>
44
*
55
* Permission to use, copy, modify, and/or distribute this software for any purpose with
66
* or without fee is hereby granted, provided that the above copyright notice and this
@@ -115,6 +115,17 @@ struct Color {
115115
*/
116116
static Color fromHTML(const char* rgb, float alpha = 1.0f) noexcept;
117117

118+
/**
119+
Create a color from a RGB unsigned integer.
120+
Basically doing:
121+
```
122+
uint8_t red = (color >> 24) & 0xff;
123+
uint8_t green = (color >> 16) & 0xff;
124+
uint8_t blue = (color >> 8) & 0xff;
125+
```
126+
*/
127+
static Color fromRGB(uint color, float alpha = 1.0f) noexcept;
128+
118129
/**
119130
Linearly interpolate this color against another.
120131
*/

dgl/src/Color.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* DISTRHO Plugin Framework (DPF)
3-
* Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
3+
* Copyright (C) 2012-2025 Filipe Coelho <falktx@falktx.com>
44
*
55
* Permission to use, copy, modify, and/or distribute this software for any purpose with
66
* or without fee is hereby granted, provided that the above copyright notice and this
@@ -242,6 +242,14 @@ Color Color::fromHTML(const char* rgb, const float alpha) noexcept
242242
return Color(r, g, b, alpha);
243243
}
244244

245+
Color Color::fromRGB(const uint color, const float alpha) noexcept
246+
{
247+
return Color(static_cast<int>(color >> 24) & 0xff,
248+
static_cast<int>(color >> 16) & 0xff,
249+
static_cast<int>(color >> 8) & 0xff,
250+
alpha);
251+
}
252+
245253
void Color::interpolate(const Color& other, float u) noexcept
246254
{
247255
fixRange(u);

distrho/DistrhoUI.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ class UI : public UIWidget
106106
107107
The following example code can be use to extract individual colors:
108108
```
109-
const int red = (bgColor >> 24) & 0xff;
110-
const int green = (bgColor >> 16) & 0xff;
111-
const int blue = (bgColor >> 8) & 0xff;
109+
int red = (bgColor >> 24) & 0xff;
110+
int green = (bgColor >> 16) & 0xff;
111+
int blue = (bgColor >> 8) & 0xff;
112112
```
113+
@see Color::fromRGB
113114
*/
114115
uint getBackgroundColor() const noexcept;
115116

@@ -119,10 +120,11 @@ class UI : public UIWidget
119120
120121
The following example code can be use to extract individual colors:
121122
```
122-
const int red = (fgColor >> 24) & 0xff;
123-
const int green = (fgColor >> 16) & 0xff;
124-
const int blue = (fgColor >> 8) & 0xff;
123+
int red = (fgColor >> 24) & 0xff;
124+
int green = (fgColor >> 16) & 0xff;
125+
int blue = (fgColor >> 8) & 0xff;
125126
```
127+
@see Color::fromRGB
126128
*/
127129
uint getForegroundColor() const noexcept;
128130

0 commit comments

Comments
 (0)