Skip to content

Commit 79051df

Browse files
authored
Merge branch 'master' into instlib-delete-copy-move
2 parents 828953f + 7778b18 commit 79051df

File tree

4 files changed

+114
-18
lines changed

4 files changed

+114
-18
lines changed

source/web/color_map.h

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef EMP_COLOR_MAP_H
1111
#define EMP_COLOR_MAP_H
1212

13+
#include <cmath>
1314
#include <iomanip>
1415
#include <map>
1516
#include <string>
@@ -39,9 +40,9 @@ namespace emp {
3940

4041
/// Generate a string to describe a JS color out of RGB values.
4142
std::string ColorRGB(int r, int g, int b) {
42-
emp_assert(r >= 0 && r < 255);
43-
emp_assert(g >= 0 && g < 255);
44-
emp_assert(b >= 0 && b < 255);
43+
emp_assert(r >= 0 && r <= 255);
44+
emp_assert(g >= 0 && g <= 255);
45+
emp_assert(b >= 0 && b <= 255);
4546
std::stringstream ss;
4647
ss << '#' << std::setw(2) << std::setfill('0') << std::hex << r
4748
<< std::setw(2) << std::setfill('0') << std::hex << g
@@ -51,14 +52,61 @@ namespace emp {
5152

5253
/// Generate a string to describe a JS color with an alpha channel.
5354
std::string ColorRGB(int r, int g, int b, double a) {
54-
emp_assert(r >= 0 && r < 255);
55-
emp_assert(g >= 0 && g < 255);
56-
emp_assert(b >= 0 && b < 255);
55+
emp_assert(r >= 0 && r <= 255);
56+
emp_assert(g >= 0 && g <= 255);
57+
emp_assert(b >= 0 && b <= 255);
58+
emp_assert(a >= 0 && a <= 1.0);
5759
std::stringstream ss;
5860
ss << "rgba(" << r << ',' << g << ',' << b << ',' << a << ')';
5961
return ss.str();
6062
}
6163

64+
/// Generate a string to describe a JS color out of HSV values.
65+
std::string ColorHSV(double h, double s, double v) {
66+
// adapted from https://gist.github.com/kuathadianto/200148f53616cbd226d993b400214a7f
67+
68+
emp_assert( h >= 0.0 && h <= 360.0);
69+
emp_assert( s >= 0.0 && s <= 1.0);
70+
emp_assert( v >= 0.0 && v <= 1.0);
71+
72+
double c = s * v;
73+
double x = c * (1 - std::abs(std::fmod(h / 60.0, 2) - 1));
74+
double m = v - c;
75+
double rs, gs, bs;
76+
77+
if(h >= 0 && h < 60) {
78+
rs = c;
79+
gs = x;
80+
bs = 0;
81+
} else if(h >= 60 && h < 120) {
82+
rs = x;
83+
gs = c;
84+
bs = 0;
85+
} else if(h >= 120 && h < 180) {
86+
rs = 0;
87+
gs = c;
88+
bs = x;
89+
} else if(h >= 180 && h < 240) {
90+
rs = 0;
91+
gs = x;
92+
bs = c;
93+
} else if(h >= 240 && h < 300) {
94+
rs = x;
95+
gs = 0;
96+
bs = c;
97+
} else {
98+
rs = c;
99+
gs = 0;
100+
bs = x;
101+
}
102+
103+
int r = (rs + m) * 255;
104+
int g = (gs + m) * 255;
105+
int b = (bs + m) * 255;
106+
107+
return ColorRGB(r, g, b);
108+
}
109+
62110
/// Generate a vector of color strings with a specified range of hues, and fixed satuation and
63111
/// luminosity,
64112
const emp::vector<std::string> &

tests/web/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ OFLAGS_web_braces := -g4 -pedantic -Wno-dollar-in-identifier-extension -s TOTAL_
1616
CFLAGS_web := $(CFLAGS_all) $(OFLAGS_web_braces) --js-library ../../source/web/library_emp.js --js-library ../../source/web/d3/library_d3.js -s EXPORTED_FUNCTIONS="['_main', '_empCppCallback']" -s DISABLE_EXCEPTION_CATCHING=1 -s NO_EXIT_RUNTIME=1
1717

1818
default: all
19-
all: JSWrap.js js_utils.js test_visualizations.js
19+
all: JSWrap.js js_utils.js test_visualizations.js color_map
20+
21+
color_map: color_map.cc
22+
g++ $(CFLAGS_all) color_map.cc -o color_map
2023

2124
JSWrap.js: JSWrap.cc
2225
$(CXX_web) $(CFLAGS_web) JSWrap.cc -o JSWrap.js
@@ -28,7 +31,7 @@ test_visualizations.js: test_visualizations.cc
2831
$(CXX_web) $(CFLAGS_web) test_visualizations.cc -o test_visualizations.js
2932

3033
clean:
31-
rm -f JSWrap.js js_utils.js test_visualizations.js *.js.map *.js.mem *~
34+
rm -f JSWrap.js js_utils.js test_visualizations.js *.js.map *.js.mem *~ color_map
3235

3336
# Debugging information
3437
#print-%: ; @echo $*=$($*)

tests/web/color_map.cc

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,62 @@
66
#include <string>
77

88
#include "base/assert.h"
9+
#include "base/vector.h"
910
#include "config/command_line.h"
1011
#include "web/color_map.h"
1112

1213
int main(int argc, char* argv[])
1314
{
14-
std::vector<std::string> args = emp::cl::args_to_strings(argc, argv);
15+
emp::vector<std::string> args = emp::cl::args_to_strings(argc, argv);
1516
const bool verbose = emp::cl::use_arg(args, "-v");
1617

1718
const auto & test_map = emp::GetHueMap(30);
1819
const auto & test_map2 = emp::GetHueMap(60);
20+
const auto & test_map3 = emp::GetHSLMap(30);
21+
const auto & test_map4 = emp::GetHSLMap(60);
1922

20-
for (int i = 0; i < (int) test_map.size(); i++) {
21-
std::cout << test_map[i] << std::endl;
23+
for (size_t i = 0; i < test_map.size(); ++i) {
24+
if (verbose) std::cout << test_map[i] << std::endl;
2225
}
26+
for (size_t i = 0; i < test_map2.size(); ++i) {
27+
if (verbose) std::cout << test_map2[i] << std::endl;
28+
}
29+
for (size_t i = 0; i < test_map3.size(); ++i) {
30+
if (verbose) std::cout << test_map3[i] << std::endl;
31+
}
32+
for (size_t i = 0; i < test_map4.size(); ++i) {
33+
if (verbose) std::cout << test_map4[i] << std::endl;
34+
}
35+
36+
emp_assert(emp::ColorRGB(255,255,255) == "#ffffff");
37+
emp_assert(emp::ColorRGB(0,0,0) == "#000000");
38+
emp_assert(emp::ColorRGB(0,255,0) == "#00ff00");
39+
emp_assert(emp::ColorRGB(26,28,147) == "#1a1c93");
40+
emp_assert(emp::ColorRGB(175,175,144) == "#afaf90");
41+
emp_assert(emp::ColorRGB(195,30,204) == "#c31ecc");
42+
emp_assert(emp::ColorRGB(195,0,204) == "#c300cc");
43+
emp_assert(emp::ColorRGB(195,0,255) == "#c300ff");
44+
45+
emp_assert(emp::ColorRGB(255,255,255,0.5) == "rgba(255,255,255,0.5)");
46+
emp_assert(emp::ColorRGB(255,255,255,1.0) == "rgba(255,255,255,1)");
47+
emp_assert(emp::ColorRGB(26,28,147,0.5) == "rgba(26,28,147,0.5)");
48+
emp_assert(emp::ColorRGB(26,28,147,1.0) == "rgba(26,28,147,1)");
49+
50+
emp_assert(emp::ColorHSL(198,100,100) == "hsl(198,100%,100%)");
51+
emp_assert(emp::ColorHSL(18,0,10) == "hsl(18,0%,10%)");
52+
emp_assert(emp::ColorHSL(360,6,10) == "hsl(360,6%,10%)");
53+
emp_assert(emp::ColorHSL(0,6,10) == "hsl(0,6%,10%)");
54+
55+
emp_assert(emp::ColorHSV(0,0,1.0) == "#ffffff");
56+
emp_assert(emp::ColorHSV(99.0,0,1.0) == "#ffffff");
57+
emp_assert(emp::ColorHSV(360.0,0,1.0) == "#ffffff");
58+
emp_assert(emp::ColorHSV(0,0,0) == "#000000");
59+
emp_assert(emp::ColorHSV(0.3333*360,1.0,1.0) == "#00ff00");
60+
emp_assert(emp::ColorHSV(0.6639*360,0.8231,0.5765) == "#1a1c93");
61+
emp_assert(emp::ColorHSV(0.1667*360,0.1771,0.6863) == "#afaf90");
62+
emp_assert(emp::ColorHSV(297,0.85,0.8000) == "#c31ecc");
63+
emp_assert(emp::ColorHSV(0.8260*360,1.0,0.8000) == "#c300cc");
64+
emp_assert(emp::ColorHSV(286,1.0,1.0) == "#c300ff");
65+
66+
return 0;
2367
}

tests/web/run_tests.sh

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ source ../../third-party/emsdk_portable_repo/emsdk_portable/emsdk_env.sh
22
echo "Source loaded"
33
make
44
echo "compilation done"
5-
for filename in js_utils.js JSWrap.js;
6-
do
7-
node $filename;
8-
if [ $? -gt 0 ];
9-
then
10-
exit 1;
11-
fi;
5+
./color_map
6+
for filename in js_utils.js JSWrap.js;
7+
do
8+
node $filename;
9+
if [ $? -gt 0 ];
10+
then
11+
exit 1;
12+
fi;
1213
done

0 commit comments

Comments
 (0)