Color.cpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
26// Headers
28#include <SFML/Graphics/Color.hpp>
29#include <algorithm>
30
31
32namespace sf
33{
35// Static member data
37const Color Color::Black(0, 0, 0);
38const Color Color::White(255, 255, 255);
39const Color Color::Red(255, 0, 0);
40const Color Color::Green(0, 255, 0);
41const Color Color::Blue(0, 0, 255);
42const Color Color::Yellow(255, 255, 0);
43const Color Color::Magenta(255, 0, 255);
44const Color Color::Cyan(0, 255, 255);
45
46
51r(0),
52g(0),
53b(0),
54a(255)
55{
56
57}
58
59
63Color::Color(Uint8 R, Uint8 G, Uint8 B, Uint8 A) :
64r(R),
65g(G),
66b(B),
67a(A)
68{
69
70}
71
72
77{
78 r = static_cast<Uint8>(std::min(r + Other.r, 255));
79 g = static_cast<Uint8>(std::min(g + Other.g, 255));
80 b = static_cast<Uint8>(std::min(b + Other.b, 255));
81 a = static_cast<Uint8>(std::min(a + Other.a, 255));
82
83 return *this;
84}
85
86
91{
92 r = static_cast<Uint8>(r * Other.r / 255);
93 g = static_cast<Uint8>(g * Other.g / 255);
94 b = static_cast<Uint8>(b * Other.b / 255);
95 a = static_cast<Uint8>(a * Other.a / 255);
96
97 return *this;
98}
99
100
104bool Color::operator ==(const Color& Other) const
105{
106 return (r == Other.r) && (g == Other.g) && (b == Other.b) && (a == Other.a);
107}
108
109
113bool Color::operator !=(const Color& Other) const
114{
115 return (r != Other.r) || (g != Other.g) || (b != Other.b) || (a != Other.a);
116}
117
118
122Color operator +(const Color& Color1, const Color& Color2)
123{
124 Color c = Color1;
125 c += Color2;
126
127 return c;
128}
129
130
134Color operator *(const Color& Color1, const Color& Color2)
135{
136 Color c = Color1;
137 c *= Color2;
138
139 return c;
140}
141
142} // namespace sf
Color is an utility class for manipulating 32-bits RGBA colors.
Definition Color.hpp:41
Color & operator*=(const Color &Other)
Operator *= overload to modulate a color.
Definition Color.cpp:90
static const Color Red
Red predefined color.
Definition Color.hpp:106
static const Color White
White predefined color.
Definition Color.hpp:105
Uint8 a
Alpha (transparency) component.
Definition Color.hpp:119
Uint8 g
Green component.
Definition Color.hpp:117
static const Color Cyan
Cyan predefined color.
Definition Color.hpp:111
Uint8 b
Blue component.
Definition Color.hpp:118
Uint8 r
Red component.
Definition Color.hpp:116
static const Color Magenta
Magenta predefined color.
Definition Color.hpp:110
static const Color Black
Black predefined color.
Definition Color.hpp:104
static const Color Green
Green predefined color.
Definition Color.hpp:107
bool operator==(const Color &Other) const
Compare two colors (for equality).
Definition Color.cpp:104
static const Color Blue
Blue predefined color.
Definition Color.hpp:108
Color & operator+=(const Color &Other)
Operator += overload to add a color.
Definition Color.cpp:76
Color()
Default constructor.
Definition Color.cpp:50
bool operator!=(const Color &Other) const
Compare two colors (for difference).
Definition Color.cpp:113
static const Color Yellow
Yellow predefined color.
Definition Color.hpp:109