|
| 1 | +# ColorPalette |
| 2 | + |
| 3 | +`ColorPalette` is a HSV color grid component with a built‑in alpha slider and an optional gray column. It uses a single Canvas and minimizes recomposition during drag interactions. With real-time color preview. |
| 4 | + |
| 5 | +<div style="position: relative; max-width: 700px; height: 300px; border-radius: 10px; overflow: hidden; border: 1px solid #777;"> |
| 6 | + <iframe id="demoIframe" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: none;" src="../compose/index.html?id=colorPalette" title="Demo" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin"></iframe> |
| 7 | +</div> |
| 8 | + |
| 9 | +## Import |
| 10 | + |
| 11 | +```kotlin |
| 12 | +import top.yukonga.miuix.kmp.basic.ColorPalette |
| 13 | +``` |
| 14 | + |
| 15 | +## Basic Usage |
| 16 | + |
| 17 | +```kotlin |
| 18 | +var selectedColor by remember { mutableStateOf(Color.Red) } |
| 19 | + |
| 20 | +ColorPalette( |
| 21 | + initialColor = selectedColor, |
| 22 | + onColorChanged = { newColor -> |
| 23 | + selectedColor = newColor |
| 24 | + } |
| 25 | +) |
| 26 | +``` |
| 27 | + |
| 28 | +## Component Variants |
| 29 | + |
| 30 | +### Without Color Preview |
| 31 | + |
| 32 | +Hide the preview panel at the top by setting `showPreview` to `false`: |
| 33 | + |
| 34 | +```kotlin |
| 35 | +ColorPalette( |
| 36 | + initialColor = selected, |
| 37 | + onColorChanged = { newColor -> |
| 38 | + selectedColor = newColor |
| 39 | + }, |
| 40 | + showPreview = false |
| 41 | +) |
| 42 | +``` |
| 43 | + |
| 44 | +### Customize Grid Density |
| 45 | + |
| 46 | +You can change the number of rows and hue columns, and toggle the gray column at the right: |
| 47 | + |
| 48 | +```kotlin |
| 49 | +ColorPalette( |
| 50 | + initialColor = selected, |
| 51 | + onColorChanged = { newColor -> |
| 52 | + selectedColor = newColor |
| 53 | + }, |
| 54 | + rows = 9, |
| 55 | + hueColumns = 18, |
| 56 | + includeGrayColumn = false |
| 57 | +) |
| 58 | +``` |
| 59 | + |
| 60 | +### Customize Shape and Indicator |
| 61 | + |
| 62 | +Control the palette corner radius and the selection indicator radius: |
| 63 | + |
| 64 | +```kotlin |
| 65 | +ColorPalette( |
| 66 | + initialColor = selected, |
| 67 | + onColorChanged = { newColor -> |
| 68 | + selectedColor = newColor |
| 69 | + }, |
| 70 | + cornerRadius = 20.dp, |
| 71 | + indicatorRadius = 12.dp |
| 72 | +) |
| 73 | +``` |
| 74 | + |
| 75 | +> Note: The alpha slider is built in and currently cannot be hidden. |
| 76 | +
|
| 77 | +## Properties |
| 78 | + |
| 79 | +| Property | Type | Description | Default | Required | |
| 80 | +| ----------------- | --------------- | ------------------------------------------------------------------ | --------- | -------- | |
| 81 | +| initialColor | Color | Initial color | - | Yes | |
| 82 | +| onColorChanged | (Color) -> Unit | Callback when selection changes | - | Yes | |
| 83 | +| modifier | Modifier | Modifier for the component | Modifier | No | |
| 84 | +| rows | Int | Grid rows (top: brighter/lower saturation → bottom: darker/higher) | 7 | No | |
| 85 | +| hueColumns | Int | Number of hue columns | 12 | No | |
| 86 | +| includeGrayColumn | Boolean | Whether to show the gray column at the right | true | No | |
| 87 | +| showPreview | Boolean | Whether to show the color preview panel | true | No | |
| 88 | +| cornerRadius | Dp | Palette corner radius | 16.dp | No | |
| 89 | +| indicatorRadius | Dp | Selection indicator radius | 10.dp | No | |
| 90 | + |
| 91 | +## Advanced Usage |
| 92 | + |
| 93 | +### Using in Forms |
| 94 | + |
| 95 | +Combine with other inputs to build a simple color form. The example below maintains a HEX string. You may extend it to include alpha if needed. |
| 96 | + |
| 97 | +```kotlin |
| 98 | +var currentColor by remember { mutableStateOf(Color(0xFF2196F3)) } |
| 99 | +var hexValue by remember(currentColor) { |
| 100 | + mutableStateOf( |
| 101 | + "#${(currentColor.red * 255).toInt().toString(16).padStart(2, '0').uppercase()}" + |
| 102 | + (currentColor.green * 255).toInt().toString(16).padStart(2, '0').uppercase() + |
| 103 | + (currentColor.blue * 255).toInt().toString(16).padStart(2, '0').uppercase() |
| 104 | + ) |
| 105 | +} |
| 106 | + |
| 107 | +Surface { |
| 108 | + Column( |
| 109 | + modifier = Modifier |
| 110 | + .fillMaxWidth() |
| 111 | + .padding(16.dp) |
| 112 | + ) { |
| 113 | + Text( |
| 114 | + text = "Select Color", |
| 115 | + style = MiuixTheme.textStyles.title2 |
| 116 | + ) |
| 117 | + Spacer(modifier = Modifier.height(16.dp)) |
| 118 | + ColorPalette( |
| 119 | + initialColor = currentColor, |
| 120 | + onColorChanged = { |
| 121 | + currentColor = it |
| 122 | + hexValue = "#${(it.red * 255).toInt().toString(16).padStart(2, '0').uppercase()}" + |
| 123 | + (it.green * 255).toInt().toString(16).padStart(2, '0').uppercase() + |
| 124 | + (it.blue * 255).toInt().toString(16).padStart(2, '0').uppercase() |
| 125 | + } |
| 126 | + ) |
| 127 | + Spacer(modifier = Modifier.height(16.dp)) |
| 128 | + TextField( |
| 129 | + value = hexValue, |
| 130 | + onValueChange = { /* Add hex parsing logic if needed */ }, |
| 131 | + label = "Hex Value", |
| 132 | + modifier = Modifier.fillMaxWidth() |
| 133 | + ) |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +### Using with Dialog |
| 139 | + |
| 140 | +Use `ColorPalette` in a dialog to select a color: |
| 141 | + |
| 142 | +```kotlin |
| 143 | +var showColorDialog = remember { mutableStateOf(false) } |
| 144 | +var selectedColor by remember { mutableStateOf(Color(0xFF2196F3)) } |
| 145 | + |
| 146 | +Scaffold { |
| 147 | + TextButton( |
| 148 | + text = "Select Color", |
| 149 | + onClick = { showColorDialog.value = true } |
| 150 | + ) |
| 151 | + SuperDialog( |
| 152 | + title = "Select Color", |
| 153 | + show = showColorDialog, |
| 154 | + onDismissRequest = { showColorDialog.value = false } // Close dialog |
| 155 | + ) { |
| 156 | + Column { |
| 157 | + ColorPalette( |
| 158 | + initialColor = selectedColor, |
| 159 | + onColorChanged = { selectedColor = it } |
| 160 | + ) |
| 161 | + Spacer(modifier = Modifier.height(16.dp)) |
| 162 | + Row( |
| 163 | + modifier = Modifier.fillMaxWidth(), |
| 164 | + horizontalArrangement = Arrangement.spacedBy(8.dp), |
| 165 | + ) { |
| 166 | + TextButton( |
| 167 | + modifier = Modifier.weight(1f), |
| 168 | + text = "Cancel", |
| 169 | + onClick = { showColorDialog.value = false } // Close dialog |
| 170 | + ) |
| 171 | + TextButton( |
| 172 | + modifier = Modifier.weight(1f), |
| 173 | + text = "Confirm", |
| 174 | + colors = ButtonDefaults.textButtonColorsPrimary(), // Use theme color |
| 175 | + onClick = { |
| 176 | + showColorDialog.value = false |
| 177 | + // Handle confirmation logic |
| 178 | + // For example: save the selected color |
| 179 | + }) |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | +``` |
0 commit comments