LCOV - code coverage report
Current view: top level - libreoffice/basegfx/inc/basegfx/color - bcolor.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 6 25 24.0 %
Date: 2012-12-27 Functions: 4 11 36.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #ifndef _BGFX_COLOR_BCOLOR_HXX
      21             : #define _BGFX_COLOR_BCOLOR_HXX
      22             : 
      23             : #include <basegfx/tuple/b3dtuple.hxx>
      24             : #include <com/sun/star/uno/Reference.hxx>
      25             : #include <com/sun/star/uno/Sequence.hxx>
      26             : #include <vector>
      27             : #include <basegfx/basegfxdllapi.h>
      28             : 
      29             : //////////////////////////////////////////////////////////////////////////////
      30             : // predeclarations
      31             : 
      32             : namespace com { namespace sun { namespace star { namespace rendering {
      33             :     class XGraphicDevice;
      34             : }}}}
      35             : 
      36             : //////////////////////////////////////////////////////////////////////////////
      37             : 
      38             : namespace basegfx
      39             : {
      40             :     /** Base Color class with three double values
      41             : 
      42             :         This class derives all operators and common handling for
      43             :         a 3D data class from B3DTuple. All necessary extensions
      44             :         which are special for colors will be added here.
      45             : 
      46             :         @see B3DTuple
      47             :     */
      48           0 :     class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColor : public B3DTuple
      49             :     {
      50             :     public:
      51             :         /** Create a Color with red, green and blue components from [0.0 to 1.0]
      52             : 
      53             :             The color is initialized to (0.0, 0.0, 0.0)
      54             :         */
      55           0 :         BColor()
      56           0 :         :   B3DTuple()
      57           0 :         {}
      58             : 
      59             :         /** Create a 3D Color
      60             : 
      61             :             @param fRed
      62             :             @param fGreen
      63             :             @param fBlue
      64             :             These parameters are used to initialize the red, green and blue intensities of the color
      65             :         */
      66          91 :         BColor(double fRed, double fGreen, double fBlue)
      67          91 :         :   B3DTuple(fRed, fGreen, fBlue)
      68          91 :         {}
      69             : 
      70             :         /** Create a 3D Color
      71             : 
      72             :             @param fLuminosity
      73             :             The parameter is used to initialize the red, green and blue intensities of the color
      74             :         */
      75             :         explicit BColor(double fLuminosity)
      76             :         :   B3DTuple(fLuminosity, fLuminosity, fLuminosity)
      77             :         {}
      78             : 
      79             :         /** Create a copy of a Color
      80             : 
      81             :             @param rVec
      82             :             The Color which will be copied.
      83             :         */
      84           0 :         BColor(const BColor& rVec)
      85           0 :         :   B3DTuple(rVec)
      86           0 :         {}
      87             : 
      88             :         /** constructor with tuple to allow copy-constructing
      89             :             from B3DTuple-based classes
      90             :         */
      91           0 :         BColor(const ::basegfx::B3DTuple& rTuple)
      92           0 :         :   B3DTuple(rTuple)
      93           0 :         {}
      94             : 
      95           0 :         ~BColor()
      96           0 :         {}
      97             : 
      98             :         // data access read
      99          91 :         double getRed() const { return mfX; }
     100          91 :         double getGreen() const { return mfY; }
     101          91 :         double getBlue() const { return mfZ; }
     102             : 
     103             :         // data access write
     104             :         void setRed(double fNew) { mfX = fNew; }
     105             :         void setGreen(double fNew) { mfY = fNew; }
     106             :         void setBlue(double fNew) { mfZ = fNew; }
     107             : 
     108             :         /** *=operator to allow usage from BColor, too
     109             :         */
     110             :         BColor& operator*=( const BColor& rPnt )
     111             :         {
     112             :             mfX *= rPnt.mfX;
     113             :             mfY *= rPnt.mfY;
     114             :             mfZ *= rPnt.mfZ;
     115             :             return *this;
     116             :         }
     117             : 
     118             :         /** *=operator to allow usage from BColor, too
     119             :         */
     120             :         BColor& operator*=(double t)
     121             :         {
     122             :             mfX *= t;
     123             :             mfY *= t;
     124             :             mfZ *= t;
     125             :             return *this;
     126             :         }
     127             : 
     128             :         /** assignment operator to allow assigning the results
     129             :             of B3DTuple calculations
     130             :         */
     131             :         BColor& operator=( const ::basegfx::B3DTuple& rVec )
     132             :         {
     133             :             mfX = rVec.getX();
     134             :             mfY = rVec.getY();
     135             :             mfZ = rVec.getZ();
     136             :             return *this;
     137             :         }
     138             : 
     139             :         // blend to another color using luminance
     140             :         void blend(const BColor& rColor)
     141             :         {
     142             :             const double fLuminance(luminance());
     143             :             mfX = rColor.getRed() * fLuminance;
     144             :             mfY = rColor.getGreen() * fLuminance;
     145             :             mfZ = rColor.getBlue() * fLuminance;
     146             :         }
     147             : 
     148             :         // luminance
     149           0 :         double luminance() const
     150             :         {
     151           0 :             const double fRedWeight(77.0 / 256.0);      // 0.30
     152           0 :             const double fGreenWeight(151.0 / 256.0);   // 0.59
     153           0 :             const double fBlueWeight(28.0 / 256.0);     // 0.11
     154             : 
     155           0 :             return (mfX * fRedWeight + mfY * fGreenWeight + mfZ * fBlueWeight);
     156             :         }
     157             : 
     158             :         // distances in color space
     159             :         double getDistanceRed(const BColor& rColor) const { return (getRed() > rColor.getRed() ? getRed() - rColor.getRed() : rColor.getRed() - getRed()); }
     160             :         double getDistanceGreen(const BColor& rColor) const { return (getGreen() > rColor.getGreen() ? getGreen() - rColor.getGreen() : rColor.getGreen() - getGreen()); }
     161             :         double getDistanceBlue(const BColor& rColor) const { return (getBlue() > rColor.getBlue() ? getBlue() - rColor.getBlue() : rColor.getBlue() - getBlue()); }
     162             : 
     163             :         double getDistance(const BColor& rColor) const
     164             :         {
     165             :             const double fDistR(getDistanceRed(rColor));
     166             :             const double fDistG(getDistanceGreen(rColor));
     167             :             const double fDistB(getDistanceBlue(rColor));
     168             : 
     169             :             return sqrt(fDistR * fDistR + fDistG * fDistG + fDistB * fDistB);
     170             :         }
     171             : 
     172             :         double getMinimumDistance(const BColor& rColor) const
     173             :         {
     174             :             const double fDistR(getDistanceRed(rColor));
     175             :             const double fDistG(getDistanceGreen(rColor));
     176             :             const double fDistB(getDistanceBlue(rColor));
     177             : 
     178             :             double fRetval(fDistR < fDistG ? fDistR : fDistG);
     179             :             return (fRetval < fDistB ? fRetval : fDistB);
     180             :         }
     181             : 
     182             :         double getMaximumDistance(const BColor& rColor) const
     183             :         {
     184             :             const double fDistR(getDistanceRed(rColor));
     185             :             const double fDistG(getDistanceGreen(rColor));
     186             :             const double fDistB(getDistanceBlue(rColor));
     187             : 
     188             :             double fRetval(fDistR > fDistG ? fDistR : fDistG);
     189             :             return (fRetval > fDistB ? fRetval : fDistB);
     190             :         }
     191             : 
     192             :         // clamp color to [0.0..1.0] values in all three intensity components
     193             :         void clamp()
     194             :         {
     195             :             mfX = basegfx::clamp(mfX, 0.0, 1.0);
     196             :             mfY = basegfx::clamp(mfY, 0.0, 1.0);
     197             :             mfZ = basegfx::clamp(mfZ, 0.0, 1.0);
     198             :         }
     199             : 
     200             :         void invert()
     201             :         {
     202             :             mfX = 1.0 - mfX;
     203             :             mfY = 1.0 - mfY;
     204             :             mfZ = 1.0 - mfZ;
     205             :         }
     206             : 
     207           0 :         static const BColor& getEmptyBColor()
     208             :         {
     209           0 :             return (const BColor&) ::basegfx::B3DTuple::getEmptyTuple();
     210             :         }
     211             : 
     212             :         com::sun::star::uno::Sequence< double > colorToDoubleSequence(const com::sun::star::uno::Reference< com::sun::star::rendering::XGraphicDevice >& /*xGraphicDevice*/) const
     213             :         {
     214             :             com::sun::star::uno::Sequence< double > aRet(4);
     215             :             double* pRet = aRet.getArray();
     216             : 
     217             :             pRet[0] = mfX;
     218             :             pRet[1] = mfY;
     219             :             pRet[2] = mfZ;
     220             :             pRet[3] = 1.0;
     221             : 
     222             :             return aRet;
     223             :         }
     224             :     };
     225             : } // end of namespace basegfx
     226             : 
     227             : #endif /* _BGFX_COLOR_BCOLOR_HXX */
     228             : 
     229             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10