LCOV - code coverage report
Current view: top level - include/basegfx/color - bcolor.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 60 70 85.7 %
Date: 2015-06-13 12:38:46 Functions: 23 25 92.0 %
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 INCLUDED_BASEGFX_COLOR_BCOLOR_HXX
      21             : #define INCLUDED_BASEGFX_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             : namespace com { namespace sun { namespace star { namespace rendering {
      30             :     class XGraphicDevice;
      31             : }}}}
      32             : 
      33             : namespace basegfx
      34             : {
      35             :     /** Base Color class with three double values
      36             : 
      37             :         This class derives all operators and common handling for
      38             :         a 3D data class from B3DTuple. All necessary extensions
      39             :         which are special for colors will be added here.
      40             : 
      41             :         @see B3DTuple
      42             :     */
      43     3085979 :     class BASEGFX_DLLPUBLIC SAL_WARN_UNUSED BColor : public B3DTuple
      44             :     {
      45             :     public:
      46             :         /** Create a Color with red, green and blue components from [0.0 to 1.0]
      47             : 
      48             :             The color is initialized to (0.0, 0.0, 0.0)
      49             :         */
      50      499766 :         BColor()
      51      499766 :         :   B3DTuple()
      52      499766 :         {}
      53             : 
      54             :         /** Create a 3D Color
      55             : 
      56             :             @param fRed
      57             :             @param fGreen
      58             :             @param fBlue
      59             :             These parameters are used to initialize the red, green and blue intensities of the color
      60             :         */
      61      827653 :         BColor(double fRed, double fGreen, double fBlue)
      62      827653 :         :   B3DTuple(fRed, fGreen, fBlue)
      63      827653 :         {}
      64             : 
      65             :         /** Create a 3D Color
      66             : 
      67             :             @param fLuminosity
      68             :             The parameter is used to initialize the red, green and blue intensities of the color
      69             :         */
      70         912 :         explicit BColor(double fLuminosity)
      71         912 :         :   B3DTuple(fLuminosity, fLuminosity, fLuminosity)
      72         912 :         {}
      73             : 
      74             :         /** Create a copy of a Color
      75             : 
      76             :             @param rVec
      77             :             The Color which will be copied.
      78             :         */
      79     2287441 :         BColor(const BColor& rVec)
      80     2287441 :         :   B3DTuple(rVec)
      81     2287441 :         {}
      82             : 
      83             :         /** constructor with tuple to allow copy-constructing
      84             :             from B3DTuple-based classes
      85             :         */
      86       44318 :         BColor(const ::basegfx::B3DTuple& rTuple)
      87       44318 :         :   B3DTuple(rTuple)
      88       44318 :         {}
      89             : 
      90     3654384 :         ~BColor()
      91     3654384 :         {}
      92             : 
      93             :         // data access read
      94     3890998 :         double getRed() const { return mfX; }
      95     3890998 :         double getGreen() const { return mfY; }
      96     3891004 :         double getBlue() const { return mfZ; }
      97             : 
      98             :         // data access write
      99      371853 :         void setRed(double fNew) { mfX = fNew; }
     100      371853 :         void setGreen(double fNew) { mfY = fNew; }
     101      371859 :         void setBlue(double fNew) { mfZ = fNew; }
     102             : 
     103             :         /** *=operator to allow usage from BColor, too
     104             :         */
     105      123447 :         BColor& operator*=( const BColor& rPnt )
     106             :         {
     107      123447 :             mfX *= rPnt.mfX;
     108      123447 :             mfY *= rPnt.mfY;
     109      123447 :             mfZ *= rPnt.mfZ;
     110      123447 :             return *this;
     111             :         }
     112             : 
     113             :         /** *=operator to allow usage from BColor, too
     114             :         */
     115           0 :         BColor& operator*=(double t)
     116             :         {
     117           0 :             mfX *= t;
     118           0 :             mfY *= t;
     119           0 :             mfZ *= t;
     120           0 :             return *this;
     121             :         }
     122             : 
     123             :         /** assignment operator to allow assigning the results
     124             :             of B3DTuple calculations
     125             :         */
     126      586403 :         BColor& operator=( const ::basegfx::B3DTuple& rVec )
     127             :         {
     128      586403 :             mfX = rVec.getX();
     129      586403 :             mfY = rVec.getY();
     130      586403 :             mfZ = rVec.getZ();
     131      586403 :             return *this;
     132             :         }
     133             : 
     134             :         // blend to another color using luminance
     135             :         void blend(const BColor& rColor)
     136             :         {
     137             :             const double fLuminance(luminance());
     138             :             mfX = rColor.getRed() * fLuminance;
     139             :             mfY = rColor.getGreen() * fLuminance;
     140             :             mfZ = rColor.getBlue() * fLuminance;
     141             :         }
     142             : 
     143             :         // luminance
     144      367920 :         double luminance() const
     145             :         {
     146      367920 :             const double fRedWeight(77.0 / 256.0);      // 0.30
     147      367920 :             const double fGreenWeight(151.0 / 256.0);   // 0.59
     148      367920 :             const double fBlueWeight(28.0 / 256.0);     // 0.11
     149             : 
     150      367920 :             return (mfX * fRedWeight + mfY * fGreenWeight + mfZ * fBlueWeight);
     151             :         }
     152             : 
     153             :         // distances in color space
     154        8411 :         double getDistanceRed(const BColor& rColor) const { return (getRed() > rColor.getRed() ? getRed() - rColor.getRed() : rColor.getRed() - getRed()); }
     155        8411 :         double getDistanceGreen(const BColor& rColor) const { return (getGreen() > rColor.getGreen() ? getGreen() - rColor.getGreen() : rColor.getGreen() - getGreen()); }
     156        8411 :         double getDistanceBlue(const BColor& rColor) const { return (getBlue() > rColor.getBlue() ? getBlue() - rColor.getBlue() : rColor.getBlue() - getBlue()); }
     157             : 
     158        6606 :         double getDistance(const BColor& rColor) const
     159             :         {
     160        6606 :             const double fDistR(getDistanceRed(rColor));
     161        6606 :             const double fDistG(getDistanceGreen(rColor));
     162        6606 :             const double fDistB(getDistanceBlue(rColor));
     163             : 
     164        6606 :             return sqrt(fDistR * fDistR + fDistG * fDistG + fDistB * fDistB);
     165             :         }
     166             : 
     167             :         double getMinimumDistance(const BColor& rColor) const
     168             :         {
     169             :             const double fDistR(getDistanceRed(rColor));
     170             :             const double fDistG(getDistanceGreen(rColor));
     171             :             const double fDistB(getDistanceBlue(rColor));
     172             : 
     173             :             double fRetval(fDistR < fDistG ? fDistR : fDistG);
     174             :             return (fRetval < fDistB ? fRetval : fDistB);
     175             :         }
     176             : 
     177        1805 :         double getMaximumDistance(const BColor& rColor) const
     178             :         {
     179        1805 :             const double fDistR(getDistanceRed(rColor));
     180        1805 :             const double fDistG(getDistanceGreen(rColor));
     181        1805 :             const double fDistB(getDistanceBlue(rColor));
     182             : 
     183        1805 :             double fRetval(fDistR > fDistG ? fDistR : fDistG);
     184        1805 :             return (fRetval > fDistB ? fRetval : fDistB);
     185             :         }
     186             : 
     187             :         // clamp color to [0.0..1.0] values in all three intensity components
     188     2906380 :         BColor& clamp()
     189             :         {
     190     2906380 :             mfX = basegfx::clamp(mfX, 0.0, 1.0);
     191     2906380 :             mfY = basegfx::clamp(mfY, 0.0, 1.0);
     192     2906380 :             mfZ = basegfx::clamp(mfZ, 0.0, 1.0);
     193     2906380 :             return *this;
     194             :         }
     195             : 
     196           0 :         BColor& invert()
     197             :         {
     198           0 :             mfX = 1.0 - mfX;
     199           0 :             mfY = 1.0 - mfY;
     200           0 :             mfZ = 1.0 - mfZ;
     201           0 :             return *this;
     202             :         }
     203             : 
     204         164 :         static const BColor& getEmptyBColor()
     205             :         {
     206         164 :             return static_cast<const BColor&>( ::basegfx::B3DTuple::getEmptyTuple() );
     207             :         }
     208             : 
     209             :         com::sun::star::uno::Sequence< double > colorToDoubleSequence(const com::sun::star::uno::Reference< com::sun::star::rendering::XGraphicDevice >& /*xGraphicDevice*/) const
     210             :         {
     211             :             com::sun::star::uno::Sequence< double > aRet(4);
     212             :             double* pRet = aRet.getArray();
     213             : 
     214             :             pRet[0] = mfX;
     215             :             pRet[1] = mfY;
     216             :             pRet[2] = mfZ;
     217             :             pRet[3] = 1.0;
     218             : 
     219             :             return aRet;
     220             :         }
     221             :     };
     222             : } // end of namespace basegfx
     223             : 
     224             : #endif // INCLUDED_BASEGFX_COLOR_BCOLOR_HXX
     225             : 
     226             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11