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