File: value.cc

package info (click to toggle)
gelemental 1.2.0-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,412 kB
  • ctags: 705
  • sloc: sh: 11,797; cpp: 5,255; makefile: 197
file content (328 lines) | stat: -rw-r--r-- 7,221 bytes parent folder | download | duplicates (7)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * This file is part of libelemental, a periodic table library with detailed
 * information on elements.
 *
 * Copyright (C) 2006-2007 Kevin Daughtridge <kevin@kdau.com>
 * Copyright (C) 2003 Jonas Frantz <jonas.frantz@welho.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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "private.hh"
#include <libelemental/value.hh>
#include <libelemental/value-types.hh>

#include <ext/stdio_filebuf.h>
#include <glibmm/utility.h>
#include <pango/pango-attributes.h>

namespace Elemental {


const double STANDARD_TEMPERATURE = 273.15; // Kelvin


ustring
get_list_separator () throw ()
{
	// Translators: This is the separator for items in a list.
	static const Message LIST_SEPARATOR = N_(", ");
	return LIST_SEPARATOR.get_string ();
}


//******************************************************************************
// struct color


color::color (double red_, double green_, double blue_) throw ()
:	red (CLAMP (red_, 0.0, 1.0)),
	green (CLAMP (green_, 0.0, 1.0)),
	blue (CLAMP (blue_, 0.0, 1.0))
{}


color::color (unsigned int red_, unsigned int green_, unsigned int blue_)
	throw ()
:	red (std::min (red_, 65535u) / 65535.0),
	green (std::min (green_, 65535u) / 65535.0),
	blue (std::min (blue_, 65535u) / 65535.0)
{}


double
color::get_luminance () const throw ()
{
	double luminance = red * 0.2126 + green * 0.7152 + blue * 0.0722;
	return CLAMP (luminance, 0.0, 1.0);
}


color
color::get_compliment () const throw ()
{
	double grey = (get_luminance () > 0.4) ? 0.0 : 1.0;
	return color (grey, grey, grey);
}


color
color::composite (const color& other, double alpha) const throw ()
{
	alpha = CLAMP (alpha, 0.0, 1.0);
	double factor = 1.0 - alpha;
	return color (red * factor + other.red * alpha,
		green * factor + other.green * alpha,
		blue * factor + other.blue * alpha);
}


std::string
color::get_hex_spec () const throw ()
{
	std::ostringstream os;
	os << "#" << std::hex << std::setfill ('0');
	os << std::setw (2) << CLAMP (int (red * 255.0), 0, 255);
	os << std::setw (2) << CLAMP (int (green * 255.0), 0, 255);
	os << std::setw (2) << CLAMP (int (blue * 255.0), 0, 255);
	return os.str ();
}


//******************************************************************************
// class EntriesView


EntriesView::~EntriesView ()
{}


ustring::size_type
EntriesView::get_max_name_length () throw ()
{
	return max_name_length;
}


void
EntriesView::accommodate_name (ustring::size_type length) throw ()
{
	if (length > max_name_length)
		max_name_length = length;
}


ustring::size_type
EntriesView::max_name_length = 0;


//******************************************************************************
// class EntriesStream


EntriesStream::EntriesStream (FILE* file) throw ()
:	buf (new __gnu_cxx::stdio_filebuf<char> (file, std::ios_base::out)),
	buf_owned (true), os (*new std::ostream (buf)), os_owned (true)
{}


EntriesStream::EntriesStream (int fd) throw ()
:	buf (new __gnu_cxx::stdio_filebuf<char> (fd, std::ios_base::out)),
	buf_owned (true), os (*new std::ostream (buf)), os_owned (true)
{}


EntriesStream::EntriesStream (std::streambuf& buf_) throw ()
:	buf (&buf_), buf_owned (false),
	os (*new std::ostream (buf)), os_owned (true)
{}


EntriesStream::EntriesStream (std::ostream& os_) throw ()
:	buf (NULL), buf_owned (false),
	os (os_), os_owned (false)
{}


EntriesStream::~EntriesStream ()
{
	if (os_owned) delete &os;
	if (buf_owned) delete buf;
}


void
EntriesStream::header (const ustring& category) throw ()
{
	os << std::endl << category << std::endl;
}


void
EntriesStream::entry (const ustring& name, const ustring& value,
	const ustring& tip) throw ()
{
	ustring result = "  ";

	if (!name.empty ())
	{
		result += name;

		ustring::size_type padding =
			std::max (0, int (get_max_name_length ()) - int (name.size ())) + 1;
		result += ustring (padding, ' ');
	}

	{
		Glib::ScopedPtr<char> parsed_value;
		GError *error = NULL;
		if (!pango_parse_markup (value.data (), -1, 0, NULL,
			parsed_value.addr (), NULL, &error))
		{
			os << Glib::Error (error).what () << std::endl;
			return;
		}

		result += tip.empty () ? parsed_value.get ()
			: compose::ucompose (_("%1 (%2)"), parsed_value.get (), tip);
	}

	if (Glib::get_charset ())
		os << result;
	else
	{
		std::string charset;
		Glib::get_charset (charset);
		os << Glib::convert_with_fallback (result, charset, "UTF-8", "?");
	}
		
	os << std::endl;
}


//******************************************************************************
// class value_base


const int value_base::YIELD_COMPARE = -2;


value_base::value_base (Qualifier qualifier_) throw ()
:	qualifier (qualifier_), always (false)
{}


value_base::~value_base ()
{}


bool
value_base::has_value () const throw ()
{
	switch (qualifier)
	{
	case Q_UNK:
	case Q_NA:
		return false;
	case Q_NEUTRAL:
	case Q_EST:
	case Q_CA:
	case Q_ISO:
	default:
		return true;
	}
}


ustring
value_base::get_string (const ustring& format) const throw ()
{
	switch (qualifier)
	{
	case Q_UNK: return _("(unknown)");
	case Q_NA: return _("(n/a)");
	// Translators: "(%1)" represents an estimated or calculated value.
	case Q_EST: return compose::ucompose (_("(%1)"), do_get_string (format));
	// Translators: "~%1" represents an approximate value.
	case Q_CA: return compose::ucompose (_("~%1"), do_get_string (format));
	// Translators: "[%1]" represents a value for a most stable isotope.
	case Q_ISO: return compose::ucompose (_("[%1]"), do_get_string (format));
	case Q_NEUTRAL:
	default:
		return do_get_string (format);
	}
}


ustring
value_base::get_tip () const throw ()
{
	switch (qualifier)
	{
	case Q_EST: return _("Estimated or calculated value");
	case Q_CA: return _("Approximate");
	case Q_ISO: return _("Value for most stable isotope");
	default: return ustring ();
	}
}


void
value_base::make_entry (EntriesView& view, const ustring& name,
	const ustring& format) const throw ()
{
	if (always || has_value ())
		view.entry (name, get_string (format), get_tip ());
}


int
value_base::compare (const value_base& other) const throw ()
{
	int base = compare_base (other);
	return (base == YIELD_COMPARE) ? 0 : base;
}


int
value_base::compare_base (const value_base& other) const throw ()
{
	if (has_value ()) {
		if (other.has_value ())
			return YIELD_COMPARE;
		else
			return -1;
	} else {
		if (other.has_value ())
			return 1;
		else
			return 0;
	}
}


//******************************************************************************
// class color_value_base


color_value_base::color_value_base (Qualifier qualifier_) throw ()
:	value_base (qualifier_)
{}


color_value_base::~color_value_base ()
{}


} // namespace Elemental