-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCordialCalc.html
More file actions
273 lines (235 loc) · 8.33 KB
/
Copy pathCordialCalc.html
File metadata and controls
273 lines (235 loc) · 8.33 KB
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
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Sugar Dilution Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
margin-bottom: 10px;
}
h2 {
margin-top: 35px;
margin-bottom: 10px;
}
.info {
border: 1px solid #ccc;
color: #3a3a3a;
background: #f9f9f9;
padding: 5px 15px;
margin-top: 20px;
border-radius: 6px;
font-size: 0.9em;
max-width: 600px;
}
.info #infoemoji{
font-size: xx-large;
padding-right: 10px;
}
.row {
display: flex;
align-items: center;
margin-bottom: 10px;
}
.row label {
width: 130px;
}
.row input {
text-align: right;
direction: rtl;
padding: 5px;
width: 100px;
margin-right: 30px;
}
table#results{
border-collapse: collapse;
margin-top: 20px;
width: auto;
}
table#results th,
table#results td {
border: 1px solid #ccc;
padding: 5px 10px;
text-align: left;
}
table th {
background: #f0f0f0;
}
.section-divider {
margin-top: 20px;
border-top: 2px solid #ddd;
padding-top: 10px;
}
#copyLink {
margin-top: 20px;
}
.error {
color: red;
margin-bottom: 20px;
}
</style>
</head>
<body>
<h1>Sugar Dilution Calculator</h1>
<div class="info">
<table>
<tbody>
<tr>
<td id="infoemoji">ℹ️</td>
<td>
<p>
This calculator computes how much sugar and water must be added as a
<a href="https://en.wikipedia.org/w/index.php?title=Syrup#Simple_sugar_syrups" target="_blank">syrup</a>
to dilute an alcoholic liquid to a desired alcohol and sugar content. It can be used to adjust an
<a href="https://en.wikipedia.org/w/index.php?title=Maceration" target="_blank">alcohol-fruit-macerate</a>
to a desired target ABV and sugar content to create a
<a href="https://en.wikipedia.org/wiki/Liqueur" target="_blank">liqueur</a>.
</p>
<p>It assumes that <b>1 g sugar increases the volume by 0.63 ml</b> – an approximation from typical density
tables. Also it simplifies by assuming the input volume does not already contain sugar - so possible sugar
from macerated fruits is not considered.</p>
<p>The syrup does not need to be boiled, just heated until the sugar dissolves. It should cool to room
temperature before using it.</p>
</td>
</tr>
</tbody>
</table>
</div>
<h2>Input</h2>
<div class="row">
<label>Volume (ml):</label>
<input id="inputVolume" type="number" min="0">
<label>ABV (%):</label>
<input id="inputAbv" type="number" step="0.1" min="0">
</div>
<h2>Target</h2>
<div class="row">
<label>Target ABV (%):</label>
<input id="targetAbv" type="number" step="0.5" min="0">
<label>Target sugar (g/l):</label>
<input id="targetSugar" type="number" step="1" min="0">
</div>
<h2>Results</h2>
<table id="results">
<tbody></tbody>
</table>
<button id="copyLink">Copy Link</button>
<script>
// Constant: Sugar volume factor (1 g sugar adds ~0.63 ml to volume)
const SUGAR_VOLUME_FACTOR = 0.63;
// Attach wheel scrolling for number inputs
document.addEventListener('wheel', function (e) {
if (document.activeElement.type === "number" && document.activeElement === e.target) {
e.preventDefault();
let input = document.activeElement;
let step = parseFloat(input.step) || 1;
let min = parseFloat(input.min) || 0;
let value = parseFloat(input.value) || 0;
if (e.deltaY < 0) { value += step; } else { value -= step; }
if (value >= min) input.value = value;
calculate();
}
}, { passive: false });
// Parse URL params into inputs
function loadParams() {
const params = new URLSearchParams(window.location.search);
if (params.has("vol")) document.getElementById("inputVolume").value = params.get("vol");
if (params.has("abv")) document.getElementById("inputAbv").value = params.get("abv");
if (params.has("abvT")) document.getElementById("targetAbv").value = params.get("abvT");
if (params.has("sugarT")) document.getElementById("targetSugar").value = params.get("sugarT");
}
// Save inputs into URL
function saveParams() {
const params = new URLSearchParams();
let v = document.getElementById("inputVolume").value;
let a = document.getElementById("inputAbv").value;
let at = document.getElementById("targetAbv").value;
let st = document.getElementById("targetSugar").value;
if (v) params.set("vol", v);
if (a) params.set("abv", a);
if (at) params.set("abvT", at);
if (st) params.set("sugarT", st);
const newUrl = window.location.pathname + "?" + params.toString();
history.replaceState({}, "", newUrl);
}
// Perform calculation
function calculate() {
const inputVolume = parseFloat(document.getElementById("inputVolume").value);
const inputAbv = parseFloat(document.getElementById("inputAbv").value);
const targetAbv = parseFloat(document.getElementById("targetAbv").value);
const targetSugar = parseFloat(document.getElementById("targetSugar").value);
const resultsBody = document.querySelector("#results tbody");
resultsBody.innerHTML = "";
// Check inputs
if (isNaN(inputVolume) || isNaN(inputAbv) || isNaN(targetAbv) || isNaN(targetSugar)) {
resultsBody.innerHTML = `<div class="error">❌ Please correctly fill in all fields.</div>`;
return;
}
if (inputVolume <= 0) {
resultsBody.innerHTML = `<div class="error">❌ Input volume must be positive.</div>`;
return;
}
if (inputAbv <= 0) {
resultsBody.innerHTML = `<div class="error">❌ Input ABV must be positive.</div>`;
return;
}
if (targetAbv <= 0) {
resultsBody.innerHTML = `<div class="error">❌ Target ABV must be positive.</div>`;
return;
}
if (targetAbv > inputAbv) {
resultsBody.innerHTML = `<div class="error">❌ Target ABV must not be greater than input ABV.</div>`;
return;
}
if (targetSugar < 0) {
resultsBody.innerHTML = `<div class="error">❌ Target sugar must be positive.</div>`;
return;
}
// Absolute alcohol in ml
const alcInMl = inputVolume * (inputAbv / 100);
// Final required volume to reach target ABV
const finalVolume = alcInMl / (targetAbv / 100);
// Needed sugar in g (based on g/l * volume)
const neededSugarInG = (targetSugar / 1000) * finalVolume;
const neededSugarInMl = neededSugarInG * SUGAR_VOLUME_FACTOR;
// Syrup volume (sugar displacement + water)
const neededWater = finalVolume - inputVolume - neededSugarInMl;
const syrupVolume = neededWater + neededSugarInMl;
const ratio = (neededSugarInG / neededWater).toFixed(2);
// Check output
if (neededWater < 0) {
resultsBody.innerHTML = `<div class="error">❌ Required water would be negative.</div>`;
return;
}
// Results table
resultsBody.innerHTML = `
<tr><th colspan="2">Syrup data</th></tr>
<tr><td>Sugar</td><td>${Math.round(neededSugarInG)} g</td></tr>
<tr><td>Water</td><td>${Math.round(neededWater)} g</td></tr>
<tr><td>Syrup volume</td><td>${Math.round(syrupVolume)} ml</td></tr>
<tr><td>Syrup sugar:water ratio</td><td>${ratio}:1</td></tr>
<tr class="section-divider"><th colspan="2">Final mixture</th></tr>
<tr><td>Total volume</td><td>${Math.round(finalVolume)} ml</td></tr>
<tr><td>ABV</td><td>${targetAbv} %</td></tr>
<tr><td>Sugar</td><td>${targetSugar} g/l</td></tr>
`;
saveParams();
}
// Copy URL to clipboard
document.getElementById("copyLink").addEventListener("click", () => {
navigator.clipboard.writeText(window.location.href);
alert("Link copied to clipboard!");
});
// Bind inputs
document.querySelectorAll("input").forEach(inp => {
inp.addEventListener("input", calculate);
});
// Init
loadParams();
calculate();
</script>
</body>
</html>