# qualpal::colors::RGB

C++ · 4.1.0

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

```cpp
class qualpal::colors::RGB
```

[RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) color representation with values in \[0,1\] range. 

Represents a color in the standard Red-Green-Blue ([RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>)) color space. All channel values are normalized to the range \[0, 1\].

Provides conversion constructors for [HSL](<f36bc23dbb1e50e6765c182aec07a7cf7cd3d62e21fe43426fa3b6afb27490d0>), [XYZ](<4d1887fdbe70c609fc0a10099309f48798368a21d082afcf2ea7968852b3bc87>), [Lab](<bc5685852e488d86b4707de1ef1d606b788c0f89d090ec8c4cf03d84d1a6defd>), and [LCHab](<40196941d3c803da97771dc26834b49dfe5163d017a78d7bdc8fdf4ca5952eb2>) color spaces. Also supports construction from hexadecimal color strings in "\#RRGGBB" or "\#RGB" format.

```cpp
// Create RGB from normalized values
RGB red(1.0, 0.0, 0.0);

// Create from hex string (6-digit and 3-digit)
RGB blue("#0000ff");
RGB magenta("#f0f");

// Convert to hex
std::string hex = red.hex(); // "#ff0000"
```

<a id="documentation-throws"></a>

### Throws

**`std::invalid_argument`**

if hex string format is invalid. 

 

<a id="documentation-see-also"></a>

### See also

[HSL](<f36bc23dbb1e50e6765c182aec07a7cf7cd3d62e21fe43426fa3b6afb27490d0>), [XYZ](<4d1887fdbe70c609fc0a10099309f48798368a21d082afcf2ea7968852b3bc87>), [Lab](<bc5685852e488d86b4707de1ef1d606b788c0f89d090ec8c4cf03d84d1a6defd>), [LCHab](<40196941d3c803da97771dc26834b49dfe5163d017a78d7bdc8fdf4ca5952eb2>) for conversions. 

<a id="cpp-methods"></a>

## Methods

<a id="cpp-cdfd19c73eb635d7d2b79036a2a83a1297191d23cc50bf93362e280bd91d4418"></a>

### `RGB`

<a id="cpp-4eca28f98a362b07bc4100118a9a0ea9f309baf495e8f3f76d3de152b1a75989"></a>

#### `RGB`

```cpp
RGB()
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Default constructor initializes [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) to black (0,0,0). 

<a id="cpp-1df68c7dfa1947597746232e83151d70f4dc38d4e229bedc5135a12be170b8e8"></a>

#### `RGB`

```cpp
RGB(const HSL & hsl)
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Construct [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) from [HSL](<f36bc23dbb1e50e6765c182aec07a7cf7cd3d62e21fe43426fa3b6afb27490d0>) color. 

<a id="cpp-1df68c7dfa1947597746232e83151d70f4dc38d4e229bedc5135a12be170b8e8-documentation-parameters"></a>

###### Parameters

**`hsl`**

[HSL](<f36bc23dbb1e50e6765c182aec07a7cf7cd3d62e21fe43426fa3b6afb27490d0>) color to convert from 

<a id="cpp-c7df6cfcb4c70dbb34173eb8ff8d56a70955476c18ad58e8b2abaef2abf6cca0"></a>

#### `RGB`

```cpp
RGB(const LCHab & lch)
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Construct [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) from [LCHab](<40196941d3c803da97771dc26834b49dfe5163d017a78d7bdc8fdf4ca5952eb2>) color. 

<a id="cpp-c7df6cfcb4c70dbb34173eb8ff8d56a70955476c18ad58e8b2abaef2abf6cca0-documentation-parameters"></a>

###### Parameters

**`lch`**

[LCHab](<40196941d3c803da97771dc26834b49dfe5163d017a78d7bdc8fdf4ca5952eb2>) color to convert from 

<a id="cpp-a32febe87eee2f4fdb1676cc0e26e14e4366164cb7c3028a37854ec4f0d6f780"></a>

#### `RGB`

```cpp
RGB(const Lab & lab)
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Construct [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) from [Lab](<bc5685852e488d86b4707de1ef1d606b788c0f89d090ec8c4cf03d84d1a6defd>) color. 

<a id="cpp-a32febe87eee2f4fdb1676cc0e26e14e4366164cb7c3028a37854ec4f0d6f780-documentation-parameters"></a>

###### Parameters

**`lab`**

[Lab](<bc5685852e488d86b4707de1ef1d606b788c0f89d090ec8c4cf03d84d1a6defd>) color to convert from 

<a id="cpp-253658205945e8195826fbeff5ba435410d9f99b06392d8e3cb7ca61dbccca43"></a>

#### `RGB`

```cpp
RGB(const XYZ & xyz)
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Construct [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) from [XYZ](<4d1887fdbe70c609fc0a10099309f48798368a21d082afcf2ea7968852b3bc87>) color. 

<a id="cpp-253658205945e8195826fbeff5ba435410d9f99b06392d8e3cb7ca61dbccca43-documentation-parameters"></a>

###### Parameters

**`xyz`**

[XYZ](<4d1887fdbe70c609fc0a10099309f48798368a21d082afcf2ea7968852b3bc87>) color to convert from 

<a id="cpp-52e9a4fa4a7cec140dea7a7457b613b9979d6be1ae423359b49a777c39df8e2d"></a>

#### `RGB`

```cpp
inline RGB(const char * hex)
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Construct [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) from hexadecimal color string literal. 

