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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2012-2021 MATE Developers
*
* Licensed under the GNU General Public License Version 2
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include "egg-discrete.h"
/**
* egg_discrete_from_percent:
* @percentage: The percentage to convert
* @levels: The number of discrete levels
*
* We have to be carefull when converting from %->discrete as precision is very
* important if we want the highest value.
*
* Return value: The discrete value for this percentage.
**/
guint
egg_discrete_from_percent (guint percentage, guint levels)
{
/* check we are in range */
if (percentage > 100)
return levels;
if (levels == 0) {
g_warning ("levels is 0!");
return 0;
}
return (guint) ((((gfloat) percentage * (gfloat) (levels - 1)) / 100.0f) + 0.5f);
}
/**
* egg_discrete_to_percent:
* @hw: The discrete level
* @levels: The number of discrete levels
*
* We have to be carefull when converting from discrete->%.
*
* Return value: The percentage for this discrete value.
**/
guint
egg_discrete_to_percent (guint discrete, guint levels)
{
/* check we are in range */
if (discrete > levels)
return 100;
if (levels == 0) {
g_warning ("levels is 0!");
return 0;
}
return (guint) (((gfloat) discrete * (100.0f / (gfloat) (levels - 1))) + 0.5f);
}
/**
* egg_discrete_to_fraction:
* @hw: The discrete level
* @levels: The number of discrete levels
*
* We have to be careful when converting from discrete->fractions.
*
* Return value: The floating point fraction (0..1) for this discrete value.
**/
gfloat
egg_discrete_to_fraction (guint discrete, guint levels)
{
/* check we are in range */
if (discrete > levels)
return 1.0;
if (levels == 0) {
g_warning ("levels is 0!");
return 0.0;
}
return (guint) ((gfloat) discrete / ((gfloat) (levels - 1)));
}
/***************************************************************************
*** MAKE CHECK TESTS ***
***************************************************************************/
#ifdef EGG_TEST
#include "egg-test.h"
void
egg_discrete_test (gpointer data)
{
guint value;
gfloat fvalue;
EggTest *test = (EggTest *) data;
if (!egg_test_start (test, "EggDiscrete"))
return;
/************************************************************/
egg_test_title (test, "convert discrete 0/10 levels");
value = egg_discrete_to_percent (0, 10);
if (value == 0) {
egg_test_success (test, "got %i", value);
} else {
egg_test_failed (test, "conversion incorrect (%i)", value);
}
/************************************************************/
egg_test_title (test, "convert discrete 9/10 levels");
value = egg_discrete_to_percent (9, 10);
if (value == 100) {
egg_test_success (test, "got %i", value);
} else {
egg_test_failed (test, "conversion incorrect (%i)", value);
}
/************************************************************/
egg_test_title (test, "convert discrete 20/10 levels");
value = egg_discrete_to_percent (20, 10);
if (value == 100) {
egg_test_success (test, "got %i", value);
} else {
egg_test_failed (test, "conversion incorrect (%i)", value);
}
/************************************************************/
egg_test_title (test, "convert discrete 0/10 levels");
fvalue = egg_discrete_to_fraction (0, 10);
if (fvalue > -0.01 && fvalue < 0.01) {
egg_test_success (test, "got %f", fvalue);
} else {
egg_test_failed (test, "conversion incorrect (%f)", fvalue);
}
/************************************************************/
egg_test_title (test, "convert discrete 9/10 levels");
fvalue = egg_discrete_to_fraction (9, 10);
if (fvalue > -1.01 && fvalue < 1.01) {
egg_test_success (test, "got %f", fvalue);
} else {
egg_test_failed (test, "conversion incorrect (%f)", fvalue);
}
egg_test_end (test);
}
#endif
|