Added existing source for inital release

Existing code for version 1.0.0
This commit is contained in:
Victor Westerlund 2020-11-11 14:56:39 +01:00
parent fbc6e30f88
commit f424afa537
2 changed files with 108 additions and 0 deletions

54
NiceColor.js Normal file
View file

@ -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

54
module/NiceColor.mjs Normal file
View file

@ -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