Skip to content

Commit b0a7c07

Browse files
move color conversion functions to separate files
1 parent 942f2ba commit b0a7c07

File tree

7 files changed

+1156
-1107
lines changed

7 files changed

+1156
-1107
lines changed

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/model/GradientColorState.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import androidx.compose.ui.unit.DpSize
1111
import com.smarttoolfactory.colorpicker.selector.gradient.GradientType
1212
import com.smarttoolfactory.colorpicker.ui.GradientAngle
1313
import com.smarttoolfactory.colorpicker.ui.GradientOffset
14+
import com.smarttoolfactory.colorpicker.util.colorToHexAlpha
1415

1516
@Composable
1617
fun rememberGradientColorState(
@@ -47,6 +48,11 @@ class GradientColorState internal constructor(initialColor: Color, size: Size) {
4748
var size by mutableStateOf(size)
4849
var color: Color = initialColor
4950

51+
val hexString: String
52+
get() {
53+
return colorToHexAlpha(color)
54+
}
55+
5056
val brush: Brush
5157
get() {
5258

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
package com.smarttoolfactory.colorpicker.util
2+
3+
import androidx.compose.ui.graphics.Color
4+
import androidx.compose.ui.graphics.toArgb
5+
import androidx.core.graphics.ColorUtils
6+
7+
8+
/**
9+
* Convert Jetpack Compose [Color] to HSV (hue-saturation-value) components.
10+
* ```
11+
* Hue is [0 .. 360)
12+
* Saturation is [0...1]
13+
* Value is [0...1]
14+
* ```
15+
* @param hslIn 3 element array which holds the input HSL components.
16+
*/
17+
fun colorToHSV(color: Color, hslIn: FloatArray) {
18+
val rgbArray: IntArray = colorIntToRGBArray(color.toArgb())
19+
val red = rgbArray[0]
20+
val green = rgbArray[1]
21+
val blue = rgbArray[2]
22+
rgbToHSV(red, green, blue, hslIn)
23+
}
24+
25+
/**
26+
* Convert Jetpack Compose [Color] to HSV (hue-saturation-value) components.
27+
* ```
28+
* Hue is [0 .. 360)
29+
* Saturation is [0...1]
30+
* Value is [0...1]
31+
* ```
32+
*/
33+
fun colorToHSV(color: Color): FloatArray {
34+
val rgbArray: IntArray = colorIntToRGBArray(color.toArgb())
35+
val red = rgbArray[0]
36+
val green = rgbArray[1]
37+
val blue = rgbArray[2]
38+
return rgbToHSV(red, green, blue)
39+
}
40+
41+
/**
42+
* Convert Jetpack Compose [Color] to HSV (hue-saturation-value) components.
43+
* ```
44+
* hsl[0] is Hue [0 .. 360)
45+
* hsl[1] is Saturation [0...1]
46+
* hsl[2] is Lightness [0...1]
47+
* ```
48+
* @param hslIn 3 element array which holds the input HSL components.
49+
*/
50+
fun colorToHSL(color: Color, hslIn: FloatArray) {
51+
val rgbArray: IntArray = colorIntToRGBArray(color.toArgb())
52+
val red = rgbArray[0]
53+
val green = rgbArray[1]
54+
val blue = rgbArray[2]
55+
rgbToHSL(red, green, blue, hslIn)
56+
}
57+
58+
/**
59+
* Convert Jetpack Compose [Color] to HSV (hue-saturation-value) components.
60+
* ```
61+
* hsl[0] is Hue [0 .. 360)
62+
* hsl[1] is Saturation [0...1]
63+
* hsl[2] is Lightness [0...1]
64+
* ```
65+
*/
66+
fun colorToHSL(color: Color): FloatArray {
67+
val rgbArray: IntArray = colorIntToRGBArray(color.toArgb())
68+
val red = rgbArray[0]
69+
val green = rgbArray[1]
70+
val blue = rgbArray[2]
71+
return rgbToHSL(red, green, blue)
72+
}
73+
74+
/*
75+
COLOR-RGB Conversions
76+
*/
77+
78+
/**
79+
* Convert Jetpack [Color] into 3 element array of red, green, and blue
80+
*```
81+
* rgb[0] is Red [0 .. 255]
82+
* rgb[1] is Green [0...255]
83+
* rgb[2] is Blue [0...255]
84+
* ```
85+
* @param color Jetpack Compose [Color]
86+
* @return 3 element array which holds the input RGB components.
87+
*/
88+
fun colorToARGBArray(color: Color): IntArray {
89+
return colorIntToRGBArray(color.toArgb())
90+
}
91+
92+
/**
93+
* Convert Jetpack [Color] into 3 element array of red, green, and blue
94+
*```
95+
* rgb[0] is Red [0 .. 255]
96+
* rgb[1] is Green [0...255]
97+
* rgb[2] is Blue [0...255]
98+
* ```
99+
* @param color Jetpack Compose [Color]
100+
* @param rgbIn 3 element array which holds the input RGB components.
101+
*/
102+
fun colorToRGBArray(color: Color, rgbIn: IntArray) {
103+
val argbArray = colorIntToRGBArray(color.toArgb())
104+
rgbIn[0] = argbArray[0]
105+
rgbIn[1] = argbArray[1]
106+
rgbIn[2] = argbArray[2]
107+
}
108+
109+
110+
fun colorToHex(color: Color): String {
111+
return rgbToHex(color.red, color.green, color.blue)
112+
}
113+
114+
fun colorToHexAlpha(color: Color): String {
115+
return argbToHex(color.alpha, color.red, color.green, color.blue)
116+
}
117+
118+
/**
119+
* Convert a RGB color in [Integer] form to HSV (hue-saturation-value) components.
120+
* * For instance, red =255, green =0, blue=0 is -65536
121+
* ```
122+
* hsv[0] is Hue [0 .. 360)
123+
* hsv[1] is Saturation [0...1]
124+
* hsv[2] is Value [0...1]
125+
* ```
126+
*/
127+
fun colorIntToHSV(color: Int): FloatArray {
128+
val hsvOut = floatArrayOf(0f, 0f, 0f)
129+
android.graphics.Color.colorToHSV(color, hsvOut)
130+
return hsvOut
131+
}
132+
133+
/**
134+
* Convert a RGB color in [Integer] form to HSV (hue-saturation-value) components.
135+
* * For instance, red =255, green =0, blue=0 is -65536
136+
*
137+
* ```
138+
* hsv[0] is Hue [0 .. 360)
139+
* hsv[1] is Saturation [0...1]
140+
* hsv[2] is Value [0...1]
141+
* ```
142+
* @param hsvIn 3 element array which holds the input HSV components.
143+
*/
144+
fun colorIntToHSV(color: Int, hsvIn: FloatArray) {
145+
android.graphics.Color.colorToHSV(color, hsvIn)
146+
}
147+
148+
149+
/**
150+
* Convert RGB color [Integer] to HSL (hue-saturation-lightness) components.
151+
* ```
152+
* hsl[0] is Hue [0 .. 360)
153+
* hsl[1] is Saturation [0...1]
154+
* hsl[2] is Lightness [0...1]
155+
* ```
156+
*/
157+
fun colorIntToHSL(color: Int): FloatArray {
158+
val hslOut = floatArrayOf(0f, 0f, 0f)
159+
ColorUtils.colorToHSL(color, hslOut)
160+
return hslOut
161+
}
162+
163+
/**
164+
* Convert RGB color [Integer] to HSL (hue-saturation-lightness) components.
165+
* ```
166+
* hsl[0] is Hue [0 .. 360)
167+
* hsl[1] is Saturation [0...1]
168+
* hsl[2] is Lightness [0...1]
169+
* ```
170+
*/
171+
fun colorIntToHSL(color: Int, hslIn: FloatArray) {
172+
ColorUtils.colorToHSL(color, hslIn)
173+
}
174+
175+
176+
/*
177+
ColorInt-RGB Conversions
178+
*/
179+
/**
180+
* Convert Color [Integer] into 3 element array of red, green, and blue
181+
*```
182+
* rgb[0] is Red [0 .. 255]
183+
* rgb[1] is Green [0...255]
184+
* rgb[2] is Blue [0...255]
185+
* ```
186+
* @return 3 element array which holds the input RGB components.
187+
*/
188+
fun colorIntToRGBArray(color: Int): IntArray {
189+
val red = android.graphics.Color.red(color)
190+
val green = android.graphics.Color.green(color)
191+
val blue = android.graphics.Color.blue(color)
192+
return intArrayOf(red, green, blue)
193+
}
194+
195+
/**
196+
* Convert Color [Integer] into 3 element array of red, green, and blue
197+
*```
198+
* rgb[0] is Red [0 .. 255]
199+
* rgb[1] is Green [0...255]
200+
* rgb[2] is Blue [0...255]
201+
* ```
202+
* @param rgbIn 3 element array which holds the input RGB components.
203+
*/
204+
fun colorIntToRGBArray(color: Int, rgbIn: IntArray) {
205+
val red = android.graphics.Color.red(color)
206+
val green = android.graphics.Color.green(color)
207+
val blue = android.graphics.Color.blue(color)
208+
209+
rgbIn[0] = red
210+
rgbIn[1] = green
211+
rgbIn[2] = blue
212+
}
213+
214+
/**
215+
* Convert Color [Integer] into 4 element array of alpha red, green, and blue
216+
*```
217+
* rgb[0] is Alpha [0 .. 255]
218+
* rgb[1] is Red [0 .. 255]
219+
* rgb[2] is Green [0...255]
220+
* rgb[3] is Blue [0...255]
221+
* ```
222+
* @return 4 element array which holds the input ARGB components.
223+
*/
224+
fun colorIntToARGBArray(color: Int): IntArray {
225+
val alpha = android.graphics.Color.alpha(color)
226+
val red = android.graphics.Color.red(color)
227+
val green = android.graphics.Color.green(color)
228+
val blue = android.graphics.Color.blue(color)
229+
return intArrayOf(alpha, red, green, blue)
230+
}
231+
232+
/**
233+
* Convert Color [Integer] into 4 element array of alpha red, green, and blue
234+
*```
235+
* rgb[0] is Alpha [0 .. 255]
236+
* rgb[1] is Red [0 .. 255]
237+
* rgb[2] is Green [0...255]
238+
* rgb[3] is Blue [0...255]
239+
* ```
240+
* @param argbIn 4 element array which holds the input ARGB components.
241+
*/
242+
fun colorIntToARGBArray(color: Int, argbIn: IntArray) {
243+
val alpha = android.graphics.Color.alpha(color)
244+
val red = android.graphics.Color.red(color)
245+
val green = android.graphics.Color.green(color)
246+
val blue = android.graphics.Color.blue(color)
247+
248+
argbIn[0] = alpha
249+
argbIn[1] = red
250+
argbIn[2] = green
251+
argbIn[3] = blue
252+
}
253+

0 commit comments

Comments
 (0)