made gradient rotatable
This commit is contained in:
@@ -1,23 +1,75 @@
|
|||||||
import QtQuick
|
import QtQuick
|
||||||
|
import QtQuick.Shapes
|
||||||
|
|
||||||
Gradient {
|
Item {
|
||||||
id: root
|
id: root
|
||||||
|
|
||||||
required property color color
|
property real angle: 0 // Degrees; 0 = transparent left, colour right
|
||||||
property double midpoint: 0.6
|
readonly property real angleRadians: angle * Math.PI / 180.0
|
||||||
|
property real blur: 0.0 // 0 = sharp, 1 = very soft
|
||||||
|
|
||||||
GradientStop {
|
// Public API
|
||||||
color: "transparent"
|
property color color: "#4da3ffff"
|
||||||
position: 0.0
|
|
||||||
|
// Half the diagonal ensures the gradient covers the entire item.
|
||||||
|
readonly property real gradientRadius: Math.sqrt(width * width + height * height) / 2.0
|
||||||
|
property real midpoint: 0.5 // 0..1
|
||||||
|
property double radius: 10
|
||||||
|
readonly property real safeBlur: Math.max(blur, 0.00001)
|
||||||
|
readonly property real safeMidpoint: clamp01(midpoint)
|
||||||
|
readonly property real transitionEnd: clamp01(safeMidpoint + safeBlur / 2.0)
|
||||||
|
readonly property real transitionStart: clamp01(safeMidpoint - safeBlur / 2.0)
|
||||||
|
readonly property color transparentColor: Qt.rgba(color.r, color.g, color.b, 0.0)
|
||||||
|
|
||||||
|
function clamp01(value) {
|
||||||
|
return Math.max(0.0, Math.min(1.0, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
GradientStop {
|
Behavior on color {
|
||||||
color: "transparent"
|
ColorAnimation {
|
||||||
position: root.midpoint
|
duration: 100
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GradientStop {
|
Shape {
|
||||||
color: root.color
|
anchors.fill: parent
|
||||||
position: 1.0
|
preferredRendererType: Shape.CurveRenderer
|
||||||
|
|
||||||
|
ShapePath {
|
||||||
|
strokeWidth: -1
|
||||||
|
|
||||||
|
fillGradient: LinearGradient {
|
||||||
|
x1: root.width / 2.0 - root.gradientRadius * Math.cos(root.angleRadians)
|
||||||
|
x2: root.width / 2.0 + root.gradientRadius * Math.cos(root.angleRadians)
|
||||||
|
y1: root.height / 2.0 - root.gradientRadius * Math.sin(root.angleRadians)
|
||||||
|
y2: root.height / 2.0 + root.gradientRadius * Math.sin(root.angleRadians)
|
||||||
|
|
||||||
|
GradientStop {
|
||||||
|
color: root.transparentColor
|
||||||
|
position: 0.0
|
||||||
|
}
|
||||||
|
|
||||||
|
GradientStop {
|
||||||
|
color: root.transparentColor
|
||||||
|
position: root.transitionStart
|
||||||
|
}
|
||||||
|
|
||||||
|
GradientStop {
|
||||||
|
color: root.color
|
||||||
|
position: root.transitionEnd
|
||||||
|
}
|
||||||
|
|
||||||
|
GradientStop {
|
||||||
|
color: root.color
|
||||||
|
position: 1.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PathRectangle {
|
||||||
|
height: root.height
|
||||||
|
radius: Math.min(root.radius, Math.min(width, height) / 2.0)
|
||||||
|
width: root.width
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user