-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrgb_color.c
69 lines (59 loc) · 1.39 KB
/
rgb_color.c
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* rgb_color.c
*
* Created on: Aug 21, 2019
* Author: kz
*/
#include <stdio.h>
#include <stdint.h>
#include "rgb_color.h"
/********************************************
*
* output: 0 .. 100
*
*********************************************/
void new_color_brgh(rgb_t *col, uint8_t brgh){
uint16_t temp;
if (brgh > 0){
temp = ((uint16_t)(col -> green) * (uint16_t)(brgh)) >> 8;
col -> green = (uint8_t)temp;
temp = ((uint16_t)(col -> red) * (uint16_t)(brgh)) >> 8;
col -> red = (uint8_t)temp;
temp = ((uint16_t)(col -> blue) * (uint16_t)(brgh)) >> 8;
col -> blue = (uint8_t)temp;
}
else{
col -> green = 0;
col -> red = 0;
col -> blue = 0;
}
}
/**********************************************
*
* output:
* 0 - colors are equal
* -1 - colors are NOT equal
*
***********************************************/
int8_t cmp_color(rgb_t color_1, rgb_t color_2){
int8_t result = 0;
if (color_1.red != color_2.red){
result = -1;
}
if (color_1.green != color_2.green){
result = -1;
}
if (color_1.blue != color_2.blue){
result = -1;
}
return result;
}
/*******************************************
*
* convert color into "#aabbcc" format where
* aa - red, bb - green, cc - blue
*
*******************************************/
void sprint_color(char *buff, rgb_t color){
sprintf(buff, "#%02x%02x%02x", color.red, color.green, color.blue);
}