From ee902debe1634a78847cdbd71208b4d0d40cb0d0 Mon Sep 17 00:00:00 2001 From: Simon Schneegans Date: Mon, 13 Jun 2022 06:15:20 +0200 Subject: [PATCH] :wrench: Add helpers to read color settings --- kwin/main.js.in | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/kwin/main.js.in b/kwin/main.js.in index eec52cf..ba29b8a 100755 --- a/kwin/main.js.in +++ b/kwin/main.js.in @@ -30,6 +30,29 @@ class %EFFECT_CLASS% { this.loadConfig(); } + // A small helper which can be used to read a hex color string (e.g. #ff3244) from the + // settings. This method will return an array of four floating point values + // [r, g, b, a]. If the settings key only refers to an rgb value, the alpha component + // will be set to 1.0. + readRGBAConfig(key) { + const color = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?([a-f\d]{2})?$/i + .exec(effect.readConfig(key, '#ffffff')) + .slice(1) + .filter(x => x !== undefined) + .map(x => parseInt(x, 16) / 255); + return color.length == 3 ? [color[0], color[1], color[2], 1.0] : + [color[1], color[2], color[3], color[0]]; + } + + // A small helper which can be used to read a hex color string (e.g. #ff3244) from the + // settings. This method will return an array of three floating point values [r, g, b] + // regardless of whether the settings key actually refers to an argb hex string. + readRGBConfig(key) { + const color = readRGBAConfig(key); + color.pop(); + return color; + } + loadConfig() { this.duration = animationTime(effect.readConfig('Duration', 1000));