-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
325 lines (273 loc) · 11.1 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import './style.css';
import { hsvToRgb, rgbToHsv } from './utils/colorConversion.js';
import { RGBCube } from './components/RGBCube.js';
import { HSVCone } from './components/HSVCone.js';
class ColorVisualizerApp {
constructor() {
// RGBキューブ用のプロパティ
this.rgbScene = new THREE.Scene();
this.rgbCamera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);
this.rgbRenderer = new THREE.WebGLRenderer();
this.rgbControls = null;
this.rgbCube = new RGBCube();
// HSVコーン用のプロパティ
this.hsvScene = new THREE.Scene();
this.hsvCamera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000);
this.hsvRenderer = new THREE.WebGLRenderer();
this.hsvControls = null;
this.hsvCone = new HSVCone();
this.initialize();
}
initialize() {
// レンダラーの設定
this.setupRenderers();
// カメラの設定
this.setupCameras();
// コントロールの設定
this.setupControls();
// ヘルパーの追加
this.addHelpers();
// UIの作成
this.createUI();
// シーンへの追加
this.rgbScene.add(this.rgbCube.getGroup());
this.hsvScene.add(this.hsvCone.getGroup());
// イベントリスナーの設定
this.setupEventListeners();
// アニメーションの開始
this.animate();
// 初期値の設定と表示
const initialR = 1;
const initialG = 1;
const initialB = 1;
this.updateRGBSliders(initialR, initialG, initialB);
this.rgbCube.updateMarkerPosition(initialR, initialG, initialB);
const [h, s, v] = rgbToHsv(initialR * 255, initialG * 255, initialB * 255);
this.updateHSVSliders(h, s, v);
const color = new THREE.Color(initialR, initialG, initialB);
this.hsvCone.updateMarkerPosition(h, s, v, color);
this.updateColorDisplay(initialR, initialG, initialB);
}
setupRenderers() {
// RGBレンダラーの設定
this.rgbRenderer.setSize(400, 400);
this.rgbRenderer.setClearColor(0x242424);
// HSVレンダラーの設定
this.hsvRenderer.setSize(400, 400);
this.hsvRenderer.setClearColor(0x242424);
}
setupCameras() {
// RGBカメラの設定
this.rgbCamera.position.set(2, 1.5, 2); // より近くに配置
// HSVカメラの設定
this.hsvCamera.position.set(1.1, 1.1, 1.1); // より近くに配置
}
setupControls() {
// RGBコントロールの設定
this.rgbControls = new OrbitControls(this.rgbCamera, this.rgbRenderer.domElement);
this.setupControlsCommon(this.rgbControls);
// HSVコントロールの設定
this.hsvControls = new OrbitControls(this.hsvCamera, this.hsvRenderer.domElement);
this.setupControlsCommon(this.hsvControls);
}
setupControlsCommon(controls) {
controls.mouseButtons = {
LEFT: THREE.MOUSE.ROTATE,
MIDDLE: THREE.MOUSE.PAN,
RIGHT: THREE.MOUSE.DOLLY
};
controls.touches = {
ONE: THREE.TOUCH.ROTATE,
TWO: THREE.TOUCH.PAN
};
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.panSpeed = 1.0;
controls.rotateSpeed = 0.8;
controls.zoomSpeed = 1.2;
controls.enableZoom = true;
}
addHelpers() {
// RGBシーンにヘルパーを追加
const rgbGridHelper = new THREE.GridHelper(1, 10);
rgbGridHelper.position.set(0.5, 0, 0.5);
this.rgbScene.add(rgbGridHelper);
const rgbAxesHelper = new THREE.AxesHelper(1.2);
this.rgbScene.add(rgbAxesHelper);
this.addAxisLabels(this.rgbScene);
// HSVシーンのヘルパーを削除
// const hsvGridHelper = new THREE.GridHelper(1, 10);
// this.hsvScene.add(hsvGridHelper);
// const hsvAxesHelper = new THREE.AxesHelper(1.2);
// this.hsvScene.add(hsvAxesHelper);
}
addAxisLabels(scene) {
const createLabel = (text, position) => {
const canvas = document.createElement('canvas');
canvas.width = 64;
canvas.height = 32;
const context = canvas.getContext('2d');
context.fillStyle = '#ffffff';
context.font = '24px Arial';
context.fillText(text, 0, 24);
const texture = new THREE.CanvasTexture(canvas);
const material = new THREE.SpriteMaterial({ map: texture });
const sprite = new THREE.Sprite(material);
sprite.position.copy(position);
sprite.scale.set(0.2, 0.1, 1);
return sprite;
};
scene.add(createLabel('R', new THREE.Vector3(1.3, 0, 0)));
scene.add(createLabel('G', new THREE.Vector3(0, 1.3, 0)));
scene.add(createLabel('B', new THREE.Vector3(0, 0, 1.3)));
}
createUI() {
const appDiv = document.querySelector('#app');
appDiv.innerHTML = `
<div class="visualizer-container">
<div class="canvas-container">
<div id="rgb-canvas-container"></div>
<div id="hsv-canvas-container"></div>
</div>
<div class="controls">
<div class="control-section">
<h3>RGB</h3>
<div class="slider-group">
<label>R: <span id="r-value">0</span></label>
<input type="range" id="r-slider" min="1" max="256" step="1" value="1">
</div>
<div class="slider-group">
<label>G: <span id="g-value">0</span></label>
<input type="range" id="g-slider" min="1" max="256" step="1" value="1">
</div>
<div class="slider-group">
<label>B: <span id="b-value">0</span></label>
<input type="range" id="b-slider" min="1" max="256" step="1" value="1">
</div>
</div>
<div class="control-section">
<h3>HSV</h3>
<div class="slider-group">
<label>H: <span id="h-value">0</span>°</label>
<input type="range" id="h-slider" min="0" max="360" step="1" value="0">
</div>
<div class="slider-group">
<label>S: <span id="s-value">0</span>%</label>
<input type="range" id="s-slider" min="0" max="100" step="1" value="0">
</div>
<div class="slider-group">
<label>V: <span id="v-value">0</span>%</label>
<input type="range" id="v-slider" min="0" max="100" step="1" value="0">
</div>
</div>
<div class="color-display-container">
<div id="current-color"></div>
<button id="copy-color-btn">Copy to Clipboard</button>
</div>
</div>
</div>
`;
// レンダラーの追加
document.getElementById('rgb-canvas-container').appendChild(this.rgbRenderer.domElement);
document.getElementById('hsv-canvas-container').appendChild(this.hsvRenderer.domElement);
}
setupEventListeners() {
// RGBスライダーのイベントリスナー
const rSlider = document.getElementById('r-slider');
const gSlider = document.getElementById('g-slider');
const bSlider = document.getElementById('b-slider');
const updateRGBValues = () => {
const r = rSlider.value / 255;
const g = gSlider.value / 255;
const b = bSlider.value / 255;
document.getElementById('r-value').textContent = Math.round(r * 255);
document.getElementById('g-value').textContent = Math.round(g * 255);
document.getElementById('b-value').textContent = Math.round(b * 255);
// マーカーの位置を更新
this.rgbCube.updateMarkerPosition(r, g, b);
// HSV値を計算して更新
const [h, s, v] = rgbToHsv(r * 255, g * 255, b * 255);
this.updateHSVSliders(h, s, v);
// HSVコーンのマーカーを更新
const color = new THREE.Color(r, g, b);
this.hsvCone.updateMarkerPosition(h, s, v, color);
// カラーディスプレイを更新
this.updateColorDisplay(r, g, b);
};
rSlider.addEventListener('input', updateRGBValues);
gSlider.addEventListener('input', updateRGBValues);
bSlider.addEventListener('input', updateRGBValues);
// HSVスライダーのイベントリスナー
const hSlider = document.getElementById('h-slider');
const sSlider = document.getElementById('s-slider');
const vSlider = document.getElementById('v-slider');
const updateHSVValues = () => {
const h = parseFloat(hSlider.value);
const s = parseFloat(sSlider.value);
const v = parseFloat(vSlider.value);
document.getElementById('h-value').textContent = Math.round(h);
document.getElementById('s-value').textContent = Math.round(s);
document.getElementById('v-value').textContent = Math.round(v);
// RGB値を計算
const [r, g, b] = hsvToRgb(h, s, v);
// RGBスライダーを更新
this.updateRGBSliders(r, g, b);
// マーカーの位置を更新
this.rgbCube.updateMarkerPosition(r, g, b);
// HSVコーンのマーカーを更新
const color = new THREE.Color(r, g, b);
this.hsvCone.updateMarkerPosition(h, s, v, color);
// カラーディスプレイを更新
this.updateColorDisplay(r, g, b);
};
hSlider.addEventListener('input', updateHSVValues);
sSlider.addEventListener('input', updateHSVValues);
vSlider.addEventListener('input', updateHSVValues);
}
updateRGBSliders(r, g, b) {
document.getElementById('r-slider').value = r * 255;
document.getElementById('g-slider').value = g * 255;
document.getElementById('b-slider').value = b * 255;
document.getElementById('r-value').textContent = Math.round(r * 255);
document.getElementById('g-value').textContent = Math.round(g * 255);
document.getElementById('b-value').textContent = Math.round(b * 255);
}
updateHSVSliders(h, s, v) {
document.getElementById('h-slider').value = h;
document.getElementById('s-slider').value = s;
document.getElementById('v-slider').value = v;
document.getElementById('h-value').textContent = Math.round(h);
document.getElementById('s-value').textContent = Math.round(s);
document.getElementById('v-value').textContent = Math.round(v);
}
updateColorDisplay(r, g, b) {
const colorDisplay = document.getElementById('current-color');
const rgbValue = `rgb(${Math.round(r * 255)}, ${Math.round(g * 255)}, ${Math.round(b * 255)})`;
colorDisplay.style.backgroundColor = rgbValue;
colorDisplay.style.width = '100px';
colorDisplay.style.height = '100px';
// コピーボタンのイベントリスナーを設定
const copyButton = document.getElementById('copy-color-btn');
copyButton.onclick = () => {
navigator.clipboard.writeText(rgbValue)
.then(() => {
alert('Color code copied to clipboard!');
})
.catch(err => {
console.error('Failed to copy to clipboard:', err);
alert('Copy failed.');
});
};
}
animate() {
requestAnimationFrame(() => this.animate());
this.rgbControls.update();
this.hsvControls.update();
this.rgbRenderer.render(this.rgbScene, this.rgbCamera);
this.hsvRenderer.render(this.hsvScene, this.hsvCamera);
}
}
// アプリケーションの起動
const app = new ColorVisualizerApp();