话不多说,先上效果图:
以上demo完整项目源码路径:
https://github.com/lesliefish/QMLInAction/tree/master/demos%26projects/Components/ColorSelector
ColorSelector组件代码如下,有问题可以留言:
import QtQuick 2.0
import QtQuick.Controls 2.2
Item {
id:baseItem
width: 350
height: width
signal colorChanged(string newColor);
property int circleWidth: 40;//圆环宽度
property var curColor: undefined;
Rectangle {
id: control
width: baseItem.width
height: width
color: "transparent"
border.width: 1
border.color: "black"
anchors.margins: 10
// 根据角度获取颜色值
function getAngleColor(angle) {
var color, d;
if (angle < Math.PI * 2 / 5) { // angle: 0-72
d = 255 / (Math.PI * 2 / 5) * angle;
color = '255,' + Math.round(d) + ',0'; // color: 255,0,0 - 255,255,0
} else if (angle < Math.PI * 4 / 5) { // angle: 72-144
d = 255 / (Math.PI * 2 / 5) * (angle - Math.PI * 2 / 5);
color = (255 - Math.round(d)) + ',255,0'; // color: 255,255,0 - 0,255,0
} else if (angle < Math.PI * 6 / 5) { // angle: 144-216
d = 255 / (Math.PI * 2 / 5) * (angle - Math.PI * 4 / 5);
color = '0,255,' + Math.round(d); // color: 0,255,0 - 0,255,