-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
129 lines (110 loc) · 3.6 KB
/
main.cpp
File metadata and controls
129 lines (110 loc) · 3.6 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <string>
#include <stdio.h>
#include "colourConverter.h"
#define TEST_VALS 9
#define ERROR_MARGIN 0.01
using namespace std;
bool sameColour(float *, float *);
void diag(float*);
int main()
{
bool pass = true; // Used to detect failures and print diagnostics
float h = 0.0, c = 0.75, l = 0.5;
float rgb[3] = {0, 0, 0}; // Result var
float expected[3] = {0, 0, 0};
float testHues[TEST_VALS] = {0.0, 0.0, 0.0, 0.0, 30.0, 75.0, 120.0, 162.4, 248.3};
float testChromas[TEST_VALS] = {1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.696, 0.448};
float testSats[TEST_VALS] = {0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.8, 0.6};
float testLumas[TEST_VALS] = {0.3, 0.0, 0.5, 1.0, 0.595, 0.815, 0.59, 0.564, 0.219};
float testHPrimes[TEST_VALS] = {0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 1.5, 0.0, 0.0};
float expectedReds[TEST_VALS] = {1.0, 0.0, 0.5, 1.0, 1.0, 0.75, 0.0, 0.099, 0.211};
float expectedGreens[TEST_VALS] = {0.0, 0.0, 0.5, 1.0, 0.5, 1.0, 1.0, 0.795, 0.149};
float expectedBlues[TEST_VALS] = {0.0, 0.0, 0.5, 1.0, 0.0, 0.0, 0.0, 0.591, 0.597};
float expectedMinorComp[TEST_VALS] = {0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 0.5, 0.0, 0.0};
colourConverter cc;
colourConverter::hcl_to_rgb(h, c, l, rgb);
// smoke test, ensure rgb is modified.
if (rgb[0] == 0) {
printf("FAIL - values not modified.\n");
}
for (int i = 0; i < TEST_VALS; i++) {
// Test minor component
float actualResult;
actualResult = cc.minorComponent(testHPrimes[i], 1.0);
if (expectedMinorComp[i] != actualResult) {
printf("FAIL - Expected MC: %f, actual MC: %f\n",
expectedMinorComp[i], actualResult);
pass = false;
break;
}
// Test conversion from HCL
cc.hcl_to_rgb(testHues[i], testChromas[i], testLumas[i], rgb);
expected[0] = expectedReds[i];
expected[1] = expectedGreens[i];
expected[2] = expectedBlues[i];
if (!sameColour(expected, rgb)) {
printf("FAIL - HCL to RGB conversion.\n");
printf("Hue: %f Expected:\n", testHues[i]);
diag(expected);
printf("Actual:\n");
pass = false;
break;
}
// Ensure values are in correct range
for (int i = 0; i < 3; i++) {
if (rgb[i] < -0.000001 || rgb[i] > 1.000001) {
printf("FAIL - Comp '%d', outside range [0, 1.0] at %f.\n",
i, rgb[i]);
pass = false;
break;
}
}
if (!pass) {
break;
}
}
if (!pass) {
diag(rgb);
}
if (pass) {
printf("SUCCESS!\n");
return 0;
}
return 1;
}
/**
* @brief Compares two float triplets
* @param expected First float triplet (colour)
* @param actual Secondi float triplet
* @return true if triplets are the same, false otherwise
*/
bool sameColour(float *expected, float *actual) {
float result = true;
for (int i = 0; i < 3; i++) {
float diff = expected[i] - actual[i];
if (diff < 0.0)
diff *= -1;
if (diff > ERROR_MARGIN) {
result = false;
break;
}
}
return result;
}
void diag(float *rgb) {
// Transform to ints with appropriate rounding.
int r, g, b;
r = int(rgb[0]*255);
g = int(rgb[1]*255);
b = int(rgb[2]*255);
if (rgb[0] - r >= 0.5)
r++;
if (rgb[1] - g >= 0.5)
g++;
if (rgb[2] - b >= 0.5)
b++;
printf("Red: %d\n", r);
printf("Green: %d\n", g);
printf("Blue: %d\n", b);
}