Skip to content

Commit c97e823

Browse files
add GradientOffset for rotating gradients by 45 degrees
1 parent 8867819 commit c97e823

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.smarttoolfactory.extendedcolors
2+
3+
import androidx.compose.ui.geometry.Offset
4+
5+
/**
6+
*
7+
* Get a [GradientOffset] that rotate a gradient clockwise with specified angle in degrees.
8+
* Default value for [GradientOffset] is [GradientAngle.CW0] which is 0 degrees
9+
* that returns a horizontal gradient.
10+
*
11+
* Get start and end offsets that are limited between [0f, Float.POSITIVE_INFINITY] in x and
12+
* y axes wrapped in [GradientOffset].
13+
* Infinity is converted to Composable width on x axis, height on y axis in shader.
14+
*
15+
* Default angle for [Brush.linearGradient] when no offset is 0 degrees in Compose ,
16+
* [Brush.verticalGradient] is [Brush.linearGradient] with 90 degrees.
17+
*
18+
* ```
19+
* 0 degrees
20+
* start = Offset(0f,0f),
21+
* end = Offset(Float.POSITIVE_INFINITY,0f)
22+
*
23+
* 45 degrees
24+
* start = Offset(0f, Float.POSITIVE_INFINITY),
25+
* end = Offset(Float.POSITIVE_INFINITY, 0f)
26+
*
27+
* 90 degrees
28+
* start = Offset(0f, Float.POSITIVE_INFINITY),
29+
* end = Offset.Zero
30+
*
31+
* 135 degrees
32+
* start = Offset.Infinity,
33+
* end = Offset.Zero
34+
*
35+
* 180 degrees
36+
* start = Offset(Float.POSITIVE_INFINITY, 0f),
37+
* end = Offset.Zero,
38+
*
39+
* ```
40+
*/
41+
fun GradientOffset(angle: GradientAngle = GradientAngle.CW0): GradientOffset {
42+
return when (angle) {
43+
GradientAngle.CW45 -> GradientOffset(
44+
start = Offset.Zero,
45+
end = Offset.Infinite
46+
)
47+
GradientAngle.CW90 -> GradientOffset(
48+
start = Offset.Zero,
49+
end = Offset(0f, Float.POSITIVE_INFINITY)
50+
)
51+
GradientAngle.CW135 -> GradientOffset(
52+
start = Offset(Float.POSITIVE_INFINITY, 0f),
53+
end = Offset(0f, Float.POSITIVE_INFINITY)
54+
)
55+
GradientAngle.CW180 -> GradientOffset(
56+
start = Offset(Float.POSITIVE_INFINITY, 0f),
57+
end = Offset.Zero,
58+
)
59+
GradientAngle.CW225 -> GradientOffset(
60+
start = Offset.Infinite,
61+
end = Offset.Zero
62+
)
63+
GradientAngle.CW270 -> GradientOffset(
64+
start = Offset(0f, Float.POSITIVE_INFINITY),
65+
end = Offset.Zero
66+
)
67+
GradientAngle.CW315 -> GradientOffset(
68+
start = Offset(0f, Float.POSITIVE_INFINITY),
69+
end = Offset(Float.POSITIVE_INFINITY, 0f)
70+
)
71+
else -> GradientOffset(
72+
start = Offset.Zero,
73+
end = Offset(Float.POSITIVE_INFINITY, 0f)
74+
)
75+
}
76+
}
77+
78+
/**
79+
* Offset for [Brush.linearGradient] to rotate gradient depending on [start] and [end] offsets.
80+
*/
81+
data class GradientOffset(val start: Offset, val end: Offset)
82+
83+
enum class GradientAngle {
84+
CW0, CW45, CW90, CW135, CW180, CW225, CW270, CW315
85+
}

0 commit comments

Comments
 (0)