source: NonGTP/FCollada/FMath/FMColor.h @ 964

Revision 964, 3.4 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*
2        Copyright (C) 2005-2006 Feeling Software Inc.
3        MIT License: http://www.opensource.org/licenses/mit-license.php
4*/
5
6/**
7        @file FMColor.h
8        The file containing the class and global functions for RBGA colors.
9*/
10
11#ifndef _FM_COLOR_H_
12#define _FM_COLOR_H_
13
14/**
15        A RBGA color.
16        Not used within FCollada.
17
18        @ingroup FMath
19*/
20class FCOLLADA_EXPORT FMColor
21{
22public:
23        uint8 r;        /**< The red component. */
24        uint8 g;        /**< The green component. */
25        uint8 b;        /**< The blue component. */
26        uint8 a;        /**< The alpha component. */
27
28        /**
29         * Creates an empty FMColor.
30         *
31         * The default values are non deterministic.
32         */
33        FMColor() {}
34
35        /**
36         * Creates the FMColor with the coordinates values.
37         *
38         * The first three coordinates are taken from the FMVector3, where the
39         * first one is the x value, the second is that y, and the third is the z.
40         * The forth value is the \c float specified.
41         *
42         *
43         * @param _r The red value.
44         * @param _g The green value.
45         * @param _b The blue value.
46         * @param _a The alpha value.
47         */
48        FMColor(uint8 _r, uint8 _g, uint8 _b, uint8 _a) { r = _r; g = _g; b = _b; a = _a; }
49
50        /**
51         * Creates the FMColor from a color encoded into a uint32.
52         *
53         * The most significant byte makes up the red value. The second most
54         * significant makes up the green value, the third the blue, and the forth
55         * the alpha.
56         *
57         * @param hex The uint to decode the color values from.
58         */
59        FMColor(uint32 hex) { r = uint8((hex & 0xFF000000) >> 24); g = uint8((hex & 0xFF0000) >> 16); b = uint8((hex & 0xFF00) >> 8); a = uint8(hex & 0xFF); }
60
61        /**
62         * Creates the FMColor from a list of \c floats.
63         *
64         * It creates the FMColor with the values specified in the \c floats, which
65         * range from 0.0f to 1.0f.
66         *
67         * \a componentCount is used to determined how many values to take from the
68         * \c float array. If there are insufficient values, then it will give the
69         * remaining values default values. The default values are 0 for the colors
70         * and 255 for the alpha. It fills in the values in this order: red, green,
71         * blue, alpha.
72         *
73         * @param components The \c float array to get values from.
74         * @param componentCount The amount of \c float to take from the array.
75         */
76        FMColor(const float* components, uint32 componentCount);
77
78        /**
79         * Get this FMColor as an array of \c floats.
80         *
81         * It fills the first \a componentCount elements (max 4) of \c components
82         * with the red, green, blue, alpha values of this FMColor in that order.
83         *
84         * @param components The \c float array to fill.
85         * @param componentCount The amount of \c float to fill into the array.
86         */
87        void ToFloats(float* components, uint32 componentCount);
88
89        /**
90         * Get the average of the three color values of this FMColor.
91         *
92         * @return The averages of the three colors values of this FMColor.
93         */
94        inline uint8 ComponentAverage() { return uint8((uint32(r) + uint32(g) + uint32(b)) / 3); }
95
96        /**
97         * Get this FMColor as an array of uint8s.
98         *
99         * @return The \c uint8 array.
100         */
101        operator uint8*() { return &b; }
102};
103
104/**
105 * Multiplication of a scalar with the FMColor.
106 *
107 * @param s The scalar to multiply by.
108 * @param c The FMColor to multiply with.
109 * @return the FMColor representing the resulting color.
110 */
111inline FMColor operator*(float s, const FMColor& c) { return FMColor(uint8(c.r * s), uint8(c.g * s), uint8(c.b * s), uint8(c.a * s)); }
112
113#endif // _FM_COLOR_H_
Note: See TracBrowser for help on using the repository browser.