Skip to content

Commit 14cce34

Browse files
update util functions
1 parent c10d63f commit 14cce34

File tree

9 files changed

+175
-37
lines changed

9 files changed

+175
-37
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.smarttoolfactory.colorpicker.model
22

33
import androidx.annotation.FloatRange
44
import androidx.compose.ui.graphics.Color
5-
import com.smarttoolfactory.colorpicker.argbToHex
6-
import com.smarttoolfactory.colorpicker.rgbToHex
5+
import com.smarttoolfactory.colorpicker.util.argbToHex
6+
import com.smarttoolfactory.colorpicker.util.rgbToHex
77

88
/**
99
* Color in HSV color model

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/slider/Slider.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import androidx.compose.ui.Modifier
1212
import androidx.compose.ui.graphics.Brush
1313
import androidx.compose.ui.unit.dp
1414
import com.smarttoolfactory.colorpicker.ui.brush.*
15+
import com.smarttoolfactory.colorpicker.util.roundToTwoDigits
1516
import com.smarttoolfactory.colorpicker.widget.drawChecker
1617
import com.smarttoolfactory.slider.ColorBrush
1718
import com.smarttoolfactory.slider.ColorfulSlider
@@ -372,7 +373,9 @@ fun CheckeredColorfulSlider(
372373
modifier = Modifier,
373374
thumbRadius = 12.dp,
374375
trackHeight = 12.dp,
375-
onValueChange = onValueChange,
376+
onValueChange = { value ->
377+
onValueChange(value.roundToTwoDigits())
378+
},
376379
valueRange = valueRange,
377380
coerceThumbInTrack = true,
378381
colors = MaterialSliderDefaults.materialColors(

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/slider/SliderDisplay.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ import androidx.compose.ui.graphics.Color
1313
import androidx.compose.ui.text.font.FontWeight
1414
import androidx.compose.ui.unit.dp
1515
import androidx.compose.ui.unit.sp
16-
import com.smarttoolfactory.colorpicker.fractionToPercent
17-
import com.smarttoolfactory.colorpicker.fractionToRGBString
18-
import com.smarttoolfactory.colorpicker.ui.Gray
16+
import com.smarttoolfactory.colorpicker.util.fractionToPercent
17+
import com.smarttoolfactory.colorpicker.util.fractionToRGBString
1918
import com.smarttoolfactory.colorpicker.ui.Grey400
2019

2120
/*

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/slider/SliderWithCircleDisplay.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import com.smarttoolfactory.slider.ColorfulSlider
1818
* for HSV color model. This composable requires minimum
1919
* 100.dp height, giving a height lower than this might break its layout.
2020
*
21+
* @param modifier [Modifier] for whole Composable
22+
* @param circleModifier [Modifier] for Circle shaped box
2123
* @param hue in [0..360f]
2224
* @param saturation in [0..1f]
2325
* @param value in [0..1f]
@@ -28,6 +30,7 @@ import com.smarttoolfactory.slider.ColorfulSlider
2830
@Composable
2931
fun SliderCircleColorDisplayHueHSV(
3032
modifier: Modifier = Modifier,
33+
circleModifier: Modifier = Modifier,
3134
@FloatRange(from = 0.0, to = 360.0) hue: Float,
3235
@FloatRange(from = 0.0, to = 1.0) saturation: Float,
3336
@FloatRange(from = 0.0, to = 1.0) value: Float,
@@ -37,6 +40,7 @@ fun SliderCircleColorDisplayHueHSV(
3740
) {
3841
SliderWithCircleDisplay(
3942
modifier = modifier,
43+
circleModifier = circleModifier,
4044
color = Color.hsv(hue, saturation, value, alpha)
4145
) {
4246

@@ -56,6 +60,8 @@ fun SliderCircleColorDisplayHueHSV(
5660
* for HSV color model. This composable requires minimum
5761
* 100.dp height, giving a height lower than this might break its layout.
5862
*
63+
* @param modifier [Modifier] for whole Composable
64+
* @param circleModifier [Modifier] for Circle shaped box
5965
* @param hue in [0..360f]
6066
* @param saturation in [0..1f]
6167
* @param value in [0..1f]
@@ -66,6 +72,7 @@ fun SliderCircleColorDisplayHueHSV(
6672
@Composable
6773
fun SliderCircleColorDisplaySaturationHSV(
6874
modifier: Modifier = Modifier,
75+
circleModifier: Modifier = Modifier,
6976
@FloatRange(from = 0.0, to = 360.0) hue: Float,
7077
@FloatRange(from = 0.0, to = 1.0) saturation: Float,
7178
@FloatRange(from = 0.0, to = 1.0) value: Float,
@@ -75,6 +82,7 @@ fun SliderCircleColorDisplaySaturationHSV(
7582
) {
7683
SliderWithCircleDisplay(
7784
modifier = modifier,
85+
circleModifier = circleModifier,
7886
color = Color.hsv(hue, saturation, value, alpha)
7987
) {
8088
SliderSaturationHSV(
@@ -96,6 +104,8 @@ fun SliderCircleColorDisplaySaturationHSV(
96104
* for HSV color model. This composable requires minimum
97105
* 100.dp height, giving a height lower than this might break its layout.
98106
*
107+
* @param modifier [Modifier] for whole Composable
108+
* @param circleModifier [Modifier] for Circle shaped box
99109
* @param hue in [0..360f]
100110
* @param saturation in [0..1f]
101111
* @param value in [0..1f]
@@ -106,6 +116,7 @@ fun SliderCircleColorDisplaySaturationHSV(
106116
@Composable
107117
fun SliderCircleColorDisplayValueHSV(
108118
modifier: Modifier = Modifier,
119+
circleModifier: Modifier = Modifier,
109120
@FloatRange(from = 0.0, to = 360.0) hue: Float,
110121
@FloatRange(from = 0.0, to = 1.0) saturation: Float,
111122
@FloatRange(from = 0.0, to = 1.0) value: Float,
@@ -116,6 +127,7 @@ fun SliderCircleColorDisplayValueHSV(
116127

117128
SliderWithCircleDisplay(
118129
modifier = modifier,
130+
circleModifier = circleModifier,
119131
color = Color.hsv(hue, saturation, value, alpha)
120132
) {
121133
SliderValueHSV(
@@ -137,6 +149,8 @@ fun SliderCircleColorDisplayValueHSV(
137149
* for HSL color model. This composable requires minimum
138150
* 100.dp height, giving a height lower than this might break its layout.
139151
*
152+
* @param modifier [Modifier] for whole Composable
153+
* @param circleModifier [Modifier] for Circle shaped box
140154
* @param hue in [0..360f]
141155
* @param saturation in [0..1f]
142156
* @param lightness in [0..1f]
@@ -147,6 +161,7 @@ fun SliderCircleColorDisplayValueHSV(
147161
@Composable
148162
fun SliderCircleColorDisplayHueHSL(
149163
modifier: Modifier = Modifier,
164+
circleModifier: Modifier = Modifier,
150165
@FloatRange(from = 0.0, to = 360.0) hue: Float,
151166
@FloatRange(from = 0.0, to = 1.0) saturation: Float,
152167
@FloatRange(from = 0.0, to = 1.0) lightness: Float,
@@ -156,6 +171,7 @@ fun SliderCircleColorDisplayHueHSL(
156171
) {
157172
SliderWithCircleDisplay(
158173
modifier = modifier,
174+
circleModifier = circleModifier,
159175
color = Color.hsl(hue, saturation, lightness, alpha)
160176
) {
161177

@@ -175,6 +191,8 @@ fun SliderCircleColorDisplayHueHSL(
175191
* for HSL color model. This composable requires minimum
176192
* 100.dp height, giving a height lower than this might break its layout.
177193
*
194+
* @param modifier [Modifier] for whole Composable
195+
* @param circleModifier [Modifier] for Circle shaped box
178196
* @param hue in [0..360f]
179197
* @param saturation in [0..1f]
180198
* @param lightness in [0..1f]
@@ -185,6 +203,7 @@ fun SliderCircleColorDisplayHueHSL(
185203
@Composable
186204
fun SliderCircleColorDisplaySaturationHSL(
187205
modifier: Modifier = Modifier,
206+
circleModifier: Modifier = Modifier,
188207
@FloatRange(from = 0.0, to = 360.0) hue: Float,
189208
@FloatRange(from = 0.0, to = 1.0) saturation: Float,
190209
@FloatRange(from = 0.0, to = 1.0) lightness: Float,
@@ -194,6 +213,7 @@ fun SliderCircleColorDisplaySaturationHSL(
194213
) {
195214
SliderWithCircleDisplay(
196215
modifier = modifier,
216+
circleModifier = circleModifier,
197217
color = Color.hsl(hue, saturation, lightness, alpha)
198218
) {
199219
SliderSaturationHSL(
@@ -211,6 +231,8 @@ fun SliderCircleColorDisplaySaturationHSL(
211231
* for HSL color model. This composable requires minimum
212232
* 100.dp height, giving a height lower than this might break its layout.
213233
*
234+
* @param modifier [Modifier] for whole Composable
235+
* @param circleModifier [Modifier] for Circle shaped box
214236
* @param hue in [0..360f]
215237
* @param saturation in [0..1f]
216238
* @param lightness in [0..1f]
@@ -221,6 +243,7 @@ fun SliderCircleColorDisplaySaturationHSL(
221243
@Composable
222244
fun SliderCircleColorDisplayLightnessHSL(
223245
modifier: Modifier = Modifier,
246+
circleModifier: Modifier = Modifier,
224247
@FloatRange(from = 0.0, to = 360.0) hue: Float,
225248
@FloatRange(from = 0.0, to = 1.0) saturation: Float,
226249
@FloatRange(from = 0.0, to = 1.0) lightness: Float,
@@ -230,6 +253,7 @@ fun SliderCircleColorDisplayLightnessHSL(
230253
) {
231254
SliderWithCircleDisplay(
232255
modifier = modifier,
256+
circleModifier = circleModifier,
233257
color = Color.hsl(hue, saturation, lightness, alpha)
234258
) {
235259
SliderLightnessHSL(
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.smarttoolfactory.colorpicker.util;
2+
3+
/**
4+
*
5+
* Code from
6+
* https://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion
7+
*/
8+
public class ColorConversionUtils {
9+
10+
/**
11+
* Converts an HSL color value to RGB. Conversion formula
12+
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
13+
* Assumes h, s, and l are contained in the set [0, 1] and
14+
* returns r, g, and b in the set [0, 255].
15+
*
16+
* @param h The hue
17+
* @param s The saturation
18+
* @param l The lightness
19+
* @return int array, the RGB representation
20+
*/
21+
public static int[] hslToRgb(float h, float s, float l) {
22+
float r, g, b;
23+
24+
if (s == 0f) {
25+
r = g = b = l; // achromatic
26+
} else {
27+
float q = l < 0.5f ? l * (1 + s) : l + s - l * s;
28+
float p = 2 * l - q;
29+
r = hueToRgb(p, q, h + 1f / 3f);
30+
g = hueToRgb(p, q, h);
31+
b = hueToRgb(p, q, h - 1f / 3f);
32+
}
33+
int[] rgb = {to255(r), to255(g), to255(b)};
34+
return rgb;
35+
}
36+
37+
public static int to255(float v) {
38+
return (int) Math.min(255, 256 * v);
39+
}
40+
41+
/**
42+
* Helper method that converts hue to rgb
43+
*/
44+
public static float hueToRgb(float p, float q, float t) {
45+
if (t < 0f)
46+
t += 1f;
47+
if (t > 1f)
48+
t -= 1f;
49+
if (t < 1f / 6f)
50+
return p + (q - p) * 6f * t;
51+
if (t < 1f / 2f)
52+
return q;
53+
if (t < 2f / 3f)
54+
return p + (q - p) * (2f / 3f - t) * 6f;
55+
return p;
56+
}
57+
58+
/**
59+
* Converts an RGB color value to HSL. Conversion formula
60+
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
61+
* Assumes pR, pG, and bpBare contained in the set [0, 255] and
62+
* returns h, s, and l in the set [0, 1].
63+
*
64+
* @param pR The red color value
65+
* @param pG The green color value
66+
* @param pB The blue color value
67+
* @return float array, the HSL representation
68+
*/
69+
public static float[] rgbToHsl(int pR, int pG, int pB) {
70+
float r = pR / 255f;
71+
float g = pG / 255f;
72+
float b = pB / 255f;
73+
74+
float max = (r > g && r > b) ? r : (g > b) ? g : b;
75+
float min = (r < g && r < b) ? r : (g < b) ? g : b;
76+
77+
float h, s, l;
78+
l = (max + min) / 2.0f;
79+
80+
if (max == min) {
81+
h = s = 0.0f;
82+
} else {
83+
float d = max - min;
84+
s = (l > 0.5f) ? d / (2.0f - max - min) : d / (max + min);
85+
86+
if (r > g && r > b)
87+
h = (g - b) / d + (g < b ? 6.0f : 0.0f);
88+
89+
else if (g > b)
90+
h = (b - r) / d + 2.0f;
91+
92+
else
93+
h = (r - g) / d + 4.0f;
94+
95+
h /= 6.0f;
96+
}
97+
98+
float[] hsl = {h, s, l};
99+
return hsl;
100+
}
101+
}

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/ColorUtil.kt renamed to colorpicker/src/main/java/com/smarttoolfactory/colorpicker/util/ColorUtil.kt

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.smarttoolfactory.colorpicker
1+
package com.smarttoolfactory.colorpicker.util
22

