fix typo, add instructions about lowercase named strings
[gnash.git] / libcore / RGBA.h
blobd133552ffcad9e961204594c245f08fbc95f002c
1 // RGBA.h: RGBA color handling.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 //
21 #ifndef GNASH_RGBA_H
22 #define GNASH_RGBA_H
24 #include "SWF.h"
26 #include <string>
27 #include <boost/cstdint.hpp>
29 namespace gnash {
30 class SWFStream;
33 namespace gnash {
35 /// A basic RGBA type
37 /// This both represents a SWF RGBA record and is a basic Gnash type for
38 /// color values.
39 class rgba
41 public:
43 /// Construct default RGBA value.
45 /// Default value is 0xffffffff (solid white).
46 rgba()
48 m_r(255),
49 m_g(255),
50 m_b(255),
51 m_a(255)
54 /// Construct an RGBA with the provided values
56 /// @param r Red
57 /// @param g Green
58 /// @param b Blue
59 /// @param a Alpha (transparency)
60 rgba(boost::uint8_t r, boost::uint8_t g, boost::uint8_t b,
61 boost::uint8_t a)
63 m_r(r),
64 m_g(g),
65 m_b(b),
66 m_a(a)
70 /// Parse a 32-bit unsigned integer as three packed R,G,B bytes.
72 /// Alpha will be untouched.
73 /// Blue is the least significant byte.
74 ///
75 /// This function is meant to be used to
76 /// parse ActionScript colors in numeric format.
77 void parseRGB(boost::uint32_t rgbCol) {
78 m_r = static_cast<boost::uint8_t>(rgbCol >> 16);
79 m_g = static_cast<boost::uint8_t>(rgbCol >> 8);
80 m_b = static_cast<boost::uint8_t>(rgbCol);
83 /// Return a 32-bit unsigned integer as four packed R,G,B bytes.
85 /// Blue is the least significant byte. The most significant (alpha)
86 /// byte is unused.
87 ///
88 /// This function is meant to be used to output ActionScript colors
89 /// in numeric format.
90 boost::uint32_t toRGB() const {
91 return (m_r << 16) + (m_g << 8) + m_b;
94 /// Return a 32-bit unsigned integer as four packed A,R,G,B bytes.
96 /// Blue is the least significant byte.
97 ///
98 /// This function is meant to be used to output ActionScript colors
99 /// in numeric format.
100 boost::uint32_t toRGBA() const {
101 return toRGB() + (m_a << 24);
104 /// Set r, g, b, a values
105 void set(boost::uint8_t r, boost::uint8_t g, boost::uint8_t b,
106 boost::uint8_t a) {
107 m_r = r;
108 m_g = g;
109 m_b = b;
110 m_a = a;
113 /// Used for morphing.
114 void set_lerp(const rgba& a, const rgba& b, float f);
116 /// Neater string output (example: "0,0,0,255")
117 std::string toShortString() const;
119 friend std::ostream& operator<< (std::ostream& os, const rgba& r);
121 bool operator==(const rgba& o) const {
122 return m_r == o.m_r &&
123 m_g == o.m_g &&
124 m_b == o.m_b &&
125 m_a == o.m_a;
128 bool operator!=(const rgba& o) const {
129 return !(*this == o);
132 boost::uint8_t m_r, m_g, m_b, m_a;
136 std::ostream& operator<< (std::ostream& os, const rgba& r);
138 /// Initialize from input stream (reads RGBA)
140 /// Throw a ParserException if there's no enough bytes in the
141 /// currently opened tag for reading. See SWFStream::ensureBytes()
142 rgba readRGBA(SWFStream& in);
144 /// Initialize from intput stream (reads RGB)
145 rgba readRGB(SWFStream& in);
147 /// Create an RGBA value from a hex string (e.g. FF0000)
149 /// @param color A hex string in 'rrbbgg' format. This must contain only
150 /// a valid hexadecimal number. It is the caller's
151 /// responsibility to check it.
152 rgba colorFromHexString(const std::string& color);
155 } // namespace gnash
157 #endif
160 // Local Variables:
161 // mode: C++
162 // indent-tabs-mode: t
163 // End: