summaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/storageutil/palette.cpp
blob: 31e2f9e426c9fcdc5b8eb8f080baf4e87a14c5ea (plain) (blame)
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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "palette.h"

#include <iostream>
#include <iomanip>

namespace storage {

namespace {
    struct Col {
        int16_t red;
        int16_t green;
        int16_t blue;

        Col(int16_t r, int16_t g, int16_t b) : red(r), green(g), blue(b) {}
    };

    std::vector<Col> createMainColors() {
        std::vector<Col> v;
        v.push_back(Col(128, 128, 128));
        v.push_back(Col(255, 0, 0));
        v.push_back(Col(255, 255, 0));
        v.push_back(Col(255, 0, 255));
        v.push_back(Col(0, 255, 0));
        v.push_back(Col(0, 255, 255));
        v.push_back(Col(0, 0, 255));
        v.push_back(Col(128, 64, 192));
        v.push_back(Col(192, 128, 64));
        v.push_back(Col(64, 192, 128));
        return v;
    }

    std::vector<Col> mainColors(createMainColors());
}

Palette::Palette(uint32_t colorCount)
{

    uint32_t variations = (colorCount + mainColors.size() - 1)
                        / (mainColors.size());
    int16_t darkvars = variations / 2;
    int16_t lightvars = (variations - 1) / 2;

    std::vector<Col> darkVars;
    if (darkvars > 0) {
        for (int32_t i=darkvars; i>0; --i) {
            for (uint32_t j=0; j<mainColors.size(); ++j) {
                Col& main(mainColors[j]);
                int rdiff = main.red / (darkvars + 1);
                int gdiff = main.green / (darkvars + 1);
                int bdiff = main.blue / (darkvars + 1);
                darkVars.push_back(Col(
                    std::max(0, main.red - rdiff * i),
                    std::max(0, main.green - gdiff * i),
                    std::max(0, main.blue - bdiff * i)));
            }
        }
    }
    std::vector<Col> lightVars;
    if (lightvars > 0) {
        for (int32_t i=1; i<=lightvars; ++i) {
            for (uint32_t j=0; j<mainColors.size(); ++j) {
                Col& main(mainColors[j]);
                int rdiff = (255 - main.red) / (lightvars + 1);
                int gdiff = (255 - main.green) / (lightvars + 1);
                int bdiff = (255 - main.blue) / (lightvars + 1);
                lightVars.push_back(Col(
                    std::min(255, main.red + rdiff * i),
                    std::min(255, main.green + gdiff * i),
                    std::min(255, main.blue + bdiff * i)));
            }
        }
    }
    for (std::vector<Col>::const_iterator it = darkVars.begin();
         it != darkVars.end(); ++it)
    {
        _colors.push_back((it->red << 16) | (it->green << 8) | it->blue);
    }
    for (std::vector<Col>::const_iterator it = mainColors.begin();
         it != mainColors.end(); ++it)
    {
        _colors.push_back((it->red << 16) | (it->green << 8) | it->blue);
    }
    for (std::vector<Col>::const_iterator it = lightVars.begin();
         it != lightVars.end(); ++it)
    {
        _colors.push_back((it->red << 16) | (it->green << 8) | it->blue);
    }
}

void
Palette::printHtmlTablePalette(std::ostream& out) const
{
    out << "<table>" << std::hex << std::setfill('0');
    uint32_t col = 0;
    while (col < _colors.size()) {
        out << "\n<tr>";
        for (uint32_t i=0; i<mainColors.size(); ++i) {
            out << "\n  <td bgcolor=\"#" << std::setw(6) << _colors[col++]
                << "\">";
            for (uint32_t j=0; j<6; ++j) out << "&nbsp;";
            out << "</td>";
        }
        out << "\n</tr>";
    }
    out << "\n</table>" << std::dec;
}

} // storage