summaryrefslogtreecommitdiff
path: root/lib/renderers
diff options
context:
space:
mode:
authorBen Beltran <ben@nsovocal.com>2016-05-09 00:30:22 -0500
committerBen Beltran <ben@nsovocal.com>2016-05-09 00:30:22 -0500
commitc7b4bd19a006d61c3b4979e884370d123cec7524 (patch)
tree6be738bd4641e2fd2ab8a754dc529e773c220d64 /lib/renderers
parentae6dbe43bf54581dd7012f95581d4a1b6ee245f0 (diff)
Ports the basic script to split modules
Diffstat (limited to 'lib/renderers')
-rw-r--r--lib/renderers/256_colors.js16
-rw-r--r--lib/renderers/ansi.js13
-rw-r--r--lib/renderers/fake_color.js8
-rw-r--r--lib/renderers/true_color.js11
4 files changed, 48 insertions, 0 deletions
diff --git a/lib/renderers/256_colors.js b/lib/renderers/256_colors.js
new file mode 100644
index 0000000..e8e6e37
--- /dev/null
+++ b/lib/renderers/256_colors.js
@@ -0,0 +1,16 @@
+'use strict';
+
+// Returns a 256 color, see
+// https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
+// under 256 colors for more info.
+const TwoFiftySixColors = function (red, blue, green) {
+ let redValue = Math.round(red * 5 / 255);
+ let blueValue = Math.round(blue * 5 / 255);
+ let greenValue = Math.round(green * 5 / 255);
+
+ let colorNumber = 16 + 36 * redValue + 6 * greenValue + blueValue;
+
+ return `\x1B[48;5;${colorNumber}m`;
+};
+
+module.exports = TwoFiftySixColors;
diff --git a/lib/renderers/ansi.js b/lib/renderers/ansi.js
new file mode 100644
index 0000000..c3975da
--- /dev/null
+++ b/lib/renderers/ansi.js
@@ -0,0 +1,13 @@
+'use strict';
+
+// Returns a basic ANSI color. See the color table in:
+// https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
+const ANSI = function (red, blue, green) {
+ let colorOffset = Math.round((red + blue + green) * 7 / (255 * 3));
+
+ let colorNumber = 40 + colorOffset;
+
+ return `\x1B[${colorNumber}m`;
+};
+
+module.exports = ANSI;
diff --git a/lib/renderers/fake_color.js b/lib/renderers/fake_color.js
new file mode 100644
index 0000000..b82bb45
--- /dev/null
+++ b/lib/renderers/fake_color.js
@@ -0,0 +1,8 @@
+'use strict';
+
+// Sends malformed ANSI 24-bit color strings
+const FakeColor = function (red, blue, green) {
+ return `\x1B[28;2;${Math.round(red)};${Math.round(green)};${Math.round(blue)}m`;
+};
+
+module.exports = FakeColor;
diff --git a/lib/renderers/true_color.js b/lib/renderers/true_color.js
new file mode 100644
index 0000000..5f7725f
--- /dev/null
+++ b/lib/renderers/true_color.js
@@ -0,0 +1,11 @@
+'use strict';
+
+// Returns an ANSI Code for True Color, see:
+// https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
+// and look for 24-bit colors. Only looks good in supported terminals,
+// otherwise looks like FakeColor.
+const TrueColor = function (red, blue, green) {
+ return `\x1B[48;2;${Math.round(red)};${Math.round(green)};${Math.round(blue)}m`;
+};
+
+module.exports = TrueColor;