-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrgb_operators.h
46 lines (35 loc) · 1019 Bytes
/
rgb_operators.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// Copyright (c) 2013 Danny Havenith
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
/**
* Operators for RGB values.
* Specifically, this file contains functions for "clipped addition" where the values of two bytes that are added
* will never overflow, but will instead be clipped at the maximum value of 255.
*/
#ifndef RGB_OPERATORS_HPP_
#define RGB_OPERATORS_HPP_
#include "rgb.h"
namespace ws2811
{
namespace detail {
inline uint8_t add_clipped( uint16_t left, uint16_t right)
{
uint16_t result = left + right;
if (result > 255) result = 255;
return result;
}
}
inline void add_clipped( rgb &left, const rgb &right)
{
using detail::add_clipped;
left = rgb(
add_clipped(left.red, right.red),
add_clipped( left.green, right.green),
add_clipped( left.blue, right.blue)
);
}
}
#endif /* RGB_OPERATORS_HPP_ */