File: GriColor.cc

package info (click to toggle)
gri 2.4.2-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,540 kB
  • ctags: 1,966
  • sloc: cpp: 32,542; lisp: 3,243; perl: 806; makefile: 548; sh: 253
file content (167 lines) | stat: -rw-r--r-- 3,291 bytes parent folder | download
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
//#define DEBUG_GRICOLOR

#include "GriColor.hh"		// from gr.hh anyway
#include "gr.hh"
#include "types.hh"
#include "errors.hh"		// for OUT_OF_MEMORY

GriColor& GriColor::operator=(const GriColor& color)
{
    t = color.get_type();
    switch (t) {
    case rgb:
	a = color.getR();
	b = color.getG();
	c = color.getB();
	break;
    case hsv:
	a = color.getH();
	b = color.getS();
	c = color.getV();
	break;
    default:
	fprintf(stderr, "GriColor::operator= cannot handle this (%d) GriColor type\n", (int)t);
	exit(1);
    }
    return *this;
}

GriColor::GriColor(const GriColor& color)
{
    t = color.get_type();
    switch (t) {
    case rgb:
	a = color.getR();
	b = color.getG();
	c = color.getB();
	break;
    case hsv:
	a = color.getH();
	b = color.getS();
	c = color.getV();
	break;
    default:
	fprintf(stderr, "GriColor::GriColor cannot handle this (%d) GriColor type\n", t);
	exit(1);
    }
}

void
GriColor::setRGB(double R, double G, double B)
{
    a = pin0_1(R);
    b = pin0_1(G);
    c = pin0_1(B);
    t = rgb;
}
void GriColor::setHSV(double H, double S, double V)
{
    a = pin0_1(H);
    b = pin0_1(S);
    c = pin0_1(V);
    t = hsv;
}

GriNamedColor::GriNamedColor()
{
    name = new char [1];
    if (!name) OUT_OF_MEMORY;
    name[0] = '\0';
    a = b = c = 0.0;
    t = rgb;
}

GriNamedColor::GriNamedColor(const char *n, double R, double G, double B)
{
    name = new char [1 + strlen(n)];
    if (!name) OUT_OF_MEMORY;
    strcpy(name, n);
    a = R;
    b = G;
    c = B;
    t = rgb;
}

GriNamedColor::GriNamedColor(const GriNamedColor& color) 
{
    char *cp = color.getName();	// prevent fcn call
    name = new char[1 + strlen(cp)];
    if (!name) OUT_OF_MEMORY;
    strcpy(name, cp);
    t = color.get_type();
    switch (t) {
    case rgb:
	a = color.getR();
	b = color.getG();
	c = color.getB();
	break;
    case hsv:
	a = color.getH();
	b = color.getS();
	c = color.getV();
	break;
    default:
	fprintf(stderr, "GriNamedColor::GriNamedColor cannot handle this (%d) GriColor type\n", t);
	exit(1);
    }
}

GriNamedColor::~GriNamedColor()
{
    delete [] name;
}

GriNamedColor& GriNamedColor::operator=(const GriNamedColor& color)
{
    char *cp = color.getName();	// prevent fcn call
    if (strlen(cp) > strlen(name)) {
	delete [] name;
	name = new char[1 + strlen(cp)];
	if (!name) OUT_OF_MEMORY;
    }
    strcpy(name, cp);
    t = color.get_type();
    switch (t) {
    case rgb:
	a = color.getR();
	b = color.getG();
	c = color.getB();
	break;
    case hsv:
	a = color.getH();
	b = color.getS();
	c = color.getV();
	break;
    default:
	fprintf(stderr, "GriNamedColor::operator= cannot handle this (%d) GriColor type\n", t);
	exit(1);
    }
    return *this;
}

void GriNamedColor::setNameRGB(const char *newName, double R, double G, double B)
{
    if (strlen(newName) > strlen(name)) {
	delete [] name;
	name = new char[1 + strlen(newName)];
	if (!name) OUT_OF_MEMORY;
    }
    strcpy (name, newName);
    t = rgb;
    a = pin0_1(R);
    b = pin0_1(G);
    c = pin0_1(B);
}
void
GriColor::getRGB(double *R, double *G, double *B) const
{
    if (t == GriColor::rgb) {
	*R = a;
	*G = b;
	*B = c;
    } else if (t == GriColor::hsv) {
	gr_hsv2rgb(a, b, c, R, G, B);
    } else {
	gr_error("Internal error in GriColor::getRGB\n");
    }
}