Skip to content

Commit 173d6d1

Browse files
committed
🔧 Use GTK for color conversion
1 parent e292c82 commit 173d6d1

File tree

2 files changed

+20
-46
lines changed

2 files changed

+20
-46
lines changed

src/effects/AuraGlow.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import * as utils from '../utils.js';
2222
// only uses the static metadata of the effect.
2323
const ShaderFactory = await utils.importInShellOnly('./ShaderFactory.js');
2424

25+
// We import Gtk for the color preview in the preferences dialog. This is only available
26+
// and required in the preferences process.
27+
const Gtk = await utils.importInPrefsOnly('gi://Gtk');
28+
2529
const _ = await utils.importGettext();
2630

2731
//////////////////////////////////////////////////////////////////////////////////////////
@@ -106,15 +110,15 @@ export default class Effect {
106110
dialog.getBuilder()
107111
.get_object('aura-glow-hue-preview')
108112
.set_draw_func((area, cairo) => {
109-
const hueScale = dialog.getBuilder().get_object('aura-glow-start-hue-slider');
110-
const satScale = dialog.getBuilder().get_object('aura-glow-saturation-slider');
111-
const color = utils.hsvToRgb(hueScale.get_value(), satScale.get_value(), 1);
112-
const height = area.get_allocated_height();
113-
const width = area.get_allocated_width();
114-
cairo.setSourceRGB(color.r, color.g, color.b);
113+
const hueScale = dialog.getBuilder().get_object('aura-glow-start-hue-slider');
114+
const satScale = dialog.getBuilder().get_object('aura-glow-saturation-slider');
115+
const [r, g, b] = Gtk.hsv_to_rgb(hueScale.get_value(), satScale.get_value(), 1);
116+
const height = area.get_allocated_height();
117+
const width = area.get_allocated_width();
118+
cairo.setSourceRGB(r, g, b);
115119
cairo.arc(width / 2, height / 2, width / 2, 0.0, 2 * Math.PI);
116120
cairo.fill();
117-
});
121+
});
118122

119123
function redrawHuePreview() {
120124
dialog.getBuilder().get_object('aura-glow-hue-preview').queue_draw();

src/utils.js

+9-39
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ export async function importInShellOnly(module) {
102102
return null;
103103
}
104104

105+
// This method can be used to import a module in the preferences process only. The same as
106+
// importInShellOnly(), but the other way around.
107+
export async function importInPrefsOnly(module) {
108+
if (typeof global === 'undefined') {
109+
return (await import(module)).default;
110+
}
111+
return null;
112+
}
113+
105114
// This method can be used to import gettext. This is done differently in the
106115
// GNOME Shell process and in the preferences process.
107116
export async function importGettext() {
@@ -195,42 +204,3 @@ export function parseColor(string) {
195204

196205
return [color.red / 255, color.green / 255, color.blue / 255, color.alpha / 255];
197206
}
198-
199-
// Converts a color in hsv space into rgb.
200-
// https://stackoverflow.com/a/17243070
201-
export function hsvToRgb(h, s, v) {
202-
var r, g, b, i, f, p, q, t;
203-
if (arguments.length === 1) {
204-
(s = h.s), (v = h.v), (h = h.h);
205-
}
206-
i = Math.floor(h * 6);
207-
f = h * 6 - i;
208-
p = v * (1 - s);
209-
q = v * (1 - f * s);
210-
t = v * (1 - (1 - f) * s);
211-
switch (i % 6) {
212-
case 0:
213-
(r = v), (g = t), (b = p);
214-
break;
215-
case 1:
216-
(r = q), (g = v), (b = p);
217-
break;
218-
case 2:
219-
(r = p), (g = v), (b = t);
220-
break;
221-
case 3:
222-
(r = p), (g = q), (b = v);
223-
break;
224-
case 4:
225-
(r = t), (g = p), (b = v);
226-
break;
227-
case 5:
228-
(r = v), (g = p), (b = q);
229-
break;
230-
}
231-
return {
232-
r,
233-
g,
234-
b,
235-
};
236-
}

0 commit comments

Comments
 (0)