33
import androidx.compose.ui.graphics.Color
44
import androidx.compose.ui.graphics.toArgb
@@ -1105,31 +1105,3 @@ fun hexToARGB(colorString: String, argbIn: IntArray) {
11051105
colorIntToARGBArray(colorInt, argbIn)
11061106
}
11071107

1108-
/**
1109-
* Converts alpha, red, green or blue values from range of [0f-1f] to [0-255].
1110-
*/
1111-
fun Float.fractionToRGBRange() = (this * 255.0f).toInt()
1112-
1113-
/**
1114-
* Converts alpha, red, green or blue values from range of [0f-1f] to [0-255] and returns
1115-
* it as [String].
1116-
*/
1117-
fun Float.fractionToRGBString() = this.fractionToRGBRange().toString()
1118-
1119-
/**
1120-
* Rounds this [Float] to another with 2 significant numbers
1121-
* 0.1234 is rounded to 0.12
1122-
* 0.129 is rounded to 0.12
1123-
*/
1124-
fun Float.roundToIntTwoDigits() = (this * 100.0f).toInt() / 100.0f
1125-
1126-
/**
1127-
* Rounds this [Float] to closest int.
1128-
*/
1129-
fun Float.round() = this.roundToInt()
1130-
1131-
/**
1132-
* Converts **HSV** or **HSL** colors that are in range of [0f-1f] to [0-100] range in [Integer]
1133-
*/
1134-
fun Float.fractionToPercent() = (this * 100.0f).toInt()
1135-

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/DimensionUtil.kt renamed to colorpicker/src/main/java/com/smarttoolfactory/colorpicker/util/DimensionUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.smarttoolfactory.colorpicker
1+
package com.smarttoolfactory.colorpicker.util
22

