xref: /trunk/main/basebmp/inc/basebmp/color.hxx (revision 48cdb363)
1*48cdb363SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*48cdb363SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*48cdb363SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*48cdb363SAndrew Rist  * distributed with this work for additional information
6*48cdb363SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*48cdb363SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*48cdb363SAndrew Rist  * "License"); you may not use this file except in compliance
9*48cdb363SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*48cdb363SAndrew Rist  *
11*48cdb363SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*48cdb363SAndrew Rist  *
13*48cdb363SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*48cdb363SAndrew Rist  * software distributed under the License is distributed on an
15*48cdb363SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*48cdb363SAndrew Rist  * KIND, either express or implied.  See the License for the
17*48cdb363SAndrew Rist  * specific language governing permissions and limitations
18*48cdb363SAndrew Rist  * under the License.
19*48cdb363SAndrew Rist  *
20*48cdb363SAndrew Rist  *************************************************************/
21*48cdb363SAndrew Rist 
22*48cdb363SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef INCLUDED_BASEBMP_COLOR_HXX
25cdf0e10cSrcweir #define INCLUDED_BASEBMP_COLOR_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <sal/types.h>
28cdf0e10cSrcweir #include <rtl/math.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir namespace basebmp
31cdf0e10cSrcweir {
32cdf0e10cSrcweir 
33cdf0e10cSrcweir class Color
34cdf0e10cSrcweir {
35cdf0e10cSrcweir private:
36cdf0e10cSrcweir 	sal_uInt32			mnColor;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir public:
39cdf0e10cSrcweir     typedef sal_uInt32  value_type;
40cdf0e10cSrcweir     typedef sal_uInt8   component_type;
41cdf0e10cSrcweir 
Color()42cdf0e10cSrcweir     Color() : mnColor(0) {}
Color(sal_uInt32 nVal)43cdf0e10cSrcweir     explicit Color( sal_uInt32 nVal ) : mnColor(nVal) {}
Color(sal_uInt8 nRed,sal_uInt8 nGreen,sal_uInt8 nBlue)44cdf0e10cSrcweir     Color( sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue ) :
45cdf0e10cSrcweir         mnColor( ((sal_uInt32)nRed << 16) | ((sal_uInt32)nGreen << 8) | nBlue )
46cdf0e10cSrcweir     {}
47cdf0e10cSrcweir 
setRed(sal_uInt8 nRed)48cdf0e10cSrcweir 	void setRed( sal_uInt8 nRed ) { mnColor &= ~0x00FF0000UL; mnColor |= (sal_uInt32)nRed << 16; }
setGreen(sal_uInt8 nGreen)49cdf0e10cSrcweir 	void setGreen( sal_uInt8 nGreen ) { mnColor &= ~0x0000FF00UL; mnColor |= (sal_uInt32)nGreen << 8; }
setBlue(sal_uInt8 nBlue)50cdf0e10cSrcweir 	void setBlue( sal_uInt8 nBlue ) { mnColor &= ~0x000000FFUL; mnColor |= nBlue; }
51cdf0e10cSrcweir 
setGrey(sal_uInt8 nGreyVal)52cdf0e10cSrcweir 	void setGrey( sal_uInt8 nGreyVal ) { mnColor = (sal_uInt32)nGreyVal << 16 | (sal_uInt32)nGreyVal << 8 | nGreyVal; }
53cdf0e10cSrcweir 
getRed() const54cdf0e10cSrcweir     sal_uInt8 getRed() const   { return 0xFF & (sal_uInt8)(mnColor >> 16); }
getGreen() const55cdf0e10cSrcweir     sal_uInt8 getGreen() const { return 0xFF & (sal_uInt8)(mnColor >> 8); }
getBlue() const56cdf0e10cSrcweir     sal_uInt8 getBlue() const  { return 0xFF & (sal_uInt8)mnColor; }
57cdf0e10cSrcweir 
getGreyscale() const58cdf0e10cSrcweir     sal_uInt8 getGreyscale() const { return (sal_uInt8)((getBlue()*28UL +
59cdf0e10cSrcweir                                                          getGreen()*151 +
60cdf0e10cSrcweir                                                          getRed()*77) / 256); }
61cdf0e10cSrcweir 
toInt32() const62cdf0e10cSrcweir     sal_uInt32 toInt32() const { return mnColor; }
63cdf0e10cSrcweir 
operator !() const64cdf0e10cSrcweir     bool  operator!() const { return mnColor == 0; }
operator &(sal_uInt32 nMask) const65cdf0e10cSrcweir     Color operator&( sal_uInt32 nMask ) const { return Color(mnColor & nMask); }
operator ^(Color col) const66cdf0e10cSrcweir     Color operator^( Color col ) const { return Color(col.getRed()^getRed(),
67cdf0e10cSrcweir                                                       col.getGreen()^getGreen(),
68cdf0e10cSrcweir                                                       col.getBlue()^getBlue()); }
operator -(Color col) const69cdf0e10cSrcweir     Color operator-( Color col ) const { return Color((sal_uInt8)abs((int)getRed()-col.getRed()),
70cdf0e10cSrcweir                                                       (sal_uInt8)abs((int)getGreen()-col.getGreen()),
71cdf0e10cSrcweir                                                       (sal_uInt8)abs((int)getBlue()-col.getBlue())); }
operator +(Color col) const72cdf0e10cSrcweir     Color operator+( Color col ) const { return Color(getRed()+col.getRed(),
73cdf0e10cSrcweir                                                       getGreen()+col.getGreen(),
74cdf0e10cSrcweir                                                       getBlue()+col.getBlue()); }
operator *(Color col) const75cdf0e10cSrcweir     Color operator*( Color col ) const { return Color((sal_uInt8)((sal_uInt32)col.getRed()*getRed()/SAL_MAX_UINT8),
76cdf0e10cSrcweir                                                       (sal_uInt8)((sal_uInt32)col.getGreen()*getGreen()/SAL_MAX_UINT8),
77cdf0e10cSrcweir                                                       (sal_uInt8)((sal_uInt32)col.getBlue()*getBlue()/SAL_MAX_UINT8)); }
operator *(sal_uInt8 n) const78cdf0e10cSrcweir     Color operator*( sal_uInt8 n ) const { return Color((sal_uInt8)((sal_uInt32)n*getRed()/SAL_MAX_UINT8),
79cdf0e10cSrcweir                                                         (sal_uInt8)((sal_uInt32)n*getGreen()/SAL_MAX_UINT8),
80cdf0e10cSrcweir                                                         (sal_uInt8)((sal_uInt32)n*getBlue()/SAL_MAX_UINT8)); }
operator *(double n) const81cdf0e10cSrcweir     Color operator*( double n ) const { return Color((sal_uInt8)(n*getRed()+.5),
82cdf0e10cSrcweir                                                      (sal_uInt8)(n*getGreen()+.5),
83cdf0e10cSrcweir                                                      (sal_uInt8)(n*getBlue()+.5)); }
operator ==(const Color & rhs) const84cdf0e10cSrcweir     bool operator==( const Color& rhs ) const { return (getRed()==rhs.getRed() &&
85cdf0e10cSrcweir                                                         getGreen()==rhs.getGreen() &&
86cdf0e10cSrcweir                                                         getBlue()==rhs.getBlue()); }
operator !=(const Color & rhs) const87cdf0e10cSrcweir     bool operator!=( const Color& rhs ) const { return !(*this==rhs); }
magnitude() const88cdf0e10cSrcweir     double magnitude() const { return sqrt((double)getRed()*getRed()
89cdf0e10cSrcweir                                            + getGreen()*getGreen()
90cdf0e10cSrcweir                                            + getBlue()*getBlue()); }
91cdf0e10cSrcweir };
92cdf0e10cSrcweir 
93cdf0e10cSrcweir } // namespace vigra
94cdf0e10cSrcweir 
95cdf0e10cSrcweir #endif /* INCLUDED_BASEBMP_COLOR_HXX */
96