-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrgbToHEx.js
More file actions
121 lines (120 loc) · 2.98 KB
/
rgbToHEx.js
File metadata and controls
121 lines (120 loc) · 2.98 KB
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
// function hex(r, g, b) {
// return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
// }
// function rgb(r, g, b) {
// return `rgb(${r},${g},${b})`
// }
// // console.log(hex(255, 100, 25));
// // console.log(rgb(235,100,25));
// function makeColor(r, g, b) {
// const color = {};
// color.r = r;
// color.g = g;
// color.b = b;
// color.rgb = function () {
// const { r, g, b } = this;
// return `rgb(${r}, ${g} ,${b})`;
// };
// color.hex = function () {
// const { r, g, b } = this;
// return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
// }
// return color;
// }
// const firstColor = makeColor(35, 225, 150);
// firstColor.hex();
// firstColor.rgb();
// const black = makeColor(0, 0, 0);
// black.rgb();
// black.hex();
// function Color(r, g, b) {
// this.r = r;
// this.g = g;
// this.b = b;
// }
// Color.prototype.hex = function () {
// const { r, g, b } = this;
// return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
// };
// Color.prototype.rgb = function () {
// const { r, g, b } = this;
// return `rgb(${r},${g},${b})`;
// };
// Color.prototype.rgba = function (a ) {
// const { r, g, b } = this;
// return `rgba(${r},${g},${b},${a})`;
// }
// const color1 = new Color(40, 50, 60);
// const color2 = new Color(0, 0, 0);
class Color{
constructor(r,g,b,a,name){
this.r=r;
this.g=g;
this.b=b;
this.a=a;
this.name=name;
this.calcHSL();
}
innerRGB(){
const {r,g,b}=this;
return `${r}, ${g},${b}`;
}
rgba(a){
return `rgba(${this.innerRGB()},${a})`;
}
rgb(){
return `rgb(${this.innerRGB()})`;
}
hex(){
const { r, g, b } = this;
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
gree(){
return `hello from ${this.name}!`;
}
hsl(){
const {h,s,l}=this;
return `hsl(${h},${s}%,${l}%)`
}
opposite(){
const {h,s,l}=this;
const newHue=(h+180)%360;
return `hsl(${newHue},${s}%,${l}%)`
}
calcHSL(){
let {r,g,b}=this;
r/=255;
g/=255;
b/=255;
let cmin= Math.min(r,g,b);
let cmax=Math.max(r,g,b);
let delta=cmax-cmin;
let h=0;
let s=0;
let l=0;
if(delta==0)h==0;
else if(cmax==r)
h=((g-b)/delta)%6;
else if(cmax==g)
h=(b-r)/delta+2;
else
h=(r-g)/delta+4;
h=Math.round(h*60);
if(h<0)h+=360;
l=(cmax+cmin)/2;
s=delta==0?0:delta/(1-Math.abs(2*l-1));
s=+(s*100).toFixed(1);
l=+(l*100).toFixed(1);
this.h=h;
this.s=s;
this.l=l;
}
}
class Cat{
constructor(name,age){
this.name=name;
this.age=age;
}
}
const red= new Color(255,67,89,'tomato');
const black = new Color(0,0,0,'black');