GUIDOLib  1.7.7
A Music Score Rendering Engine
VGColor.h
Go to the documentation of this file.
1 #ifndef VGColor_H
2 #define VGColor_H
3 
4 /*
5  GUIDO Library
6  Copyright (C) 2006 Grame
7 
8  This Source Code Form is subject to the terms of the Mozilla Public
9  License, v. 2.0. If a copy of the MPL was not distributed with this
10  file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 
12  Grame Research Laboratory, 11, cours de Verdun Gensoul 69002 Lyon - France
13  research@grame.fr
14 
15 */
16 
17 #include <iostream>
18 
24 // define int constants to bound alpha values
25 #define ALPHA_TRANSPARENT 0
26 #define ALPHA_OPAQUE 255
27 
28 
29 // --------------------------------------------------------------
30 // VGColor class
31 // --------------------------------------------------------------
34 class VGColor
35 {
36  public:
37  VGColor( int gray = 0 )
38  : mRed( gray ), mGreen( gray ), mBlue( gray ), mAlpha( ALPHA_OPAQUE ) { }
39  VGColor( const VGColor & in )
40  { Set( in ); }
41  VGColor( int r, int g, int b, int a = ALPHA_OPAQUE )
42  : mRed( r ), mGreen( g ), mBlue ( b ), mAlpha( a ) { }
43 
44  explicit VGColor( const unsigned char col [4] )
45  { if( col ) Set( col[0], col[1], col[2], col[3] ); else Set( 0 ); }
46 
47  void Set( int r, int g, int b, int a = ALPHA_OPAQUE )
48  { mRed = r; mGreen = g; mBlue = b; mAlpha = a; }
49  void Set( const VGColor & in )
50  { mRed = in.mRed; mGreen = in.mGreen; mBlue = in.mBlue; mAlpha = in.mAlpha; }
51 
52 
53  bool operator == ( const VGColor & col ) const
54  { return (( col.mRed == mRed ) && ( col.mGreen == mGreen )
55  && ( col.mBlue == mBlue ) && ( col.mAlpha == mAlpha )); }
56 
57  bool operator != ( const VGColor & col ) const
58  { return (( col.mRed != mRed ) || ( col.mGreen != mGreen )
59  || ( col.mBlue != mBlue ) || ( col.mAlpha != mAlpha )); }
60 
61  VGColor & operator += ( short v );
62 
63  //modifications of this method must be applied also in the action parser.
64  //This fonction displays color as html does. ex : #45fc0a[ff]
65  std::ostream& print(std::ostream& out) const
66  {
67  out << "#";
68  char prev = out.fill ('0');
69  out.width(2);out << std::hex << int(mRed);
70  out.width(2);out << std::hex << int(mGreen);
71  out.width(2);out << std::hex << int(mBlue);
72  out.width(1);out << "[";
73  out.width(2);out << std::hex << int(mAlpha) << std::dec;
74  out.width(1);out << "]";
75  out.fill(prev);
76 
77  return out;
78  }
79 
80  int mRed;
81  int mGreen;
82  int mBlue;
83  int mAlpha; // 0 = invisible, 255 = opaque.
84 };
85 
86 
87 inline VGColor & VGColor::operator += ( short v )
88 {
89  int sred = mRed + v;
90  mRed = (unsigned char)((sred > 255) ? 255 : sred);
91  int sgreen = mGreen + v;
92  mGreen = (unsigned char)((sgreen > 255) ? 255 : sgreen);
93  int sblue = mBlue + v;
94  mBlue = (unsigned char)((sblue > 255) ? 255 : sblue);
95  return *this;
96 }
97 
98 inline std::ostream& operator << (std::ostream& out, const VGColor& c)
99 {
100  return (&c)->print(out);
101 }
102 
103 
106 #endif /* VGColor */
VGColor::print
std::ostream & print(std::ostream &out) const
Definition: VGColor.h:65
VGColor::operator+=
VGColor & operator+=(short v)
Definition: VGColor.h:87
VGColor::Set
void Set(const VGColor &in)
Definition: VGColor.h:49
ALPHA_OPAQUE
#define ALPHA_OPAQUE
Definition: VGColor.h:26
VGColor::VGColor
VGColor(int gray=0)
Definition: VGColor.h:37
VGColor::operator==
bool operator==(const VGColor &col) const
Definition: VGColor.h:53
operator<<
std::ostream & operator<<(std::ostream &out, const VGColor &c)
Definition: VGColor.h:98
VGColor::mGreen
int mGreen
Definition: VGColor.h:81
VGColor::VGColor
VGColor(const VGColor &in)
Definition: VGColor.h:39
VGColor::VGColor
VGColor(int r, int g, int b, int a=ALPHA_OPAQUE)
Definition: VGColor.h:41
VGColor::VGColor
VGColor(const unsigned char col [4])
Definition: VGColor.h:44
VGColor::operator!=
bool operator!=(const VGColor &col) const
Definition: VGColor.h:57
VGColor::mAlpha
int mAlpha
Definition: VGColor.h:83
VGColor
Generic class to manipulate device independant colors.
Definition: VGColor.h:34
VGColor::Set
void Set(int r, int g, int b, int a=ALPHA_OPAQUE)
Definition: VGColor.h:47
VGColor::mRed
int mRed
Definition: VGColor.h:80
VGColor::mBlue
int mBlue
Definition: VGColor.h:82

Guido Project Copyright © 2019 Grame-CNCM