Skip to content

Commit 7728148

Browse files
committed
Add Function and Header File
Add Function and Header File
1 parent a977867 commit 7728148

File tree

6 files changed

+352
-0
lines changed

6 files changed

+352
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,50 @@
11
# deneyap-renk-donusturme-arduino-library
22
The library contains simple functions to convert colors between different systems (RGB, HSV, HSL, temperature)
3+
4+
### SDK
5+
List fo Functions
6+
7+
```c++
8+
static void RgbToHsv(uint8_t r, uint8_t g, uint8_t b, double &hue, double &saturation, double &value);
9+
static void RgbToHsl(uint8_t red, uint8_t green, uint8_t blue, double &hue, double &saturation, double &lighting);
10+
static void HsvToRgb(double hue, double saturation, double value, uint8_t & red, uint8_t & green, uint8_t & blue);
11+
static void HslToRgb(double hue, double saturation, double lightness, uint8_t &red, uint8_t &green, uint8_t &blue);
12+
static void TemperatureToRgb(int kelvin, uint8_t & red, uint8_t & green, uint8_t & blue);
13+
static void HexToRgb(String hex, uint8_t& r, uint8_t& g, uint8_t& b);
14+
static void RgbToHex(uint8_t r, uint8_t g, uint8_t b, String &hex);
15+
```
16+
17+
### Example
18+
This is an example.
19+
20+
```c++
21+
#include "Deneyap_RenkDonusturme.h"
22+
23+
void setup()
24+
{
25+
uint8_t red = 50;
26+
uint8_t green = 100;
27+
uint8_t blue = 150;
28+
double hue, saturation, lighting, value;
29+
30+
ColorConverter::RgbToHsl(red, green, blue, hue, saturation, lighting);
31+
ColorConverter::RgbToHsv(red, green, blue, hue, saturation, value);
32+
ColorConverter::HslToRgb(hue, saturation, lighting, red, green, blue);
33+
ColorConverter::HsvToRgb(hue, saturation, lighting, red, green, blue);
34+
35+
ColorConverter::TemperatureToRgb(15000, red, green, blue);
36+
37+
String hex = "010509";
38+
ColorConverter::HexToRgb(hex, red, green, blue);
39+
ColorConverter::RgbToHex(red, green, blue, hex);
40+
}
41+
42+
void loop()
43+
{
44+
45+
}
46+
```
47+
48+
## :bookmark_tabs:License Information
49+
Based on Arduino-ColorConverter Library by Luis Llamas [SORUCE](https://github.com/luisllamasbinaburo/Arduino-ColorConverter).
50+
Please review the [LICENSE](https://github.com/deneyapkart/deneyap-renk-donusturme-arduino-library/blob/master/LICENSE) file for license information.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "Deneyap_RenkDonusturme.h"
2+
3+
void setup()
4+
{
5+
uint8_t red = 50;
6+
uint8_t green = 100;
7+
uint8_t blue = 150;
8+
double hue, saturation, lighting, value;
9+
10+
ColorConverter::RgbToHsl(red, green, blue, hue, saturation, lighting);
11+
ColorConverter::RgbToHsv(red, green, blue, hue, saturation, value);
12+
ColorConverter::HslToRgb(hue, saturation, lighting, red, green, blue);
13+
ColorConverter::HsvToRgb(hue, saturation, lighting, red, green, blue);
14+
15+
ColorConverter::TemperatureToRgb(15000, red, green, blue);
16+
17+
String hex = "010509";
18+
ColorConverter::HexToRgb(hex, red, green, blue);
19+
ColorConverter::RgbToHex(red, green, blue, hex);
20+
}
21+
22+
void loop()
23+
{
24+
25+
}

keywords.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#######################################
2+
# Syntax Coloring Map ColorConverter
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
ColorConverter KEYWORD1
9+
10+
#######################################
11+
# Methods and Functions (KEYWORD2)
12+
#######################################
13+
RgbToHsl KEYWORD2
14+
HslToRgb KEYWORD2
15+
RgbToHsv KEYWORD2
16+
HsvToRgb KEYWORD2
17+
TemperatureToRgb KEYWORD2
18+
19+
#######################################
20+
# Constants (LITERAL1)
21+
#######################################

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Deneyap Renk Donusturme
2+
version=1.0.0
3+
author=Turkish Technnology Team Foundation (T3)
4+
maintainer=Turkish Technnology Team Foundation (T3)
5+
sentence=Arduino library to change colors between different systems
6+
paragraph=The library contains simple functions to convert colors between different systems (RGB, HSV, HSL, temperature, HEX).
7+
category=Other
8+
url=https://github.com/deneyapkart/deneyap-renk-donusturme-arduino-library
9+
architectures=*

src/Deneyap_RenkDonusturme.cpp

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
*****************************************************************************
3+
@file Deneyap_RenkDonusturme.cpp
4+
@mainpage Deneyap Color Converter Arduino library source file
5+
@maintainer RFtek Electronics <[email protected]>
6+
@version v1.0.0
7+
@date March 20, 2023
8+
@brief Includes functions to change colors between different systems
9+
Arduino library
10+
Library includes:
11+
--> Configuration functions
12+
--> Data manipulation functions
13+
*****************************************************************************
14+
*/
15+
16+
#include "Deneyap_RenkDonusturme.h"
17+
18+
void ColorConverter::RgbToHsv(uint8_t red, uint8_t green, uint8_t blue, double& hue, double& saturation, double& value)
19+
{
20+
auto rd = static_cast<double>(red) / 255;
21+
auto gd = static_cast<double>(green) / 255;
22+
auto bd = static_cast<double>(blue) / 255;
23+
auto max = threeway_max(rd, gd, bd), min = threeway_min(rd, gd, bd);
24+
25+
value = max;
26+
27+
auto d = max - min;
28+
saturation = max == 0 ? 0 : d / max;
29+
30+
hue = 0;
31+
if (max != min)
32+
{
33+
if (max == rd)
34+
{
35+
hue = (gd - bd) / d + (gd < bd ? 6 : 0);
36+
}
37+
else if (max == gd)
38+
{
39+
hue = (bd - rd) / d + 2;
40+
}
41+
else if (max == bd)
42+
{
43+
hue = (rd - gd) / d + 4;
44+
}
45+
hue /= 6;
46+
}
47+
}
48+
49+
50+
void ColorConverter::RgbToHsl(uint8_t red, uint8_t green, uint8_t blue, double& hue, double& saturation, double& lighting)
51+
{
52+
auto rd = static_cast<double>(red) / 255;
53+
auto gd = static_cast<double>(green) / 255;
54+
auto bd = static_cast<double>(blue) / 255;
55+
56+
auto max = threeway_max(rd, gd, bd);
57+
auto min = threeway_min(rd, gd, bd);
58+
59+
double h, s, l = (max + min) / 2;
60+
61+
if (max == min)
62+
{
63+
h = s = 0; // achromatic
64+
}
65+
else
66+
{
67+
auto d = max - min;
68+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
69+
if (max == rd)
70+
{
71+
h = (gd - bd) / d + (gd < bd ? 6 : 0);
72+
}
73+
else if (max == gd)
74+
{
75+
h = (bd - rd) / d + 2;
76+
}
77+
else if (max == bd)
78+
{
79+
h = (rd - gd) / d + 4;
80+
}
81+
h /= 6;
82+
}
83+
84+
hue = h;
85+
saturation = s;
86+
lighting = l;
87+
}
88+
89+
void ColorConverter::HsvToRgb(double hue, double saturation, double value, uint8_t& red, uint8_t& green, uint8_t& blue)
90+
{
91+
double r, g, b;
92+
93+
auto i = static_cast<int>(hue * 6);
94+
auto f = hue * 6 - i;
95+
auto p = value * (1 - saturation);
96+
auto q = value * (1 - f * saturation);
97+
auto t = value * (1 - (1 - f) * saturation);
98+
99+
switch (i % 6)
100+
{
101+
case 0: r = value , g = t , b = p;
102+
break;
103+
case 1: r = q , g = value , b = p;
104+
break;
105+
case 2: r = p , g = value , b = t;
106+
break;
107+
case 3: r = p , g = q , b = value;
108+
break;
109+
case 4: r = t , g = p , b = value;
110+
break;
111+
case 5: r = value , g = p , b = q;
112+
break;
113+
}
114+
115+
red = static_cast<uint8_t>(r * 255);
116+
green = static_cast<uint8_t>(g * 255);
117+
blue = static_cast<uint8_t>(b * 255);
118+
}
119+
120+
121+
void ColorConverter::HslToRgb(double hue, double saturation, double lightness, uint8_t& red, uint8_t& green, uint8_t& blue)
122+
{
123+
double r, g, b;
124+
125+
if (saturation == 0)
126+
{
127+
r = g = b = lightness; // achromatic
128+
}
129+
else
130+
{
131+
auto q = lightness < 0.5 ? lightness * (1 + saturation) : lightness + saturation - lightness * saturation;
132+
auto p = 2 * lightness - q;
133+
r = hue2rgb(p, q, hue + 1 / 3.0);
134+
g = hue2rgb(p, q, hue);
135+
b = hue2rgb(p, q, hue - 1 / 3.0);
136+
}
137+
138+
red = static_cast<uint8_t>(r * 255);
139+
green = static_cast<uint8_t>(g * 255);
140+
blue = static_cast<uint8_t>(b * 255);
141+
}
142+
143+
void ColorConverter::TemperatureToRgb(int kelvin, uint8_t& red, uint8_t& green, uint8_t& blue)
144+
{
145+
auto temp = kelvin / 100;
146+
147+
if (temp <= 66)
148+
{
149+
red = 255;
150+
green = 99.4708025861 * log(temp) - 161.1195681661;
151+
152+
if (temp <= 19)
153+
{
154+
blue = 0;
155+
}
156+
else
157+
{
158+
blue = 138.5177312231 * log(temp - 10) - 305.0447927307;
159+
}
160+
}
161+
else
162+
{
163+
red = 329.698727446 * pow(temp - 60, -0.1332047592);
164+
green = 288.1221695283 * pow(temp - 60, -0.0755148492);
165+
blue = 255;
166+
}
167+
}
168+
169+
void ColorConverter::HexToRgb(String hex, uint8_t& red, uint8_t& green, uint8_t& blue)
170+
{
171+
long number;
172+
if(hex[0] == '#') number = strtol(&hex[1], nullptr, 16);
173+
else number = strtol(&hex[0], nullptr, 16);
174+
red = number >> 16;
175+
green = number >> 8 & 0xFF;
176+
blue = number & 0xFF;
177+
}
178+
179+
void ColorConverter::RgbToHex(uint8_t red, uint8_t green, uint8_t blue, String &hex)
180+
{
181+
char hexArray[6] = { 0 };
182+
sprintf(hexArray, "%02X%02X%02X", red, green, blue);
183+
hex = hexArray;
184+
}
185+
186+
187+
double inline ColorConverter::threeway_max(double a, double b, double c)
188+
{
189+
return max(a, max(b, c));
190+
}
191+
192+
double inline ColorConverter::threeway_min(double a, double b, double c)
193+
{
194+
return min(a, min(b, c));
195+
}
196+
197+
double ColorConverter::hue2rgb(double p, double q, double t)
198+
{
199+
if (t < 0) t += 1;
200+
if (t > 1) t -= 1;
201+
if (t < 1 / 6.0) return p + (q - p) * 6 * t;
202+
if (t < 1 / 2.0) return q;
203+
if (t < 2 / 3.0) return p + (q - p) * (2 / 3.0 - t) * 6;
204+
return p;
205+
}

src/Deneyap_RenkDonusturme.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
*****************************************************************************
3+
@file Deneyap_RenkDonusturme.cpp
4+
@mainpage Deneyap Color Converter Arduino library header file
5+
@version v1.0.0
6+
@date March 20, 2023
7+
@brief This file contains all function prototypes and macros
8+
for Deneyap Color Converter Arduino library
9+
*****************************************************************************
10+
*/
11+
12+
#ifndef _COLOERCONVERTER_h
13+
#define _COLOERCONVERTER_h
14+
15+
#if defined(ARDUINO) && ARDUINO >= 100
16+
#include "Arduino.h"
17+
#else
18+
#include "WProgram.h"
19+
#endif
20+
21+
class ColorConverter
22+
{
23+
24+
public:
25+
static void RgbToHsv(uint8_t r, uint8_t g, uint8_t b, double &hue, double &saturation, double &value);
26+
27+
static void RgbToHsl(uint8_t red, uint8_t green, uint8_t blue, double &hue, double &saturation, double &lighting);
28+
29+
static void HsvToRgb(double hue, double saturation, double value, uint8_t & red, uint8_t & green, uint8_t & blue);
30+
31+
static void HslToRgb(double hue, double saturation, double lightness, uint8_t &red, uint8_t &green, uint8_t &blue);
32+
33+
static void TemperatureToRgb(int kelvin, uint8_t & red, uint8_t & green, uint8_t & blue);
34+
35+
static void HexToRgb(String hex, uint8_t& r, uint8_t& g, uint8_t& b);
36+
37+
static void RgbToHex(uint8_t r, uint8_t g, uint8_t b, String &hex);
38+
39+
private:
40+
static double threeway_max(double a, double b, double c);
41+
static double threeway_min(double a, double b, double c);
42+
static double hue2rgb(double p, double q, double t);
43+
};
44+
#endif

0 commit comments

Comments
 (0)