-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNormalColorPicker.html
More file actions
202 lines (179 loc) · 5.97 KB
/
NormalColorPicker.html
File metadata and controls
202 lines (179 loc) · 5.97 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modern Color Picker</title>
<style>
body {
margin: 0;
padding: 20px;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.color-picker-container {
background: white;
border-radius: 15px;
padding: 25px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
width: 350px;
max-width: 100%;
}
.color-preview {
width: 100%;
height: 120px;
border-radius: 10px;
margin-bottom: 20px;
transition: background-color 0.3s ease;
border: 1px solid #eee;
}
.input-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
color: #333;
font-weight: 500;
}
input[type="color"] {
width: 100%;
height: 40px;
border: none;
border-radius: 5px;
cursor: pointer;
padding: 0;
}
input[type="text"] {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
font-size: 16px;
transition: border-color 0.3s ease;
}
input[type="text"]:focus {
outline: none;
border-color: #6c63ff;
}
.toggle-button {
background: #6c63ff;
color: white;
border: none;
padding: 8px 15px;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s ease;
margin-top: 10px;
width: 100%;
}
.toggle-button:hover {
background: #5a52cc;
}
.values-container {
display: flex;
justify-content: space-between;
gap: 10px;
}
.value-box {
flex: 1;
text-align: center;
padding: 10px;
background: #f8f9fa;
border-radius: 5px;
font-size: 14px;
color: #666;
}
</style>
</head>
<body>
<div class="color-picker-container">
<div class="color-preview" id="colorPreview"></div>
<div class="input-group">
<label for="colorInput">Select Color</label>
<input type="color" id="colorInput" value="#6c63ff">
</div>
<div class="input-group">
<label for="colorValue">Color Value</label>
<input type="text" id="colorValue" value="#6c63ff">
</div>
<button class="toggle-button" id="toggleFormat">Switch to RGB</button>
<div class="values-container">
<div class="value-box">R: <span id="rValue">108</span></div>
<div class="value-box">G: <span id="gValue">99</span></div>
<div class="value-box">B: <span id="bValue">255</span></div>
</div>
</div>
<script>
const colorInput = document.getElementById('colorInput');
const colorPreview = document.getElementById('colorPreview');
const colorValue = document.getElementById('colorValue');
const toggleButton = document.getElementById('toggleFormat');
const rValue = document.getElementById('rValue');
const gValue = document.getElementById('gValue');
const bValue = document.getElementById('bValue');
let isHexFormat = true;
function hexToRgb(hex) {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
return { r, g, b };
}
function rgbToHex(r, g, b) {
return '#' + [r, g, b]
.map(x => x.toString(16).padStart(2, '0'))
.join('');
}
function updateDisplay(color) {
colorPreview.style.backgroundColor = color;
const rgb = hexToRgb(color);
rValue.textContent = rgb.r;
gValue.textContent = rgb.g;
bValue.textContent = rgb.b;
if (isHexFormat) {
colorValue.value = color;
} else {
colorValue.value = `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`;
}
}
colorInput.addEventListener('input', (e) => {
updateDisplay(e.target.value);
});
colorValue.addEventListener('input', (e) => {
let value = e.target.value;
let validColor = false;
if (isHexFormat) {
if (/^#[0-9A-Fa-f]{6}$/.test(value)) {
validColor = true;
}
} else {
const rgbMatch = value.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/);
if (rgbMatch) {
const r = parseInt(rgbMatch[1]);
const g = parseInt(rgbMatch[2]);
const b = parseInt(rgbMatch[3]);
if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255) {
value = rgbToHex(r, g, b);
validColor = true;
}
}
}
if (validColor) {
colorInput.value = value;
updateDisplay(value);
}
});
toggleButton.addEventListener('click', () => {
isHexFormat = !isHexFormat;
toggleButton.textContent = isHexFormat ? 'Switch to RGB' : 'Switch to HEX';
updateDisplay(colorInput.value);
});
// Initial update
updateDisplay(colorInput.value);
</script>
</body>
</html>