| 1 | // SuperTux |
| 2 | // Copyright (C) 2006 Matthias Braun <matze@braunis.de> |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | #include "video/color.hpp" |
| 18 | |
| 19 | #include <assert.h> |
| 20 | |
| 21 | const Color Color::BLACK(0.0, 0.0, 0.0); |
| 22 | const Color Color::RED(1.0, 0.0, 0.0); |
| 23 | const Color Color::GREEN(0.0, 1.0, 0.0); |
| 24 | const Color Color::BLUE(0.0, 0.0, 1.0); |
| 25 | const Color Color::CYAN(0.0, 1.0, 1.0); |
| 26 | const Color Color::MAGENTA(1.0, 0.0, 1.0); |
| 27 | const Color Color::YELLOW(1.0, 1.0, 0.0); |
| 28 | const Color Color::WHITE(1.0, 1.0, 1.0); |
| 29 | |
| 30 | Color::Color() : |
| 31 | red(0), |
| 32 | green(0), |
| 33 | blue(0), |
| 34 | alpha(1.0f) |
| 35 | {} |
| 36 | |
| 37 | Color::Color(float red_, float green_, float blue_, float alpha_) : |
| 38 | red(red_), |
| 39 | green(green_), |
| 40 | blue(blue_), |
| 41 | alpha(alpha_) |
| 42 | { |
| 43 | assert(0 <= red && red <= 1.0f); |
| 44 | assert(0 <= green && green <= 1.0f); |
| 45 | assert(0 <= blue && blue <= 1.0f); |
| 46 | } |
| 47 | |
| 48 | Color::Color(const std::vector<float>& vals) : |
| 49 | red(), |
| 50 | green(), |
| 51 | blue(), |
| 52 | alpha() |
| 53 | { |
| 54 | if (vals.size() < 3) { |
| 55 | red = 0; |
| 56 | green = 0; |
| 57 | blue = 0; |
| 58 | alpha = 0; |
| 59 | return; |
| 60 | } |
| 61 | red = vals[0]; |
| 62 | green = vals[1]; |
| 63 | blue = vals[2]; |
| 64 | if (vals.size() > 3) |
| 65 | alpha = vals[3]; |
| 66 | else |
| 67 | alpha = 1.0; |
| 68 | assert(0 <= red && red <= 1.0f); |
| 69 | assert(0 <= green && green <= 1.0f); |
| 70 | assert(0 <= blue && blue <= 1.0f); |
| 71 | } |
| 72 | |
| 73 | bool |
| 74 | Color::operator==(const Color& other) const |
| 75 | { |
| 76 | return red == other.red && green == other.green && blue == other.blue |
| 77 | && alpha == other.alpha; |
| 78 | } |
| 79 | |
| 80 | bool |
| 81 | Color::operator!=(const Color& other) const |
| 82 | { |
| 83 | return !(operator==(other)); |
| 84 | } |
| 85 | |
| 86 | float |
| 87 | Color::greyscale() const |
| 88 | { |
| 89 | return red * 0.30f + green * 0.59f + blue * 0.11f; |
| 90 | } |
| 91 | |
| 92 | bool |
| 93 | Color::operator < (const Color& other) const |
| 94 | { |
| 95 | return greyscale() < other.greyscale(); |
| 96 | } |
| 97 | |
| 98 | std::vector<float> |
| 99 | Color::toVector() |
| 100 | { |
| 101 | std::vector<float> result; |
| 102 | result.clear(); |
| 103 | result.push_back(red); |
| 104 | result.push_back(green); |
| 105 | result.push_back(blue); |
| 106 | result.push_back(alpha); |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | /* EOF */ |
| 111 | |