import QtQuick // Preset swatches, a hue strip and a hex field. It only reports the // picked color; saving it is up to whoever uses it. Item { id: root required property Theme theme required property color current signal picked(color value) implicitHeight: column.implicitHeight Component.onCompleted: root.showHex() // Leave the field alone while it is being typed in. onCurrentChanged: { if (!hexField.activeFocus) root.showHex(); } function showHex(): void { hexField.text = root.current.toString(); } // Keeps the current saturation and brightness, but not so low that // the hue would be lost in grey or black. function pickHue(x: real): void { const hue = Math.max(0, Math.min(0.999, x / hueBar.width)); const saturation = Math.max(root.current.hsvSaturation, 0.5); const value = Math.max(root.current.hsvValue, 0.6); root.picked(Qt.hsva(hue, saturation, value, 1)); } function pickHex(text: string): void { const hex = text.trim().replace(/^#?/, "#"); if (/^#[0-9a-fA-F]{6}$/.test(hex)) root.picked(hex); } Column { id: column width: root.width spacing: root.theme.popupSpacing Grid { columns: root.theme.swatchColumns spacing: root.theme.swatchSpacing Repeater { model: root.theme.swatches delegate: Rectangle { id: swatch required property color modelData readonly property bool chosen: Qt.colorEqual(swatch.modelData, root.current) width: root.theme.swatchSize height: root.theme.swatchSize radius: root.theme.swatchSize / 2 color: swatch.modelData border.width: swatch.chosen ? 2 : 1 border.color: swatch.chosen ? root.theme.textColor : root.theme.glassRimBottom MouseArea { anchors.fill: parent cursorShape: Qt.PointingHandCursor onClicked: root.picked(swatch.modelData) } } } } Rectangle { id: hueBar width: parent.width height: root.theme.hueHeight radius: root.theme.hueHeight / 2 gradient: Gradient { orientation: Gradient.Horizontal GradientStop { position: 0 color: "#ff0000" } GradientStop { position: 1 / 6 color: "#ffff00" } GradientStop { position: 2 / 6 color: "#00ff00" } GradientStop { position: 3 / 6 color: "#00ffff" } GradientStop { position: 4 / 6 color: "#0000ff" } GradientStop { position: 5 / 6 color: "#ff00ff" } GradientStop { position: 1 color: "#ff0000" } } // Marks the current hue. Greys have no hue, so it hides for them. Rectangle { width: 4 height: parent.height + 4 radius: 2 anchors.verticalCenter: parent.verticalCenter x: Math.max(0, root.current.hsvHue) * (hueBar.width - width) visible: root.current.hsvHue >= 0 color: root.theme.textColor border.width: 1 border.color: root.theme.buttonText } MouseArea { anchors.fill: parent cursorShape: Qt.PointingHandCursor onPressed: mouse => root.pickHue(mouse.x) onPositionChanged: mouse => root.pickHue(mouse.x) } } Item { width: parent.width height: root.theme.fieldHeight Rectangle { id: preview width: parent.height height: parent.height radius: root.theme.cornerRadius / 2 color: root.current border.width: 1 border.color: root.theme.glassRimTop } Item { anchors.left: preview.right anchors.leftMargin: root.theme.popupSpacing anchors.right: parent.right anchors.top: parent.top anchors.bottom: parent.bottom Glass { anchors.fill: parent theme: root.theme radius: root.theme.cornerRadius / 2 tint: root.theme.fieldBg selected: hexField.activeFocus shadow: false } TextInput { id: hexField anchors.fill: parent anchors.leftMargin: root.theme.fieldPadding anchors.rightMargin: root.theme.fieldPadding verticalAlignment: TextInput.AlignVCenter color: root.theme.textColor font.pixelSize: root.theme.fontSize font.family: "monospace" maximumLength: 7 selectByMouse: true selectionColor: root.theme.fieldBorder selectedTextColor: root.theme.buttonText clip: true onTextEdited: root.pickHex(hexField.text) // Tidy up a half-typed value when done. onEditingFinished: root.showHex() } } } } }