<a id="cpp-52e9a4fa4a7cec140dea7a7457b613b9979d6be1ae423359b49a777c39df8e2d-documentation-parameters"></a>

###### Parameters

**`hex`**

Hex color string in format "\#RRGGBB" or "\#RGB" 

 

<a id="cpp-52e9a4fa4a7cec140dea7a7457b613b9979d6be1ae423359b49a777c39df8e2d-documentation-throws"></a>

###### Throws

**`std::invalid_argument`**

if hex format is invalid 

<a id="cpp-a24d9abc874b54b7b2d4077da7927540aa9424ebda5a287e62bae51ea6250214"></a>

#### `RGB`

```cpp
RGB(const double r, const double g, const double b)
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Construct [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) from normalized values. 

<a id="cpp-a24d9abc874b54b7b2d4077da7927540aa9424ebda5a287e62bae51ea6250214-documentation-parameters"></a>

###### Parameters

**`r`**

Red component \[0,1\] 

**`g`**

Green component \[0,1\] 

**`b`**

Blue component \[0,1\] 

<a id="cpp-f452260401ee8d303dcd5bdac5f2e7f0aa194c0cacced4211d25e30dfb3e20eb"></a>

#### `RGB`

```cpp
RGB(const std::array<double, 3> rgb)
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Construct [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) from array of normalized values. 

<a id="cpp-f452260401ee8d303dcd5bdac5f2e7f0aa194c0cacced4211d25e30dfb3e20eb-documentation-parameters"></a>

###### Parameters

**`rgb`**

Array containing \[r, g, b\] values in \[0,1\] 

<a id="cpp-8bdcdbb7e260c102115269a3e02a277fc6130adf6d098bb6e4c1b63992b3cc24"></a>

#### `RGB`

```cpp
RGB(const std::string & hex)
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Construct [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) from hexadecimal color string. 

<a id="cpp-8bdcdbb7e260c102115269a3e02a277fc6130adf6d098bb6e4c1b63992b3cc24-documentation-parameters"></a>

###### Parameters

**`hex`**

Hex color string in format "\#RRGGBB" or "\#RGB" 

 

<a id="cpp-8bdcdbb7e260c102115269a3e02a277fc6130adf6d098bb6e4c1b63992b3cc24-documentation-throws"></a>

###### Throws

**`std::invalid_argument`**

if hex format is invalid

 

```cpp
RGB red("#ff0000");     // 6-digit format
RGB blue("#00f");       // 3-digit format
```

<a id="cpp-d1e12073e4a74750a17c09a95e8f0b6a981926b122b8d1730f252866c9962cb3"></a>

### `b`

```cpp
inline double b() const
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Get blue component \[0,1\]. 

<a id="cpp-f6a990d61f4d66830b50a9bd4e4895222a137f619354ab427ed618e07ac21f9a"></a>

### `g`

```cpp
inline double g() const
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Get green component \[0,1\]. 

<a id="cpp-453e7eb5d68797f66c78facf2a2bdbf79c6446388e2b049d1e6cbb3dd6035a57"></a>

### `hex`

```cpp
std::string hex() const
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Convert [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) to hexadecimal string. 

<a id="cpp-453e7eb5d68797f66c78facf2a2bdbf79c6446388e2b049d1e6cbb3dd6035a57-documentation-returns"></a>

##### Returns

Hex color string in format "\#rrggbb"

 

```cpp
RGB red(1.0, 0.0, 0.0);
std::string hex = red.hex(); // "#ff0000"
```

<a id="cpp-df5b0556f4669df1473fa2b2f974296dc42ab8b7c79fed542483bb1fb6947212"></a>

### `operator!=`

```cpp
inline bool operator!=(const RGB & other) const
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Inequality operator for [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) colors. 

<a id="cpp-df5b0556f4669df1473fa2b2f974296dc42ab8b7c79fed542483bb1fb6947212-documentation-parameters"></a>

##### Parameters

**`other`**

Other [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) color to compare with 

 

<a id="cpp-df5b0556f4669df1473fa2b2f974296dc42ab8b7c79fed542483bb1fb6947212-documentation-returns"></a>

##### Returns

true if [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) colors are not equal, false otherwise 

<a id="cpp-844fa921fb90b47219d01d1790090d37b52e73f8efb0307cf2b571d682991c59"></a>

### `operator==`

```cpp
inline bool operator==(const RGB & other) const
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Equality operator for [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) colors. 

<a id="cpp-844fa921fb90b47219d01d1790090d37b52e73f8efb0307cf2b571d682991c59-documentation-parameters"></a>

##### Parameters

**`other`**

Other [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) color to compare with 

 

<a id="cpp-844fa921fb90b47219d01d1790090d37b52e73f8efb0307cf2b571d682991c59-documentation-returns"></a>

##### Returns

true if both [RGB](<1a9e7400df83ac8262249ade4a2c007375c2d75db6ae37e8f78ca450266e8411>) colors are equal, false otherwise 

<a id="cpp-f675ea953234d3ef082a35f3c16079de2a7fa7a507fad03e5e60fd6ec6631338"></a>

### `r`

```cpp
inline double r() const
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/colors.h>)

Get red component \[0,1\].
