summaryrefslogtreecommitdiffstats
path: root/src/base/Colour.cpp
blob: ea1f5a2deb65392be4d8750262e92bb99779142f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// -*- c-basic-offset: 4 -*-


/*
    Rosegarden
    A sequencer and musical notation editor.

    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <bownie@bownie.com>

    This file is Copyright 2003
        Mark Hymers         <markh@linuxfromscratch.org>

    The moral right of the authors to claim authorship of this work
    has been asserted.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "Colour.h"

#if (__GNUC__ < 3)
#include <strstream>
#define stringstream strstream
#else
#include <sstream>
#endif

namespace Rosegarden 
{

// The Colour Class

Colour::Colour()
{
    m_r = 0;
    m_g = 0;
    m_b = 0;
}

Colour::Colour(const unsigned int red, const unsigned int green, const unsigned int blue)
{
    this->setColour(red, green, blue);
}

Colour::Colour(const Colour& input)
{
    this->setColour(input.getRed(), input.getGreen(), input.getBlue());
}

Colour::~Colour()
{

}

Colour&
Colour::operator= (const Colour& input)
{
    // We don't have to check for assignment to self because it'll have
    //  no nasty effects (in fact, it'll do what it should - nothing)
    this->setColour(input.getRed(), input.getGreen(), input.getBlue());
    return *this;
}

void
Colour::setColour(const unsigned int red, const unsigned int green, const unsigned int blue)
{
    (red<=255)   ? m_r=red   : m_r=0;
    (green<=255) ? m_g=green : m_g=0;
    (blue<=255)  ? m_b=blue  : m_b=0;
}

void
Colour::getColour(unsigned int &red, unsigned int &green, unsigned int &blue) const
{
    red = m_r;
    green = m_g;
    blue = m_b;
}

unsigned int
Colour::getRed() const
{
    return m_r;
}

unsigned int
Colour::getBlue() const
{
    return m_b;
}

unsigned int
Colour::getGreen() const
{
    return m_g;
}

void
Colour::setRed(const unsigned int red)
{
    (red<=255) ? m_r=red : m_r=0;
}

void
Colour::setBlue(const unsigned int blue)
{
    (blue<=255) ? m_b=blue: m_b=0;
}

void
Colour::setGreen(const unsigned int green)
{
    (green<=255) ? m_g=green : m_g=0;
}

Colour
Colour::getContrastingColour() const
{
    Colour ret(255-m_r, 255-m_g, 255-m_b);
    return ret;
}

std::string
Colour::toXmlString() const
{
    std::stringstream output;

    output << "<colour red=\"" << m_r
           << "\" green=\"" << m_g
           << "\" blue=\"" << m_b
#if (__GNUC__ < 3)
           << "\"/>" << std::endl << std::ends;
#else
           << "\"/>" << std::endl;
#endif

    return output.str();
}

std::string
Colour::dataToXmlString() const
{
    std::stringstream output;
    output << "red=\"" << m_r
           << "\" green=\"" << m_g
           << "\" blue=\"" << m_b
#if (__GNUC__ < 3)
           << "\"" << std::ends;
#else
           << "\"";
#endif

    return output.str();
}

// Generic Colour routines:

Colour
getCombinationColour(const Colour &input1, const Colour &input2)
{
    Colour ret((input1.getRed()+input2.getRed())/2,
                (input1.getGreen()+input2.getGreen())/2,
                (input1.getBlue()+input2.getBlue())/2);
    return ret;

}

}