File: Styles.cpp

package info (click to toggle)
ausaxs 1.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 72,592 kB
  • sloc: cpp: 49,853; ansic: 6,901; python: 730; makefile: 18
file content (54 lines) | stat: -rw-r--r-- 1,592 bytes parent folder | download
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
// SPDX-License-Identifier: LGPL-3.0-or-later
// Author: Kristian Lytje

#include <plots/Styles.h>

#include <array>
#include <sstream>
#include <iomanip>

using namespace ausaxs;

std::string style::color_map::Rainbow::next() {
    std::array<double, 3> start = {1, 1, 1};

    // implementation adapted from https://stackoverflow.com/questions/7706339/grayscale-to-red-green-blue-matlab-jet-color-scale
    double v = static_cast<double>(i)/n;
    if (v < 0.25) {
        start[0] = 0;
        start[1] = 4*v;
    } else if (v < 0.5) {
        start[0] = 0;
        start[2] = 1 + 4*(0.25 - v);
    } else if (v < 0.75) {
        start[0] = 4*(v - 0.5);
        start[2] = 0;
    } else {
        start[1] = 1 + 4*(0.75 - v);
        start[2] = 0;
    }

    if (++i == n) {i = 0;}

    std::stringstream ss;
    ss << "#" << std::hex << std::setfill('0') 
        << std::setw(2) << static_cast<int>(start[0]*255) 
        << std::setw(2) << static_cast<int>(start[1]*255) 
        << std::setw(2) << static_cast<int>(start[2]*255);
    return ss.str();
}

std::string style::color_map::RedBlue::next() {
    std::array<double, 3> start = {153, 0, 0}, end = {0, 0, 153};

    double v = static_cast<double>(i)/n;
    std::array<double, 3> rgb = {
        start[0]*(1-v) + end[0]*v, 
        start[1]*(1-v) + end[1]*v, 
        start[2]*(1-v) + end[2]*v
    };

    std::stringstream ss;
    ss << "#" << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(rgb[0]) << std::setw(2) << static_cast<int>(rgb[1]) << std::setw(2) << static_cast<int>(rgb[2]);
    return ss.str();
}