diff options
author | Ansuel Smith <ansuelsmth@gmail.com> | 2020-11-18 00:48:08 +0100 |
---|---|---|
committer | Ansuel Smith <ansuelsmth@gmail.com> | 2020-11-18 00:48:16 +0100 |
commit | 44d02afab15f3e7b30d6f5c24854e850b20dba57 (patch) | |
tree | 245516458dd051eb539a3a6baf523adb67e1c3b4 /modules/luci-base/htdocs/luci-static/resources/tools | |
parent | 1da9df837685b3a2afc80125d056e37198933533 (diff) |
luci-base: generalize random color generation
Generation of pseudo random hex color from a string can be useful also for other task. Generalize it to make it available also for other purpose.
Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
Diffstat (limited to 'modules/luci-base/htdocs/luci-static/resources/tools')
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/tools/prng.js | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/tools/prng.js b/modules/luci-base/htdocs/luci-static/resources/tools/prng.js index 752dc75ce8..b916cc7792 100644 --- a/modules/luci-base/htdocs/luci-static/resources/tools/prng.js +++ b/modules/luci-base/htdocs/luci-static/resources/tools/prng.js @@ -89,5 +89,23 @@ return L.Class.extend({ } return Math.floor(r * (u - l + 1)) + l; + }, + + derive_color: function(string) { + this.seed(parseInt(sfh(string), 16)); + + var r = this.get(128), + g = this.get(128), + min = 0, + max = 128; + + if ((r + g) < 128) + min = 128 - r - g; + else + max = 255 - r - g; + + var b = min + Math.floor(this.get() * (max - min)); + + return '#%02x%02x%02x'.format(0xff - r, 0xff - g, 0xff - b); } }); |