From f424afa537c1dd77a1cee2911af5512d015a7002 Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Wed, 11 Nov 2020 14:56:39 +0100 Subject: [PATCH] Added existing source for inital release Existing code for version 1.0.0 --- NiceColor.js | 54 ++++++++++++++++++++++++++++++++++++++++++++ module/NiceColor.mjs | 54 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 NiceColor.js create mode 100644 module/NiceColor.mjs diff --git a/NiceColor.js b/NiceColor.js new file mode 100644 index 0000000..50507a9 --- /dev/null +++ b/NiceColor.js @@ -0,0 +1,54 @@ +class NiceColor { + + constructor() { + this.hex = [ + null, // R + null, // G + null // B + ], + this.selected = this.randomDec(3) + } + + // 8-bit + randomHex() { + return [...Array(2)].map(() => Math.floor(Math.random() * 16).toString(16)).join(''); + } + + randomDec(ceil) { + return Math.floor(Math.random() * ceil) + 0; + } + + // Generate a 32-bit hexadecimal RGB string + generate() { + let binary = this.randomDec(2); + + this.hex[this.selected] = this.randomHex(); + + this.hex.forEach((value,index) => { + // Continue if index is our special value + if(this.hex[index] !== null) { + return; + } + + // Assign index and ignore if(binary) next run + if(binary) { + this.hex[index] = "16"; + binary = 0; + return; + } + + // Assign index and enter if(binary) next run + binary = 1; + this.hex[index] = "ff"; + }) + } + + // Generate and return HEX-color (#RRGGBB) + get() { + this.generate(); + + return "#" + this.hex.join(""); + } + +} +// Victor Westerlund \ No newline at end of file diff --git a/module/NiceColor.mjs b/module/NiceColor.mjs new file mode 100644 index 0000000..fb9c0d3 --- /dev/null +++ b/module/NiceColor.mjs @@ -0,0 +1,54 @@ +export class NiceColor { + + constructor() { + this.hex = [ + null, // R + null, // G + null // B + ], + this.selected = this.randomDec(3) + } + + // 8-bit + randomHex() { + return [...Array(2)].map(() => Math.floor(Math.random() * 16).toString(16)).join(''); + } + + randomDec(ceil) { + return Math.floor(Math.random() * ceil) + 0; + } + + // Generate a 32-bit hexadecimal RGB string + generate() { + let binary = this.randomDec(2); + + this.hex[this.selected] = this.randomHex(); + + this.hex.forEach((value,index) => { + // Continue if index is our special value + if(this.hex[index] !== null) { + return; + } + + // Assign index and ignore if(binary) next run + if(binary) { + this.hex[index] = "16"; + binary = 0; + return; + } + + // Assign index and enter if(binary) next run + binary = 1; + this.hex[index] = "ff"; + }) + } + + // Generate and return HEX-color (#RRGGBB) + get() { + this.generate(); + + return "#" + this.hex.join(""); + } + +} +// Victor Westerlund \ No newline at end of file