File: Color_ramp.cpp

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (183 lines) | stat: -rw-r--r-- 2,842 bytes parent folder | download | duplicates (4)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "Color_ramp.h"
#include <iostream>

// -----------------------------------
// Color_component
// -----------------------------------
Color_component::
Color_component()
{
  add(0,0);
  add(1,1);
}

Color_component::
Color_component(const double c0, const double c1)
{
  add(0,c0);
  add(1,c1);
}

double
Color_component::
interpolate(const double v) const
{
  Values::const_iterator next = next_it(v);

  // next is just after v
  Values::const_iterator prev = --next;
  ++next;

  if ( v>=1 && next != values_.end())
    std::cerr << ".";

  if ( next == values_.end() )
  {
    return prev->second;
  }

  const double& a = prev->first;
  const double& b = next->first;
  return (b-v)/(b-a) * prev->second + (v-a)/(b-a) * next->second;

}

void
Color_component::
add(const double v, double color)
{
  if ( color > 1 ) { color = 1; }
  if ( color < 0 ) { color = 0; }

  Values::iterator next = next_it(v);
  values_.insert(next, std::make_pair(v,color));
}


void
Color_component::
rebuild(const double c0, const double c1)
{
  values_.clear();
  add(1,c1);
  add(0,c0);
}

void
Color_component::
print() const
{
  for ( Values::const_iterator it = values_.begin(),
       end = values_.end() ; it != end ; ++it )
  {
    std::cout << "<" << it->first << "," << it->second << "> ";
  }

  std::cout << std::endl;
}


// -----------------------------------
// Color_ramp
// -----------------------------------
Color_ramp::Color_ramp()
  : r_()
  , g_()
  , b_()
{
}

Color_ramp::Color_ramp(const double r0, const double r1,
           const double g0, const double g1,
           const double b0, const double b1)
{
  r_.rebuild(r0,r1);
  g_.rebuild(g0,g1);
  b_.rebuild(b0,b1);

  r_.add(0.5,(std::max)(r0,r1));
  g_.add(0.5,(std::max)(g0,g1));
  b_.add(0.5,(std::max)(b0,b1));
  }

void
Color_ramp::build_red()
{
  r_.rebuild(1,0.5);
  g_.rebuild(1,0);
  b_.rebuild(1,0);

  r_.add(0.3,1);
  r_.add(0.05,1);
  g_.add(0.05,0.95);
  g_.add(0.3,0.5);
  b_.add(0.05,0);
}

void
Color_ramp::build_blue()
{
  r_.rebuild(1,0);
  g_.rebuild(1,0);
  b_.rebuild(1,0.5);

  b_.add(0.1,0.8);
  g_.add(0.1,0.4);
  r_.add(0.1,0.4);
}

void
Color_ramp::build_thermal()
{
  r_.rebuild(1,0.5);
  g_.rebuild(1,0);
  b_.rebuild(1,0);

  r_.add(0.0,0);
  r_.add(1,1);
  g_.add(0.05,0.8);
  g_.add(0.3,0.5);
  b_.add(0.0,1.0);
  b_.add(1.0,0.0);
}

void
Color_ramp::build_rainbow()
{
  r_.rebuild(1,0.5);
  g_.rebuild(1,0);
  b_.rebuild(1,0);

  r_.add(0,0.75);
  g_.add(0,0.75);
  b_.add(0,1);

  r_.add(0.2,0);
  g_.add(0.2,0);
  b_.add(0.2,1);

  r_.add(0.4,0);
  g_.add(0.4,1);
  b_.add(0.4,0);

  r_.add(0.6,1);
  g_.add(0.6,1);
  b_.add(0.6,0);

  r_.add(0.8,1);
  g_.add(0.8,0);
  b_.add(0.8,0);
}

void
Color_ramp::
print() const
{
  std::cout << "r: ";
  r_.print();
  std::cout << "g: ";
  g_.print();
  std::cout << "b: ";
  b_.print();
}