Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement selectByHsv #60

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.github.skydoves.colorpicker.compose.AlphaSlider
Expand All @@ -46,6 +47,7 @@ import com.github.skydoves.colorpicker.compose.rememberColorPickerController
import com.github.skydoves.colorpickercomposedemo.PhotoPickerIcon
import com.github.skydoves.colorpickercomposedemo.R

@Preview
@Composable
fun ImageColorPickerScreen() {
val controller = rememberColorPickerController()
Expand Down
1 change: 1 addition & 0 deletions colorpicker-compose/api/colorpicker-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public final class com/github/skydoves/colorpicker/compose/ColorPickerController
public final fun getSelectedPoint ()Landroidx/compose/runtime/State;
public final fun selectByColor-DxMtmZc (JZ)V
public final fun selectByCoordinate (FFZ)V
public final fun selectByHsv ([FFZ)V
public final fun selectCenter (Z)V
public final fun setAlpha (FZ)V
public final fun setBrightness (FZ)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,39 @@ public class ColorPickerController {
(color.blue * 255).toInt(),
hsv,
)
selectByHsv(hsv = hsv, alpha = color.alpha, fromUser = fromUser)
}
}
}

/**
* Select a specific color and update with the selected color.
*
* @param hsv A float array that represents hsv color code.
* @param alpha An alpha value that will be composed with the [hsv] color code.
* @param fromUser Represents this event is triggered by user or not.
*/
public fun selectByHsv(hsv: FloatArray, alpha: Float, fromUser: Boolean) {
val palette = paletteBitmap
if (palette != null) {
val pickerRadius: Float = palette.width.coerceAtMost(palette.height) * 0.5f
if (pickerRadius > 0) {
val angle = (Math.PI / 180f) * hsv[0] * (-1)
val saturationVector = pickerRadius * hsv[1]
val x = saturationVector * cos(angle) + (palette.width / 2)
val y = saturationVector * sin(angle) + (palette.height / 2)
selectByCoordinate(x.toFloat(), y.toFloat(), fromUser)
setAlpha(alpha, fromUser = false)

setAlpha(color.alpha, fromUser = false)
val color = android.graphics.Color.HSVToColor((alpha * 255).toInt(), hsv)

val brightness = max(max(color.red, color.green), color.blue)
val brightness = max(
max(
android.graphics.Color.red(color) / 255.0f,
android.graphics.Color.green(color) / 255.0f,
),
android.graphics.Color.blue(color) / 255.0f,
)
setBrightness(brightness, fromUser = false)
}
}
Expand Down
Loading