Renderer, ...: use PixelRect::GetCenter()
[xcsoar.git] / src / Screen / Ramp.cpp
blob481f1f285f75639632c71be282e7d9c14d874803
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "Screen/Ramp.hpp"
25 #include "Color.hpp"
27 #include <assert.h>
28 #include <stddef.h>
30 Color
31 ColorRampLookup(const short h,
32 const ColorRamp* ramp_colors,
33 const int numramp,
34 const unsigned char interp_levels)
36 assert(ramp_colors != NULL);
37 assert(numramp >= 2);
39 unsigned short f, of;
40 unsigned short is = 1<<interp_levels;
42 // gone past end, so use last color
43 if (h >= ramp_colors[numramp - 1].h) {
44 return Color(ramp_colors[numramp-1].r,
45 ramp_colors[numramp-1].g,
46 ramp_colors[numramp-1].b);
48 for (int i = numramp - 2; i >= 0; i--) {
49 assert(ramp_colors[i].h < ramp_colors[i + 1].h);
51 if (h >= ramp_colors[i].h) {
52 if (interp_levels) {
53 f = (unsigned short)(h - ramp_colors[i].h) * is
54 / (unsigned short)(ramp_colors[i + 1].h - ramp_colors[i].h);
55 of = is - f;
57 return Color((f * ramp_colors[i + 1].r + of * ramp_colors[i].r) >> interp_levels,
58 (f * ramp_colors[i + 1].g + of * ramp_colors[i].g) >> interp_levels,
59 (f * ramp_colors[i + 1].b + of * ramp_colors[i].b) >> interp_levels);
60 } else {
61 return Color(ramp_colors[i].r, ramp_colors[i].g, ramp_colors[i].b);
66 // check if h lower than lowest
67 assert(h <= ramp_colors[0].h);
69 return Color(ramp_colors[0].r, ramp_colors[0].g, ramp_colors[0].b);