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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Item {
id: colorPicker
property bool isHovered: false
property alias color: root.color
width: 100
height: 26
Rectangle {
id: border
color: "transparent"
border.width: 2
border.color: colorPicker.isHovered ? palette.dark : palette.alternateBase
anchors.fill: parent
Image {
anchors.fill: parent
anchors.margins: 4
source: "images/grid_8x8.png"
fillMode: Image.Tile
}
Rectangle {
anchors.fill: parent
anchors.margins: 4
color: colorPicker.color
}
MouseArea {
anchors.fill: parent
hoverEnabled: true
onEntered: colorPicker.isHovered = true
onExited: colorPicker.isHovered = false
onClicked: {
colorPickerPopup.open()
}
}
}
Dialog {
id: colorPickerPopup
title: "Color Picker"
anchors.centerIn: Overlay.overlay
modal: true
focus: true
closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutsideParent
Pane {
RowLayout {
id: root
property color color: "red"
Item {
implicitWidth: 135
implicitHeight: 135
ShaderEffect {
id: hsvWheel
anchors.centerIn: parent
blending: true
width: 128
height: 128
property real value: 1.0
fragmentShader: "shaders/huesaturation.frag.qsb"
Item {
id: reticule
function reflectColor(hue, saturation) {
let angleDegrees = hue * 360
if (angleDegrees > 180)
angleDegrees = angleDegrees - 360
let angleRadians = angleDegrees * (Math.PI / 180)
let vector = Qt.vector2d(Math.cos(angleRadians), Math.sin(angleRadians)).normalized().times(0.5).times(saturation)
vector = vector.plus(Qt.vector2d(0.5, 0.5))
reticule.x = vector.x * hsvWheel.width
reticule.y = vector.y * hsvWheel.width
}
Rectangle {
x: -5
y: -5
width: 10
height: 10
radius: 5
color: "transparent"
border.width: 1
border.color: "black"
Rectangle {
x: 0.5
y: 0.5
width: 9
height: 9
radius: 4.5
color: root.color
border.width: 1
border.color: "white"
}
}
}
MouseArea {
anchors.fill: parent
preventStealing: true
function handleMouseMove(x : real, y : real, width : real) : vector2d {
let normalizedX = x / width - 0.5;
let normalizedY = y / width - 0.5;
let angle = Math.atan2(normalizedY, normalizedX);
let toCenter = Qt.vector2d(normalizedX, normalizedY);
let radius = toCenter.length() * 2.0;
let degrees = angle * (180 / Math.PI)
if (degrees < 0)
degrees = 360 + degrees
let hue = degrees / 360
root.color = Qt.hsva(hue, radius, root.color.hsvValue, root.color.a)
if (radius <= 1.0)
return Qt.vector2d(x, y)
// Limit to radius of 1.0
toCenter = toCenter.normalized();
let halfWidth = width * 0.5;
let newX = halfWidth * toCenter.x + halfWidth
let newY = halfWidth * toCenter.y + halfWidth
return Qt.vector2d(newX, newY)
}
onClicked: (mouse) => {
let pos = handleMouseMove(mouse.x, mouse.y, hsvWheel.width)
reticule.x = pos.x;
reticule.y = pos.y;
}
onPositionChanged: (mouse) => {
let pos = handleMouseMove(mouse.x, mouse.y, hsvWheel.width)
reticule.x = pos.x;
reticule.y = pos.y;
}
}
}
}
Component.onCompleted: {
updateColorSections()
reticule.reflectColor(root.color.hsvHue, root.color.hsvSaturation)
}
Connections {
target: root
function onColorChanged() {
root.updateColorSections()
}
}
function updateColorSections() {
rgbSection.redValue = root.color.r * 255
rgbSection.greenValue = root.color.g * 255
rgbSection.blueValue = root.color.b * 255
hsvSection.hueValue = root.color.hsvHue * 360
hsvSection.saturationValue = root.color.hsvSaturation * 100
hsvSection.valueValue = root.color.hsvValue * 100
alphaSection.alphaValue = root.color.a * 255
}
ColumnLayout {
SectionLayout {
title: "RGB"
id: rgbSection
property int redValue: 0
property int greenValue: 0
property int blueValue: 0
implicitWidth: 300
function updateRGB() {
root.color = Qt.rgba(redValue / 255, greenValue / 255, blueValue / 255, alphaSection.alphaValue / 255)
reticule.reflectColor(root.color.hsvHue, root.color.hsvSaturation)
}
onRedValueChanged: hexTextField.updateText()
onGreenValueChanged: hexTextField.updateText()
onBlueValueChanged: hexTextField.updateText()
RowLayout {
Layout.fillWidth: true
Label {
text: "R:"
}
Slider {
id: redSlider
Layout.fillWidth: true
from: 0
to: 255
value: rgbSection.redValue
onValueChanged: {
if (value !== rgbSection.redValue) {
rgbSection.redValue = value
if (pressed)
rgbSection.updateRGB()
}
}
}
}
SpinBox {
Layout.fillWidth: true
from: 0
to: 255
value: rgbSection.redValue
onValueChanged: {
if (value !== rgbSection.redValue)
rgbSection.redValue = value
}
onValueModified: {
rgbSection.updateRGB();
}
}
RowLayout {
Layout.fillWidth: true
Label {
text: "G:"
}
Slider {
id: greenSlider
Layout.fillWidth: true
from: 0
to: 255
value: rgbSection.greenValue
onValueChanged: {
if (value !== rgbSection.greenValue) {
rgbSection.greenValue = value
if (pressed)
rgbSection.updateRGB()
}
}
}
}
SpinBox {
Layout.fillWidth: true
from: 0
to: 255
value: rgbSection.greenValue
onValueChanged: {
if (value !== rgbSection.greenValue)
rgbSection.greenValue = value
}
onValueModified: {
rgbSection.updateRGB()
}
}
RowLayout {
Layout.fillWidth: true
Label {
text: "B:"
}
Slider {
id: blueSlider
Layout.fillWidth: true
from: 0
to: 255
value: rgbSection.blueValue
onValueChanged: {
if (value !== rgbSection.blueValue) {
rgbSection.blueValue = value
if (pressed)
rgbSection.updateRGB()
}
}
}
}
SpinBox {
Layout.fillWidth: true
from: 0
to: 255
value: rgbSection.blueValue
onValueChanged: {
if (value !== rgbSection.blueValue)
rgbSection.blueValue = value
}
onValueModified: {
rgbSection.updateRGB()
}
}
Label {
text: "Hex:"
Layout.fillWidth: true
}
TextField {
id: hexTextField
maximumLength: 6
Layout.alignment: Qt.AlignRight
function updateText() {
if (activeFocus)
return
let redText = rgbSection.redValue.toString(16).toUpperCase()
let greenText = rgbSection.greenValue.toString(16).toUpperCase()
let blueText = rgbSection.blueValue.toString(16).toUpperCase()
if (redText.length == 1)
redText = "0" + redText
if (greenText.length == 1)
greenText = "0" + greenText
if (blueText.length == 1)
blueText = "0" + blueText
text = redText + greenText + blueText;
}
function expandText(text) {
let newText = text.toUpperCase()
let expandLength = 6 - newText.length
for (let i = 0; i < expandLength; ++i)
newText = "0" + newText
return newText
}
Component.onCompleted: updateText()
validator: RegularExpressionValidator {
regularExpression: /^[0-9A-Fa-f]{0,6}$/
}
onTextChanged: {
if (!acceptableInput)
return;
let colorText = expandText(text)
rgbSection.redValue = parseInt(colorText.substr(0, 2), 16)
rgbSection.greenValue = parseInt(colorText.substr(2, 2), 16)
rgbSection.blueValue = parseInt(colorText.substr(4, 2), 16)
if (activeFocus)
rgbSection.updateRGB()
}
onAccepted: {
text = expandText(text)
}
}
}
SectionLayout {
title: "HSV"
id: hsvSection
property int hueValue: 0
property int saturationValue: 0
property int valueValue: 0
implicitWidth: 300
function updateHSV() {
root.color = Qt.hsva(hueValue / 360, saturationValue / 100, valueValue / 100, alphaSection.alphaValue / 255)
reticule.reflectColor(root.color.hsvHue, root.color.hsvSaturation)
}
RowLayout {
Layout.fillWidth: true
Label {
text: "H:"
}
Slider {
id: hueSlider
Layout.fillWidth: true
from: 0
to: 360
value: hsvSection.hueValue
onValueChanged: {
if (value !== hsvSection.hueValue) {
hsvSection.hueValue = value
if (pressed)
hsvSection.updateHSV()
}
}
}
}
SpinBox {
Layout.fillWidth: true
from: 0
to: 360
value: hsvSection.hueValue
onValueChanged: {
if (value !== hsvSection.hueValue)
hsvSection.hueValue = value
}
onValueModified: {
hsvSection.updateHSV()
}
}
RowLayout {
Layout.fillWidth: true
Label {
text: "S:"
}
Slider {
id: saturationSlider
Layout.fillWidth: true
from: 0
to: 100
value: hsvSection.saturationValue
onValueChanged: {
if (value !== hsvSection.saturationValue) {
hsvSection.saturationValue = value
if (pressed)
hsvSection.updateHSV()
}
}
}
}
SpinBox {
Layout.fillWidth: true
from: 0
to: 100
value: hsvSection.saturationValue
onValueChanged: {
if (value !== hsvSection.saturationValue)
hsvSection.saturationValue = value
}
onValueModified: {
hsvSection.updateHSV()
}
}
RowLayout {
Layout.fillWidth: true
Label {
text: "V:"
}
Slider {
id: valueSlider
Layout.fillWidth: true
from: 0
to: 100
value: hsvSection.valueValue
onValueChanged: {
if (value !== hsvSection.valueValue) {
hsvSection.valueValue = value
if (pressed)
hsvSection.updateHSV()
}
}
}
}
SpinBox {
Layout.fillWidth: true
from: 0
to: 100
value: hsvSection.valueValue
onValueChanged: {
if (value !== hsvSection.valueValue)
hsvSection.valueValue = value
}
onValueModified: {
hsvSection.updateHSV()
}
}
}
SectionLayout {
title: "Opacity / Alpha"
id: alphaSection
property int alphaValue: 0
implicitWidth: 300
RowLayout {
Layout.fillWidth: true
Label {
text: "V:"
}
Slider {
id: alphaSlider
Layout.fillWidth: true
from: 0
to: 255
value: alphaSection.alphaValue
onValueChanged: {
if (value !== alphaSection.alphaValue) {
alphaSection.alphaValue = value
if (pressed)
hsvSection.updateHSV()
}
}
}
}
SpinBox {
Layout.fillWidth: true
from: 0
to: 255
value: alphaSection.alphaValue
onValueChanged: {
if (value !== alphaSection.alphaValue)
alphaSection.alphaValue = value
}
onValueModified: {
hsvSection.updateHSV()
}
}
}
}
}
}
}
}
|