From 8bd45ce893406d3cba3fee0c17b0528833380d47 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sun, 12 May 2019 16:36:43 +0100 Subject: [PATCH] add /rainbow and /rainbowme Slash Commands Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/SlashCommands.js | 21 ++++++++++++++ src/utils/colour.js | 67 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/utils/colour.js diff --git a/src/SlashCommands.js b/src/SlashCommands.js index f72ba1e005..0dc717a6fe 100644 --- a/src/SlashCommands.js +++ b/src/SlashCommands.js @@ -30,6 +30,7 @@ import MultiInviter from './utils/MultiInviter'; import { linkifyAndSanitizeHtml } from './HtmlUtils'; import QuestionDialog from "./components/views/dialogs/QuestionDialog"; import WidgetUtils from "./utils/WidgetUtils"; +import {textToHtmlRainbow} from "./utils/colour"; class Command { constructor({name, args='', description, runFn, hideCompletionAfterSpace=false}) { @@ -718,6 +719,26 @@ export const CommandMap = { return success(); }, }), + + rainbow: new Command({ + name: "rainbow", + description: _td(""), + args: '', + runFn: function(roomId, args) { + if (!args) return reject(this.getUserId()); + return success(MatrixClientPeg.get().sendHtmlMessage(roomId, args, textToHtmlRainbow(args))); + }, + }), + + rainbowme: new Command({ + name: "rainbowme", + description: _td(""), + args: '', + runFn: function(roomId, args) { + if (!args) return reject(this.getUserId()); + return success(MatrixClientPeg.get().sendHtmlEmote(roomId, args, textToHtmlRainbow(args))); + }, + }) }; /* eslint-enable babel/no-invalid-this */ diff --git a/src/utils/colour.js b/src/utils/colour.js new file mode 100644 index 0000000000..5d90bf0773 --- /dev/null +++ b/src/utils/colour.js @@ -0,0 +1,67 @@ +/* +Copyright 2019 Michael Telatynski <7t3chguy@gmail.com> + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +export function hueToRGB(h, s, l) { + const c = s * (1 - Math.abs(2 * l - 1)); + const x = c * (1 - Math.abs((h / 60) % 2 - 1)); + const m = l - c / 2; + + let r = 0; + let g = 0; + let b = 0; + + if (0 <= h && h < 60) { + r = c; + g = x; + b = 0; + } else if (60 <= h && h < 120) { + r = x; + g = c; + b = 0; + } else if (120 <= h && h < 180) { + r = 0; + g = c; + b = x; + } else if (180 <= h && h < 240) { + r = 0; + g = x; + b = c; + } else if (240 <= h && h < 300) { + r = x; + g = 0; + b = c; + } else if (300 <= h && h < 360) { + r = c; + g = 0; + b = x; + } + + return [Math.round((r + m) * 255), Math.round((g + m) * 255), Math.round((b + m) * 255)]; +} + + +export function textToHtmlRainbow(str) { + const frequency = 360 / str.length; + + return str.split("").map((c, i) => { + const [r, g, b] = hueToRGB(i * frequency, 1.0, 0.5); + return '' + c + ''; + }).join(""); +}