33
import androidx.compose.ui.geometry.Offset
44
import kotlin.math.*

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/DrawUtil.kt renamed to colorpicker/src/main/java/com/smarttoolfactory/colorpicker/util/DrawUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.smarttoolfactory.colorpicker
1+
package com.smarttoolfactory.colorpicker.util
22

33
import androidx.compose.ui.geometry.Offset
44
import androidx.compose.ui.geometry.Size
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.smarttoolfactory.colorpicker.util
2+
3+
import kotlin.math.roundToInt
4+
5+
6+
/**
7+
* Converts alpha, red, green or blue values from range of [0f-1f] to [0-255].
8+
*/
9+
fun Float.fractionToRGBRange() = (this * 255.0f).toInt()
10+
11+
/**
12+
* Converts alpha, red, green or blue values from range of [0f-1f] to [0-255] and returns
13+
* it as [String].
14+
*/
15+
fun Float.fractionToRGBString() = this.fractionToRGBRange().toString()
16+
17+
/**
18+
* Rounds this [Float] to another with 2 significant numbers
19+
* 0.1234 is rounded to 0.12
20+
* 0.129 is rounded to 0.12
21+
*/
22+
fun Float.roundToTwoDigits() = (this * 100.0f).roundToInt() / 100.0f
23+
24+
/**
25+
* Rounds this [Float] to closest int.
26+
*/
27+
fun Float.round() = this.roundToInt()
28+
29+
/**
30+
* Converts **HSV** or **HSL** colors that are in range of [0f-1f] to [0-100] range in [Integer]
31+
* with [Float.roundToInt]
32+
*/
33+
fun Float.fractionToPercent() = (this * 100.0f).roundToInt()
34+
35+
/**
36+
* Converts **HSV** or **HSL** colors that are in range of [0f-1f] to [0-100] range in [Integer]
37+
* with [Float.toInt]
38+
*/
39+
fun Float.fractionToIntPercent() = (this * 100.0f).toInt()

0 commit comments

Comments
 (0)