Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 86ae9d9

Browse files
authored
Merge pull request #2958 from matrix-org/t3chguy/cmd_rainbow
add /rainbow and /rainbowme Slash Commands
2 parents 99e2ac7 + 909354c commit 86ae9d9

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

src/SlashCommands.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import MultiInviter from './utils/MultiInviter';
3131
import { linkifyAndSanitizeHtml } from './HtmlUtils';
3232
import QuestionDialog from "./components/views/dialogs/QuestionDialog";
3333
import WidgetUtils from "./utils/WidgetUtils";
34+
import {textToHtmlRainbow} from "./utils/colour";
3435
import Promise from "bluebird";
3536

3637
class Command {
@@ -761,6 +762,26 @@ export const CommandMap = {
761762
return success();
762763
},
763764
}),
765+
766+
rainbow: new Command({
767+
name: "rainbow",
768+
description: _td("Sends the given message coloured as a rainbow"),
769+
args: '<message>',
770+
runFn: function(roomId, args) {
771+
if (!args) return reject(this.getUserId());
772+
return success(MatrixClientPeg.get().sendHtmlMessage(roomId, args, textToHtmlRainbow(args)));
773+
},
774+
}),
775+
776+
rainbowme: new Command({
777+
name: "rainbowme",
778+
description: _td("Sends the given emote coloured as a rainbow"),
779+
args: '<message>',
780+
runFn: function(roomId, args) {
781+
if (!args) return reject(this.getUserId());
782+
return success(MatrixClientPeg.get().sendHtmlEmote(roomId, args, textToHtmlRainbow(args)));
783+
},
784+
}),
764785
};
765786
/* eslint-enable babel/no-invalid-this */
766787

src/i18n/strings/en_EN.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@
179179
"The signing key you provided matches the signing key you received from %(userId)s's device %(deviceId)s. Device marked as verified.": "The signing key you provided matches the signing key you received from %(userId)s's device %(deviceId)s. Device marked as verified.",
180180
"Displays action": "Displays action",
181181
"Forces the current outbound group session in an encrypted room to be discarded": "Forces the current outbound group session in an encrypted room to be discarded",
182+
"Sends the given message coloured as a rainbow": "Sends the given message coloured as a rainbow",
183+
"Sends the given emote coloured as a rainbow": "Sends the given emote coloured as a rainbow",
182184
"Unrecognised command:": "Unrecognised command:",
183185
"Reason": "Reason",
184186
"%(targetName)s accepted the invitation for %(displayName)s.": "%(targetName)s accepted the invitation for %(displayName)s.",

src/utils/colour.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
export function hueToRGB(h, s, l) {
18+
const c = s * (1 - Math.abs(2 * l - 1));
19+
const x = c * (1 - Math.abs((h / 60) % 2 - 1));
20+
const m = l - c / 2;
21+
22+
let r = 0;
23+
let g = 0;
24+
let b = 0;
25+
26+
if (0 <= h && h < 60) {
27+
r = c;
28+
g = x;
29+
b = 0;
30+
} else if (60 <= h && h < 120) {
31+
r = x;
32+
g = c;
33+
b = 0;
34+
} else if (120 <= h && h < 180) {
35+
r = 0;
36+
g = c;
37+
b = x;
38+
} else if (180 <= h && h < 240) {
39+
r = 0;
40+
g = x;
41+
b = c;
42+
} else if (240 <= h && h < 300) {
43+
r = x;
44+
g = 0;
45+
b = c;
46+
} else if (300 <= h && h < 360) {
47+
r = c;
48+
g = 0;
49+
b = x;
50+
}
51+
52+
return [Math.round((r + m) * 255), Math.round((g + m) * 255), Math.round((b + m) * 255)];
53+
}
54+
55+
56+
export function textToHtmlRainbow(str) {
57+
const frequency = 360 / str.length;
58+
59+
return str.split("").map((c, i) => {
60+
const [r, g, b] = hueToRGB(i * frequency, 1.0, 0.5);
61+
return '<font color="#' +
62+
r.toString(16).padStart(2, "0") +
63+
g.toString(16).padStart(2, "0") +
64+
b.toString(16).padStart(2, "0") +
65+
'">' + c + '</font>';
66+
}).join("");
67+
}

0 commit comments

Comments
 (0)