This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
206 lines (188 loc) · 5.48 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
(function(global, factory) {
if(typeof exports === 'object' && typeof module !== 'undefined') {
// CommonJS (Node.js)
module.exports = factory();
}
else if(typeof define === 'function' && define.amd) {
// AMD
define([], factory);
}
else {
// Browser Globals
global.ccc = factory();
}
})(this, function() {
/**
* Colour Code Converter
*
* @param {string | number | Array<string | number>} arg1 Colour Code or RGB Codes String or RGB Code Array
* @param {string | number} arg2 Green Colour or undefined
* @param {string | number} arg3 Blue Colour or undefined
* @return {string | Array<number>} Colour Code or RGB Codes or null : ex. '#1f2a3c' or 'rgb(15, 255, 0)'
*/
function ccc(arg1, arg2, arg3) {
if(arg1 && !arg2 && !arg3) {
// One Argument
if(typeof arg1 === 'string' && isColourCode(arg1)) {
// Colour Code
const parsed = beautifyColourCode(arg1);
return colourCodeToRGBCode(parsed);
}
else if(isRGBCode(arg1)) {
// RGB Code
const parsed = beautifyRGBCode(arg1);
return rgbCodeToColourCode(parsed);
}
}
else if(arg1 && arg2 && arg3 && isRGBCode(arg1, arg2, arg3)) {
// Three Arguments : RGB Code
const parsed = beautifyRGBCode(arg1, arg2, arg3);
return rgbCodeToColourCode(parsed);
}
// Invalid Values
return null;
}
return ccc;
/**
* Is Colour Code
*
* @param {string} input Input string
* @return {boolean} true if the argument is valid colour code
*/
function isColourCode(input) {
// Lower Case
input = `${input}`.toLowerCase();
// Remove Sharp
if(input.startsWith('#')) {
input = input.slice(1);
}
// Invalid Length
if(input.length !== 3 && input.length !== 6) {
return false;
}
// Contains Invalid Clour Code Characters
return input === input.replace(/[^0-9a-f]/g, '');
}
/**
* Is RGB Code
*
* @param {string | number} arg1 Argument 1 : RGB Codes or Red Colour
* @param {string | number} arg2 Argument 2 : Green Colour or undefined
* @param {string | number} arg3 Argument 3 : Blue Colour or undefined
* @return {boolean} true if the arguments are valid RGB code
*/
function isRGBCode(arg1, arg2, arg3) {
if(arg1 && !arg2 && !arg3) {
// "ccc 10,255,30"
const array = `${arg1}`.split(',');
// Invalid Length
if(array.length !== 3) {
return false;
}
// Every Arguments Are Valid?
return array.every((input) => {
return isValidRGBCode(input);
});
}
else if(arg1 && arg2 && arg3) {
// "ccc 10, 255, 30" or "ccc 10 255 30"
return isValidRGBCode(arg1) && isValidRGBCode(arg2) && isValidRGBCode(arg3);
}
else {
// Invalid Arguments Length
return false;
}
/**
* Is Valid RGB Code
*
* @param {string | number} input Input String
* @return {boolean} true if the input is valid RGB code
*/
function isValidRGBCode(input) {
// Remove Comma And Space
input = `${input}`.replace(/,/g, '').trim();
return input >= 0 && input <= 255;
}
}
/**
* Beautify Colour Code
*
* @param {string} input Input string
* @return {string} beautified colour code : ex. '39c' -> '3399cc'
*/
function beautifyColourCode(input) {
// Lower Case
input = `${input}`.toLowerCase();
// Remove Sharp
if(input.startsWith('#')) {
input = input.slice(1);
}
// Fix Shorthand : ex. '39c' => '3399cc'
if(input.length === 3) {
input = input
.split('')
.map((str) => {
return str.repeat(2);
})
.join('');
}
return input;
}
/**
* Beautify RGB Code
*
* @param {string | number} arg1 Argument 1 : RGB Codes or Red Colour
* @param {string | number} arg2 Argument 2 : Green Colour or undefined
* @param {string | number} arg3 Argument 3 : Blue Colour or undefined
* @return {Array<string | number>} beautified RGB codes array : ex. '15, 255, 0' -> [15, 255, 0]
*/
function beautifyRGBCode(arg1, arg2, arg3) {
if(arg1 && !arg2 && !arg3) {
return `${arg1}`.split(',');
}
else {
return [
`${arg1}`.replace(/,/g, '').trim(),
`${arg2}`.replace(/,/g, '').trim(),
`${arg3}`.replace(/,/g, '').trim()
];
}
}
/**
* Colour Code To RGB Code
*
* @param {string} code Colour Code : ex. '1f2a3c'
* @return {Array<string | number>} RGB codes array : ex. [15, 255, 0]
*/
function colourCodeToRGBCode(code) {
const codeR = `${code}`.substr(0, 2);
const codeG = `${code}`.substr(2, 2);
const codeB = `${code}`.substr(4, 2);
const r = parseInt(codeR, 16);
const g = parseInt(codeG, 16);
const b = parseInt(codeB, 16);
return [r, g, b];
}
/**
* RGB Code To Colour Code
*
* @param {Array<string | number>} rgb RGB Code Array : ex. [15, 255, 0]
* @return {string} Colour code : ex. '#1f2a3c'
*/
function rgbCodeToColourCode(rgb) {
const codeR = decimalToHex(rgb[0]);
const codeG = decimalToHex(rgb[1]);
const codeB = decimalToHex(rgb[2]);
return `#${codeR}${codeG}${codeB}`;
/**
* Decimal To Hex
*
* @param {string | number} decimal Decimal value : ex. 10
* @return {string} hex value : ex. '0a'
*/
function decimalToHex(decimal) {
let hex = Number(decimal).toString(16);
return `0${hex}`.substr(-2);
}
}
});