File: HSBColor.h

package info (click to toggle)
contextfree 2.2%2Bdfsg1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,292 kB
  • ctags: 4,316
  • sloc: cpp: 20,388; yacc: 507; objc: 494; ansic: 270; makefile: 114; lex: 92; sh: 35; xml: 24
file content (130 lines) | stat: -rw-r--r-- 4,438 bytes parent folder | download | duplicates (2)
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
// HSBColor.h
// this file is part of Context Free
// ---------------------
// Copyright (C) 2005-2008 Mark Lentczner - markl@glyphic.com
// Copyright (C) 2005-2008 John Horigan - john@glyphic.com
//
// 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.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// 
// John Horigan can be contacted at john@glyphic.com or at
// John Horigan, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
// Mark Lentczner can be contacted at markl@glyphic.com or at
// Mark Lentczner, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
//

#ifndef __HSBCOLOR_H_
#define __HSBCOLOR_H_

namespace agg {
    struct rgba;
};

#define EQUALITY_THRESHOLD  0.00001

#include <math.h>

struct HSBColor
{
    enum AssignmentTypes { HueTarget = 1, 
                           SaturationTarget = 2, 
                           BrightnessTarget = 4, 
                           AlphaTarget = 8 
                         };
                             
    HSBColor() : h(0), s(0), b(0), a(0), mUseTarget(0) {};
    HSBColor(double hue, double sat, double bri, double alpha) 
        : h(hue), s(sat), b(bri), a(alpha), mUseTarget(0) {};
    HSBColor(const agg::rgba&);

    void adjustWith(const HSBColor& adj, const HSBColor& target = HSBColor(0,0,0,0));

    void getRGBA(agg::rgba& c) const;
    
    bool operator!=(const HSBColor& hsb) const {
        return h != hsb.h || s != hsb.s || b != hsb.b || a != hsb.s;
    }

	double h, s, b, a;
    int mUseTarget;
    
    static inline double adjust(const double& base, const double& adjustment, 
                                int useTarget = 0, const double& target = 0.0);
    static inline double adjustHue(const double& base, const double& adjustment, 
                                   int useTarget = 0, const double& target = 0.0);
    static double delta(double to, double from, unsigned int steps = 1);
    static double deltaHue(double to, double from, unsigned int steps = 1);
};

inline double
HSBColor::adjust(const double& base, const double& adjustment, 
                 int useTarget, const double& target)
{
    if (adjustment == 0.0)
        return base;
    
    if (useTarget) {
            // If we are really close to the target then don't change, even if
            // the adjustment is negative (which way would we go?)
        if (adjustment > 0 && fabs(base - target) < EQUALITY_THRESHOLD)
            return base;
        
            // Otherwise move away from or toward the target
        double edge = base < target ? 0 : 1;
        if (adjustment < 0)
            return base + (  base - edge) * adjustment;
        else 
            return base + (target - base) * adjustment;
    } else {
            // Move toward 0 or toward 1
        if (adjustment < 0)
            return base + base * adjustment;
        else 
            return base + (1 - base) * adjustment;
    }
}

inline double
HSBColor::adjustHue(const double& base, const double& adjustment, 
                    int useTarget, const double& target)
{
    if (adjustment == 0.0)
        return base;
    
    double h;
    if (useTarget & HueTarget) {
        // decrease or increase toward target. If the target hue does not 
        // cooperate by being smaller (or larger) than the current hue then 
        // add/subtract 360 to make it so. This only works if all hues are
        // within the interval [0,360).
        double t = target;
        if (adjustment < 0) {
            if (t > base) t -= 360;
            h = base + (base - t) * adjustment;
        } else {
            if (t < base) t += 360;
            h = base + (t - base) * adjustment;
        }
    } else {
        h = base + adjustment;
    }
    
    // Normalize result to the interval [0,360)
    return h < 0.0 ? fmod(h + 360.0, 360.0) : fmod(h, 360.0);
}


#endif