colourmat.cpp

Go to the documentation of this file.
00001 // $Id: colourmat.cpp 1402 2006-07-04 09:26:38Z alex $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 
00099 
00100 #include "camtypes.h"
00101 
00102 #include "colourmat.h"
00103 
00104 /********************************************************************************************
00105 
00106 >   ColourVector ColourVector::Apply(ColourMatrix &cm)
00107 
00108 
00109     Author:     Alex_Bligh <alex@alex.org.uk>
00110     Created:    24/01/2005
00111     Inputs:     cm - Colour matrix to operate on
00112     Outputs:    
00113     Returns:    The matrix multiplied colour vector
00114     Purpose:    Multiplies a ColourVector by a ColourMatrix and returns the result
00115     Errors:     -
00116     SeeAlso:    -
00117 
00118                     ( a1 a2 a3 a4 a5 )
00119                     ( b1 b2 b3 b4 b5 )
00120     ( a b c d e )   ( c1 c2 c3 c4 c5 ) = (a*a1+b*b1+c*c1+d*d1+e*e1, a*a2+b*b2+c*c2+d*d2+e*e2 etc.)
00121                     ( d1 d2 d3 d4 d5 )
00122                     ( e1 e2 e3 e4 e5 )
00123 
00124 ********************************************************************************************/
00125 
00126 ColourVector ColourVector::Apply(ColourMatrix &cm)
00127 {
00128     ColourVector result;
00129     INT32 i, j;
00130 
00131     for (i=0; i<5; i++) // Column of matrix
00132     {
00133         result.elements[i] = 0.0;
00134         for (j=0 ; j<5 ; j++) // Row of matrix
00135         {
00136             result.elements[i]+=elements[j] * cm.elements.el[i+j*5];
00137         }
00138     }
00139     result.elements[Spare]=1.0; // Fix this up in case of rounding peculiarities
00140     return result;
00141 }
00142 
00143 /********************************************************************************************
00144 
00145 >   static ColourMatrix ColourMatrix::Boost(double d=1.2)
00146 
00147 
00148     Author:     Alex_Bligh <alex@alex.org.uk>
00149     Created:    24/01/2005
00150     Inputs:     d - boost quantity
00151     Outputs:    
00152     Returns:    A matrix which boosts colour
00153     Purpose:    -
00154     Errors:     -
00155     SeeAlso:    -
00156 
00157 ********************************************************************************************/
00158 
00159 ColourMatrix ColourMatrix::Boost(double d)
00160 {
00161     ColourMatrix r;
00162     if (d != 1.0)
00163     {
00164         double cc = (1.0-d)/2.0; // Add constant boost to centre colour change
00165         elements_t e = {
00166             { d,   0.0, 0.0, 0.0, 0.0,
00167             0.0, d,   0.0, 0.0, 0.0,
00168             0.0, 0.0, d,   0.0, 0.0,
00169             0.0, 0.0, 0.0, 1.0, 0.0,
00170             cc,  cc,  cc,  0.0, 1.0 }
00171             };
00172         r.elements = e;
00173     }
00174     return r;
00175 }
00176 
00177 /********************************************************************************************
00178 
00179 >   static ColourMatrix ColourMatrix::Whiten()
00180 
00181 
00182     Author:     Alex_Bligh <alex@alex.org.uk>
00183     Created:    24/01/2005
00184     Inputs:     None
00185     Outputs:    
00186     Returns:    A matrix which makes the entire image white
00187     Purpose:    -
00188     Errors:     -
00189     SeeAlso:    -
00190 
00191 ********************************************************************************************/
00192 
00193 ColourMatrix ColourMatrix::Whiten()
00194 {
00195     ColourMatrix r;
00196     elements_t e = {
00197         { 0.0, 0.0, 0.0, 0.0, 0.0,
00198         0.0, 0.0, 0.0, 0.0, 0.0,
00199         0.0, 0.0, 0.0, 0.0, 0.0,
00200         0.0, 0.0, 0.0, 1.0, 0.0,
00201         1.0, 1.0, 1.0, 0.0, 1.0 }
00202         };
00203     r.elements=e;
00204     return r;
00205 }
00206 
00207 /********************************************************************************************
00208 
00209 >   static ColourMatrix ColourMatrix::Grey()
00210 
00211 
00212     Author:     Alex_Bligh <alex@alex.org.uk>
00213     Created:    24/01/2005
00214     Inputs:     d - boost quantity
00215     Outputs:    
00216     Returns:    A matrix which greys the image
00217     Purpose:    -
00218     Errors:     -
00219     SeeAlso:    -
00220 
00221 ********************************************************************************************/
00222 
00223 ColourMatrix ColourMatrix::Grey()
00224 {
00225     ColourMatrix r;
00226     elements_t e = {
00227         { 0.1, 0.1, 0.1, 0.0, 0.0,
00228         0.1, 0.1, 0.1, 0.0, 0.0,
00229         0.1, 0.1, 0.1, 0.0, 0.0,
00230         0.0, 0.0, 0.0, 1.0, 0.0,
00231         0.6, 0.6, 0.6, 0.0, 1.0 }
00232         };
00233     r.elements=e;
00234     return r;
00235 }
00236 
00237 
00238 
00239 
00240 
00241 
00242 #if 0
00243 The control makes a Hot version of a button face from a Normal version at render time if a Hot version was not loaded from the resource system. And the control always makes grey versions using the same techniques:
00244 
00245 
00246 double colourboost = 1.0;
00247 // If glyph is highlighted but button should be normal, knock it back a bit
00248 // Else if glyph is normal but button should be highlighted, boost it up
00249 if ((BBX_HIGHLIGHT_ID(BMPIndex)) && !(ISSELECTED(hwnd) || (flags & BS_POINTEROVER)))
00250     colourboost = 0.8;
00251 else if (!(BBX_HIGHLIGHT_ID(BMPIndex)) && (ISSELECTED(hwnd) || (flags & BS_POINTEROVER)))
00252     colourboost = 1.2;
00253 
00254 PlotGdiPlusBitmap(hDC, pShared->pGPBitmap, SrcOffset,
00255 xPlotOffset, yPlotOffset, XSize, YSize,
00256 ISDISABLED(hwnd), colourboost, BGColour);
00257 
00258 
00259 static void PlotGdiPlusBitmap   (HDC destDC, CGdiPlusBitmap* pBitmap, INT32 SrcOffset,
00260                                 INT32 xPlotOffset,INT32 yPlotOffset, INT32 width, INT32 height,
00261                                 BOOL Shaded, double colourboost, INT32 BGColour)
00262 {
00263     // Always fill the entire background with BTNFACE (grey) to make sure we don't
00264     // miss any pixels - we don't need to be careful about redraw flicker etc
00265     // now that we're plotting into an offscreen bitmap.
00266     
00267     if (pBitmap == NULL)
00268         return;
00269     
00270     Graphics graphics(destDC);
00271     Status stat;
00272     
00273     Gdiplus::Rect faceRect(0, 0, MAXBITMAPSIZE, MAXBITMAPSIZE);
00274     
00275     Brush* pHighlightBrush = NULL;
00276     switch (BGColour)
00277     {
00278         case BGCOLOUR_LTGREY:
00279         {
00280             Color face;
00281             face.SetFromCOLORREF(GetSysColor(COLOR_BTNHIGHLIGHT));
00282             pHighlightBrush = new SolidBrush(face);
00283         }
00284         break;
00285         
00286         case BGCOLOUR_CREAM:
00287         {
00288             pHighlightBrush = new SolidBrush(Color(255, 255, 192));
00289             // Color face;
00290             // face.SetFromCOLORREF(GetSysColor(COLOR_BTNSHADOW));
00291             // pHighlightBrush = new LinearGradientBrush(faceRect, Color(255,255,192), face, LinearGradientModeVertical);
00292         }
00293         break;
00294         
00295         default:
00296         {
00297             Color face;
00298             face.SetFromCOLORREF(GetSysColor(COLOR_BTNFACE));
00299             pHighlightBrush = new SolidBrush(face);
00300         }
00301         break;
00302     }
00303     
00304     graphics.FillRectangle(pHighlightBrush, 0, 0, MAXBITMAPSIZE, MAXBITMAPSIZE);
00305     stat = graphics.GetLastStatus();
00306     
00307     if (Shaded)
00308     {
00309         // Button is shaded (greyed)
00310         // Make a colour transform here to set all pixels to white
00311         ImageAttributes effect;
00312         ColorMatrix whiteMatrix = {
00313         0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
00314         0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
00315         0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
00316         0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
00317         1.0f, 1.0f, 1.0f, 0.0f, 1.0f
00318         };
00319     
00320         effect.SetColorMatrix(&whiteMatrix);
00321         
00322         // Plot white version of glyph first, with slight offset
00323         Gdiplus::Rect destRect(xPlotOffset+1, yPlotOffset+1, width, height);
00324         graphics.DrawImage(pBitmap->m_pBitmap, destRect, SrcOffset, 0, width, height, UnitPixel, &effect);
00325         stat = graphics.GetLastStatus();
00326         
00327         // Make a colour transform here to remove hue from the pixels
00328         ColorMatrix greyMatrix = {
00329         0.1f, 0.1f, 0.1f, 0.0f, 0.0f,
00330         0.1f, 0.1f, 0.1f, 0.0f, 0.0f,
00331         0.1f, 0.1f, 0.1f, 0.0f, 0.0f,
00332         0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
00333         0.6f, 0.6f, 0.6f, 0.0f, 1.0f
00334         };
00335         
00336         effect.SetColorMatrix(&greyMatrix);
00337         
00338         // Plot greyed out glyph over the top
00339         Gdiplus::Rect destRect2(xPlotOffset, yPlotOffset, width, height);
00340         graphics.DrawImage(pBitmap->m_pBitmap, destRect2, SrcOffset, 0, width, height, UnitPixel, &effect);
00341         stat = graphics.GetLastStatus();
00342         
00343     }
00344     else
00345     {
00346         // Button is not shaded but may have it colour boosted or diminished
00347         // to indicate other things...
00348         // Make a colour transform here to apply colour boost
00349         ImageAttributes effect;
00350         if (colourboost!=1.0)
00351         {
00352             float cb = (float)colourboost;
00353             float cc = (1.0f-cb)/2.0f; // Add constant boost to centre colour change
00354             ColorMatrix boostMatrix = {
00355             cb, 0.0f, 0.0f, 0.0f, 0.0f,
00356             0.0f, cb, 0.0f, 0.0f, 0.0f,
00357             0.0f, 0.0f, cb, 0.0f, 0.0f,
00358             0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
00359             cc, cc, cc, 0.0f, 1.0f
00360             };
00361         
00362             effect.SetColorMatrix(&boostMatrix);
00363         }
00364 
00365         Gdiplus::Rect destRect(xPlotOffset, yPlotOffset, width, height);
00366         graphics.DrawImage(pBitmap->m_pBitmap, destRect, SrcOffset, 0, width, height, UnitPixel, &effect);
00367         stat = graphics.GetLastStatus();
00368     }
00369         
00370     delete pHighlightBrush;
00371     pHighlightBrush = NULL;
00372     
00373 }
00374 
00375 Info about GDI+ colour matrix here:
00376 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/UsingGDIPlus/Recoloring.asp
00377 
00378 Phil
00379 #endif

Generated on Sat Nov 10 03:48:17 2007 for Camelot by  doxygen 1.4.4