|
| 1 | +package com.smarttoolfactory.colorpicker.picker |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.* |
| 4 | +import androidx.compose.material.TextFieldDefaults |
| 5 | +import androidx.compose.runtime.* |
| 6 | +import androidx.compose.ui.Alignment |
| 7 | +import androidx.compose.ui.Modifier |
| 8 | +import androidx.compose.ui.graphics.Color |
| 9 | +import androidx.compose.ui.unit.Dp |
| 10 | +import androidx.compose.ui.unit.dp |
| 11 | +import com.smarttoolfactory.colorpicker.model.ColorHSL |
| 12 | +import com.smarttoolfactory.colorpicker.model.ColorModel |
| 13 | +import com.smarttoolfactory.colorpicker.selector.SelectorDiamondSaturationLightnessHSL |
| 14 | +import com.smarttoolfactory.colorpicker.selector.SelectorRingHue |
| 15 | +import com.smarttoolfactory.colorpicker.slider.CompositeSliderPanel |
| 16 | +import com.smarttoolfactory.colorpicker.ui.Grey400 |
| 17 | +import com.smarttoolfactory.colorpicker.ui.Grey600 |
| 18 | +import com.smarttoolfactory.colorpicker.util.colorToHSL |
| 19 | +import com.smarttoolfactory.colorpicker.util.colorToHex |
| 20 | +import com.smarttoolfactory.colorpicker.util.colorToHexAlpha |
| 21 | +import com.smarttoolfactory.colorpicker.widget.ColorDisplayRoundedRect |
| 22 | +import com.smarttoolfactory.colorpicker.widget.ColorModelChangeTabRow |
| 23 | +import com.smarttoolfactory.colorpicker.widget.HexTextField |
| 24 | + |
| 25 | +/** |
| 26 | + * ColorPicker with [SelectorRingHue] hue selector and [SelectorDiamondSaturationLightnessHSL] |
| 27 | + * saturation lightness Selector uses [HSL](https://en.wikipedia.org/wiki/HSL_and_HSV) |
| 28 | + * color model as base. |
| 29 | + * |
| 30 | + * This color picker has tabs section that can be changed between |
| 31 | + * HSL, HSV and RGB color models and color can be set using [CompositeSliderPanel] which contains |
| 32 | + * sliders for each color models. |
| 33 | + * |
| 34 | + * @param initialColor color that is passed to this picker initially. |
| 35 | + * @param ringOuterRadiusFraction outer radius of [SelectorRingHue]. |
| 36 | + * @param ringInnerRadiusFraction inner radius of [SelectorRingHue]. |
| 37 | + * @param ringBackgroundColor background from center to inner radius of [SelectorRingHue]. |
| 38 | + * @param ringBorderStrokeColor stroke color for drawing borders around inner or outer radius. |
| 39 | + * @param ringBorderStrokeWidth stroke width of borders. |
| 40 | + * @param selectionRadius radius of white and black circle selector. |
| 41 | + * @param onColorChange callback that is triggered when [Color] is changed using [SelectorRingHue], |
| 42 | + * [SelectorDiamondSaturationLightnessHSL] or [CompositeSliderPanel] |
| 43 | + */ |
| 44 | +@Composable |
| 45 | +fun ColorPickerRingDiamondHEX( |
| 46 | + modifier: Modifier = Modifier, |
| 47 | + initialColor: Color, |
| 48 | + ringOuterRadiusFraction: Float = .9f, |
| 49 | + ringInnerRadiusFraction: Float = .6f, |
| 50 | + ringBackgroundColor: Color = Color.Black, |
| 51 | + ringBorderStrokeColor: Color = Color.Black, |
| 52 | + ringBorderStrokeWidth: Dp = 4.dp, |
| 53 | + selectionRadius: Dp = 8.dp, |
| 54 | + onColorChange: (Color, String) -> Unit |
| 55 | +) { |
| 56 | + |
| 57 | + var inputColorModel by remember { mutableStateOf(ColorModel.HSL) } |
| 58 | + |
| 59 | + val hslArray = colorToHSL(initialColor) |
| 60 | + |
| 61 | + var hue by remember { mutableStateOf(hslArray[0]) } |
| 62 | + var saturation by remember { mutableStateOf(hslArray[1]) } |
| 63 | + var lightness by remember { mutableStateOf(hslArray[2]) } |
| 64 | + var alpha by remember { mutableStateOf(initialColor.alpha) } |
| 65 | + |
| 66 | + val currentColor = |
| 67 | + Color.hsl(hue = hue, saturation = saturation, lightness = lightness, alpha = alpha) |
| 68 | + |
| 69 | + var hexString by remember { |
| 70 | + mutableStateOf( |
| 71 | + colorToHexAlpha( |
| 72 | + currentColor |
| 73 | + ) |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + onColorChange(currentColor, colorToHex(currentColor)) |
| 78 | + |
| 79 | + Column( |
| 80 | + modifier = modifier, |
| 81 | + horizontalAlignment = Alignment.CenterHorizontally |
| 82 | + ) { |
| 83 | + |
| 84 | + Spacer(modifier = Modifier.height(10.dp)) |
| 85 | + |
| 86 | + // Initial and Current Colors |
| 87 | + ColorDisplayRoundedRect( |
| 88 | + modifier = Modifier |
| 89 | + .fillMaxWidth() |
| 90 | + .padding(horizontal = 50.dp, vertical = 10.dp), |
| 91 | + initialColor = initialColor, |
| 92 | + currentColor = currentColor |
| 93 | + ) |
| 94 | + |
| 95 | + Box(contentAlignment = Alignment.Center) { |
| 96 | + |
| 97 | + // Ring Shaped Hue Selector |
| 98 | + SelectorRingHue( |
| 99 | + modifier = Modifier.fillMaxWidth(1f), |
| 100 | + hue = hue, |
| 101 | + outerRadiusFraction = ringOuterRadiusFraction, |
| 102 | + innerRadiusFraction = ringInnerRadiusFraction, |
| 103 | + backgroundColor = ringBackgroundColor, |
| 104 | + borderStrokeColor = ringBorderStrokeColor, |
| 105 | + borderStrokeWidth = ringBorderStrokeWidth, |
| 106 | + selectionRadius = selectionRadius |
| 107 | + ) { hueChange -> |
| 108 | + hue = hueChange |
| 109 | + hexString = colorToHexAlpha( |
| 110 | + Color.hsl( |
| 111 | + hue = hue, |
| 112 | + saturation = saturation, |
| 113 | + lightness = lightness, |
| 114 | + alpha = alpha |
| 115 | + ) |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + // Diamond Shaped Saturation and Lightness Selector |
| 120 | + SelectorDiamondSaturationLightnessHSL( |
| 121 | + modifier = Modifier.fillMaxWidth(ringInnerRadiusFraction * .9f), |
| 122 | + hue = hue, |
| 123 | + saturation = saturation, |
| 124 | + lightness = lightness, |
| 125 | + selectionRadius = selectionRadius |
| 126 | + ) { s, l -> |
| 127 | + saturation = s |
| 128 | + lightness = l |
| 129 | + hexString = colorToHexAlpha( |
| 130 | + Color.hsl( |
| 131 | + hue = hue, |
| 132 | + saturation = saturation, |
| 133 | + lightness = lightness, |
| 134 | + alpha = alpha |
| 135 | + ) |
| 136 | + ) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // HSL-HSV-RGB Color Model Change Tabs |
| 141 | + ColorModelChangeTabRow( |
| 142 | + modifier = Modifier.width(350.dp), |
| 143 | + colorModel = inputColorModel, |
| 144 | + onColorModelChange = { |
| 145 | + inputColorModel = it |
| 146 | + } |
| 147 | + ) |
| 148 | + // HSL-HSV-RGB Sliders |
| 149 | + CompositeSliderPanel( |
| 150 | + modifier = Modifier.padding(start = 10.dp, end = 7.dp), |
| 151 | + compositeColor = ColorHSL( |
| 152 | + hue = hue, |
| 153 | + saturation = saturation, |
| 154 | + lightness = lightness, |
| 155 | + alpha = alpha |
| 156 | + ), |
| 157 | + onColorChange = { |
| 158 | + (it as? ColorHSL)?.let { color -> |
| 159 | + hue = color.hue |
| 160 | + saturation = color.saturation |
| 161 | + lightness = color.lightness |
| 162 | + alpha = color.alpha |
| 163 | + |
| 164 | + hexString = colorToHexAlpha( |
| 165 | + Color.hsl( |
| 166 | + hue = hue, |
| 167 | + saturation = saturation, |
| 168 | + lightness = lightness, |
| 169 | + alpha = alpha |
| 170 | + ) |
| 171 | + ) |
| 172 | + } |
| 173 | + }, |
| 174 | + showAlphaSlider = true, |
| 175 | + inputColorModel = inputColorModel, |
| 176 | + outputColorModel = ColorModel.HSL |
| 177 | + ) |
| 178 | + |
| 179 | + HexTextField( |
| 180 | + modifier = Modifier |
| 181 | + .padding(horizontal = 10.dp) |
| 182 | + .fillMaxWidth(), |
| 183 | + hexString = hexString, |
| 184 | + useAlpha = true, |
| 185 | + colors = TextFieldDefaults.textFieldColors( |
| 186 | + textColor = Grey400, |
| 187 | + placeholderColor = Grey600, |
| 188 | + backgroundColor = Color.Transparent, |
| 189 | + focusedIndicatorColor = Color.Transparent, |
| 190 | + unfocusedIndicatorColor = Color.Transparent |
| 191 | + ), |
| 192 | + onTextChange = { |
| 193 | + hexString = it |
| 194 | + }, |
| 195 | + onColorChange = { |
| 196 | + val hslArrayNew = colorToHSL(it) |
| 197 | + hue = hslArrayNew[0] |
| 198 | + saturation = hslArrayNew[1] |
| 199 | + lightness = hslArrayNew[2] |
| 200 | + alpha = it.alpha |
| 201 | + } |
| 202 | + ) |
| 203 | + } |
| 204 | +} |
0 commit comments