-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor.cc
More file actions
58 lines (48 loc) · 1.84 KB
/
Copy pathColor.cc
File metadata and controls
58 lines (48 loc) · 1.84 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
#include <iostream>
#include "Color.h"
/// Returns an interpolated color, at the given intensity value.
/// The m_colorScale indices are mapped linearly with the
/// given intensity range, so that m_colorScale[0] <-> m_bottom
/// and m_colorScale[m_N] <-> m_top.
/// The color vector OC between OA and AB, is interpolated by:
/// x{C}^i = x{A}^i + d / |AB| * (x{B}^i - x{A}^i)
Color ColorIndex::index( float value ) const
{
if (value > m_top || value < m_bottom) {
std::cerr << "Value: '"<<value<<"', out of range ["<<m_bottom<<","<<m_top<<"]" << std::endl;
return WHITE;
}
if (m_top == m_bottom)
return m_colorScale[0];
float ci = m_N*(value-m_bottom)/(m_top-m_bottom); ///< color index, mapped to [m_bottom,m_top]
float d = ci - floor(ci); ///< Fractional part of 'ci'
int i1 = (int)floor(ci);
int i2 = (int)ceil(ci);
// If the fractional part is zero, it means we are given
// either the top or bottom value, so let's return that color:
if (d == 0)
return m_colorScale[i1];
if (i1 < 0 || i1 > m_N) {
std::cerr << "i1: '"<<i1<<"', out of range [0,"<<m_N<<"]" << std::endl;
return WHITE;
}
if (i2 < 0 || i2 > m_N) {
std::cerr << "i2: '"<<i2<<"', out of range [0,"<<m_N<<"]" << std::endl;
return WHITE;
}
// Colors are treated as 3D vectors in the RGB color cube.
Color OA = m_colorScale[i1], OB = m_colorScale[i2];
float AB = distance( OA, OB );
return Color( OA.r()+d/AB*(OB.r()-OA.r()),
OA.g()+d/AB*(OB.g()-OA.g()),
OA.b()+d/AB*(OB.b()-OA.b()) );
}
const Color WHITE( 1.0, 1.0, 1.0 );
const Color BLACK( 0.0, 0.0, 0.0 );
const Color RED( 1.0, 0.0, 0.0 );
const Color GREEN( 0.0, 1.0, 0.0 );
const Color BLUE( 0.0, 0.0, 1.0 );
const Color PINK( 244, 29, 244 );
const Color LIGHT_BLUE( 16, 242, 227 );
const Color YELLOW( 242, 231, 29 );
const Color ORANGE( 252, 150, 25 );