summaryrefslogtreecommitdiff
path: root/src/pmview/colorscale.cpp
blob: c90658d2c5bd52b1a60a2c6104a0ab11e2bae1fe (plain)
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
/*
 * Copyright (c) 1997 Silicon Graphics, Inc.  All Rights Reserved.
 * Copyright (c) 2009 Aconex.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */
#include "main.h"
#include "colorscale.h"

ColorStep::~ColorStep()
{
}

ColorStep::ColorStep(SbColor col, float val)
: _color(), 
  _min(val)
{
    _color.setValue(col.getValue());
}

ColorStep::ColorStep(float r, float g, float b, float val)
: _color(), 
  _min(val)
{
    SbColor tmp(r, g, b);
    _color.setValue(tmp.getValue());
}

ColorStep::ColorStep(uint32_t col, float val)
: _color(), 
  _min(val)
{
    float dummy = 0.0;
    _color.setPackedValue(col, dummy);
}

ColorStep::ColorStep(const ColorStep &rhs)
: _color(), 
  _min(rhs._min)
{
    _color.setValue(rhs._color.getValue());
}

const ColorStep &
ColorStep::operator=(const ColorStep &rhs)
{
    if (this != &rhs) {
	_color.setValue(rhs._color.getValue());
	_min = rhs._min;
    }
    return *this;
}

ColorScale::~ColorScale()
{
    int		i;

    for (i = 0; i < _colors.size(); i++)
	delete _colors.takeAt(i);
}

ColorScale::ColorScale(const SbColor &col)
: _colors()
{
    add(new ColorStep(col));
}

ColorScale::ColorScale(float r, float g, float b)
: _colors()
{
    add(new ColorStep(r, g, b));
}

ColorScale::ColorScale(uint32_t col)
: _colors()
{
    add(new ColorStep(col));
}

ColorScale::ColorScale(const ColorScale &rhs)
: _colors()
{
    int		i;

    for (i = 0; i < rhs._colors.size(); i++)
    	add(new ColorStep(rhs[i]));
}

const ColorScale &
ColorScale::operator=(const ColorScale &rhs)
{
    int		i;

    if (this != &rhs) {
    	for (i = 0; i < _colors.size(); i++)
	    delete _colors.takeAt(i);
	for (i = 0; i < rhs._colors.size(); i++)
	    add(new ColorStep(rhs[i]));
    }
    return *this;
}

int
ColorScale::add(ColorStep *ptr)
{
    if (_colors.size()) {
    	float prev = _colors.last()->min();
	if (prev >= ptr->min()) {
	    warningMsg(_POS_, 
		"Color step (%f) was less than previous step (%f), skipping.",
		ptr->min(), prev);
	    return -1;
	}
    }
    _colors.append(ptr);

    return 0;
}

const ColorStep &
ColorScale::step(float value)
{
    int		i = _colors.size();

    while (i > 0 && _colors[i-1]->min() > value)
    	i--;

    if (i == 0)
	return *(_colors[0]);
    return *(_colors[i-1]);
}

QTextStream&
operator<<(QTextStream &os, const ColorScale &rhs)
{
    int		i;

    if (rhs._colors.size() > 0) {
        os << '[' << rhs[0].min();
	for (i = 1; i < rhs.numSteps(); i++)
	    os << ", " << rhs[i].min();
	os << ']';
    }
    else {
    	os << "empty";
    }

    return os;
}