-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorpicker.js
193 lines (142 loc) · 5.89 KB
/
colorpicker.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
// COLOR PICKER
let drawingColor = vec4(1, 0, 0, 1);
function initColorPicker() {
glC.viewport(0, 0, colorsCanvas.width, colorsCanvas.height);
glC.clearColor(1.0, 1.0, 1.0, 1.0);
glS.viewport(0, 0, sliderCanvas.width, sliderCanvas.height);
glS.clearColor(1.0, 1.0, 1.0, 1.0);
// COLOR PICKER
var programC = initShaders(glC, "vertex-shaderC", "fragment-shaderC");
glC.useProgram(programC);
//Center
var vertices = [
vec2(0.0, 0.0)
];
//center color
var colors = [
vec4(1.0,1.0,1.0,1.0)
]
var nbTriangles = 24;
let angle = Math.PI * 2 / nbTriangles;
for (i = 0; i <= nbTriangles; i++) {
let circleVertex = vec2(
Math.cos(angle * i) * 0.9 ,
Math.sin(angle * i) * 0.9
);
vertices.push(circleVertex);
}
colors.push(vec4(1.0,0.0,0.0,1.0));
colors.push(vec4(1.0,0.25,0.0,1.0));
colors.push(vec4(1.0,0.5,0.0,1.0));
colors.push(vec4(1.0,0.75,0.0,1.0));
colors.push(vec4(1.0,1.0,0.0,1.0));
colors.push(vec4(0.75,1.0,0.0,1.0));
colors.push(vec4(0.5,1.0,0.0,1.0));
colors.push(vec4(0.25,1.0,0.0,1.0));
colors.push(vec4(0.0,1.0,0.0,1.0));
colors.push(vec4(0.0,1.0,0.25,1.0));
colors.push(vec4(0.0,1.0,0.5,1.0));
colors.push(vec4(0.0,1.0,0.75,1.0));
colors.push(vec4(0.0,1.0,1.0,1.0));
colors.push(vec4(0.0, 0.75,1.0,1.0));
colors.push(vec4(0.0, 0.5,1.0,1.0));
colors.push(vec4(0.0, 0.25,1.0,1.0));
colors.push(vec4(0.0, 0.0,1.0,1.0));
colors.push(vec4(0.25,0.0,1.0, 1.0));
colors.push(vec4(0.5, 0.0, 1.0 , 1.0));
colors.push(vec4(0.75,0.0,1.0,1.0));
colors.push(vec4(1.0,0.0,1.0,1.0));
colors.push(vec4(1.0,0.0,0.75,1.0));
colors.push(vec4(1.0,0.0,0.5,1.0));
colors.push(vec4(1.0,0.0,0.25,1.0));
colors.push(vec4(1.0,0.0,0.0,1.0));
var buffer = glC.createBuffer();
glC.bindBuffer(glC.ARRAY_BUFFER, buffer);
glC.bufferData(glC.ARRAY_BUFFER, flatten(vertices), glC.STATIC_DRAW);
var vPosition = glC.getAttribLocation(programC, "vPositionC");
glC.vertexAttribPointer(vPosition, 2, glC.FLOAT, false, 0, 0);
glC.enableVertexAttribArray(vPosition);
var colorBuffer = glC.createBuffer();
glC.bindBuffer(glC.ARRAY_BUFFER, colorBuffer);
glC.bufferData(glC.ARRAY_BUFFER, flatten(colors), glC.STATIC_DRAW);
var vertexColor = glC.getAttribLocation(programC, "vertexColorC");
glC.vertexAttribPointer(vertexColor, 4, glC.FLOAT, false, 0, 0);
glC.enableVertexAttribArray(vertexColor);
var verticesSlider= [
vec2(0.0, -1.0),//1
vec2(-1.0, 1.0),
vec2(1.0, 1.0),
];
var colorSlider = [
vec4(1.0, 1.0, 1.0, 1.0),
vec4(0.0, 0.0, 0.0, 1.0),
vec4(1.0, 0.0, 0.0, 1.0),
];
function setColorSlider(newColorSlider){
var remainedcolorSlider=[];
for(var i=0;i<2;i++){
remainedcolorSlider.push(colorSlider[i]);
}
remainedcolorSlider.push(newColorSlider);
colorSlider=[];
colorSlider=remainedcolorSlider;
console.log(colorSlider);
glS.bindBuffer(glS.ARRAY_BUFFER, colorSliderBuffer);
glS.bufferData(glS.ARRAY_BUFFER, flatten(colorSlider), glS.STATIC_DRAW);
render();
}
var programSlider = initShaders(glS, "vertex-shaderSlider", "fragment-shaderSlider");
glS.useProgram(programSlider);
var bufferSlider = glS.createBuffer();
glS.bindBuffer(glS.ARRAY_BUFFER, bufferSlider);
glS.bufferData(glS.ARRAY_BUFFER, flatten(verticesSlider), glS.STATIC_DRAW);
var vPositionSlider = glS.getAttribLocation(programSlider, "vPositionC");
glS.vertexAttribPointer(vPositionSlider, 2, glS.FLOAT, false, 0, 0);
glS.enableVertexAttribArray(vPositionSlider);
var colorSliderBuffer = glS.createBuffer();
glS.bindBuffer(glS.ARRAY_BUFFER, colorSliderBuffer);
glS.bufferData(glS.ARRAY_BUFFER, flatten(colorSlider), glS.STATIC_DRAW);
var vertexColorSlider = glS.getAttribLocation(programSlider, "vertexColorC");
glS.vertexAttribPointer(vertexColorSlider, 4, glS.FLOAT, false, 0, 0);
glS.enableVertexAttribArray(vertexColorSlider);
sliderCanvas.addEventListener("click", function(event) {
render();
// Get click coordinates
var correction = event.target.getBoundingClientRect();
var t = vec2(event.clientX-correction.left,
sliderCanvas.height-(event.clientY-correction.top));
// Get color in look up map
selectedColorBuffer = new Uint8Array(4);
glS.bindFramebuffer(glS.FRAMEBUFFER, null);
glS.readPixels(t[0], t[1], 1, 1, glS.RGBA, glS.UNSIGNED_BYTE, selectedColorBuffer);
//glC.bindFramebuffer(gl.FRAMEBUFFER, null);
drawingColor=vec4(selectedColorBuffer[0]/255,selectedColorBuffer[1]/255,selectedColorBuffer[2]/255,1.0);
// console.log("selected " + drawingColor);
// console.log("cubeIndex = " + cubeIndex + ", faceIndex = " + faceIndex);
});
colorsCanvas.addEventListener("click", function(event) {
render();
// Get click coordinates
var correction = event.target.getBoundingClientRect();
var t = vec2(event.clientX-correction.left,
colorsCanvas.height-(event.clientY-correction.top));
// Get color in look up map
selectedColorBuffer = new Uint8Array(4);
glC.bindFramebuffer(glC.FRAMEBUFFER, null);
glC.readPixels(t[0], t[1], 1, 1, glC.RGBA, glC.UNSIGNED_BYTE, selectedColorBuffer);
//glC.bindFramebuffer(gl.FRAMEBUFFER, null);
// console.log(selectedColorBuffer);
drawingColor=vec4(selectedColorBuffer[0]/255,selectedColorBuffer[1]/255,selectedColorBuffer[2]/255,1.0);
// console.log(selectedColor);
// console.log("cubeIndex = " + cubeIndex + ", faceIndex = " + faceIndex);
setColorSlider(drawingColor);
});
render();
function render(){
glC.clear(glC.COLOR_BUFFER_BIT);
glC.drawArrays(glC.TRIANGLE_FAN, 0, nbTriangles + 2);
glS.clear(glS.COLOR_BUFFER_BIT);
glS.lineWidth(10);
glS.drawArrays(glS.TRIANGLES, 0, 3);
}
}