Branch data Line data Source code
1 : : #ifdef RUN_TESTS
2 : : #include <gtest/gtest.h>
3 : : #include <algorithm>
4 : : #endif
5 : : /**@file tty/256color.cc
6 : : * @brief Defines xterm256table.
7 : : */
8 : :
9 : : #include "256color.hh"
10 : : #include "color.hh"
11 : :
12 : : /** Converts 5-bit colors into 8-bit colors. */
13 : : constexpr unsigned Make16(unsigned r,unsigned g,unsigned b)
14 : : {
15 : : return Repack( { r*255u/31u, g*255u/31u, b*255u/31u } );
16 : : }
17 : : inline constexpr unsigned char grayramp[24] = { 1,2,3,5,6,7,8,9,11,12,13,14,16,17,18,19,20,22,23,24,25,27,28,29 };
18 : : inline constexpr unsigned char colorramp[6] = { 0,12,16,21,26,31 };
19 : :
20 : : /** Builds and defines the table of 256 colors. 8-bit index, 24-bit value. */
21 : : const constinit std::array<unsigned,256> xterm256table = []() consteval
22 : : {
23 : : std::array<unsigned,256> result =
24 : : {
25 : : Make16(0,0,0), Make16(21,0,0), Make16(0,21,0), Make16(21,10,0),
26 : : Make16(0,0,21), Make16(21,0,21), Make16(0,21,21), Make16(21,21,21),
27 : : Make16(15,15,15), Make16(31,10,10), Make16(5,31,10), Make16(31,31,10),
28 : : Make16(10,10,31), Make16(31,10,31), Make16(5,31,31), Make16(31,31,31)
29 : : };
30 : : for(unsigned n=0; n<216; ++n) //LCOV_EXCL_BR_LINE
31 : : {
32 : : result[16+n] = Make16(colorramp[(n/36)%6], colorramp[(n/6)%6], colorramp[(n)%6]);
33 : : }
34 : : for(unsigned n=0; n<24; ++n) //LCOV_EXCL_BR_LINE
35 : : {
36 : : result[232 + n] = Make16(grayramp[n],grayramp[n],grayramp[n]);
37 : : }
38 : : return result;
39 : : }();
40 : :
41 : :
42 : : #ifdef RUN_TESTS
43 : 3 : TEST(xterm256color, contains_two_blacks)
44 : : {
45 : 1 : volatile unsigned r = 0;
46 : 1 : Make16(r,1,2);
47 : 1 : EXPECT_EQ(2, std::count(xterm256table.begin(), xterm256table.end(), 0x000000));
48 : 1 : }
49 : 3 : TEST(xterm256color, contains_two_whites)
50 : : {
51 : 1 : volatile unsigned r = 3;
52 : 1 : EXPECT_EQ(2, std::count(xterm256table.begin(), xterm256table.end(), 0xFFFFFF));
53 : : // Dummy function calls for coverage reasons (gcov counts inline functions):
54 : 1 : Mix(Repack(Unpack(r)),cmyk2rgb(r),r,r,cmy2rgb(r));
55 : 1 : }
56 : : #endif
|