Skip to content
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 @@ -105,6 +105,7 @@ public class ColorPickerView extends FrameLayout implements LifecycleObserver {

private String preferenceName;
private boolean selectorPointValidation = true;
private boolean resetBrightnessOnLowSaturation = true;
private final ColorPickerPreferenceManager preferenceManager =
ColorPickerPreferenceManager.getInstance(getContext());

Expand Down Expand Up @@ -178,6 +179,10 @@ private void getAttrs(AttributeSet attrs) {
this.selectorPointValidation =
a.getBoolean(R.styleable.ColorPickerView_selectorPointValidation, selectorPointValidation);
}
if (a.hasValue(R.styleable.ColorPickerView_resetBrightnessOnLowSaturation)) {
this.resetBrightnessOnLowSaturation =
a.getBoolean(R.styleable.ColorPickerView_resetBrightnessOnLowSaturation, resetBrightnessOnLowSaturation);
}
} finally {
a.recycle();
}
Expand Down Expand Up @@ -284,6 +289,7 @@ protected void onCreateByBuilder(Builder builder) {
if (builder.initialColor != 0) setInitialColor(builder.initialColor);
if (builder.lifecycleOwner != null) setLifecycleOwner(builder.lifecycleOwner);
this.selectorPointValidation = builder.selectorPointValidation;
this.resetBrightnessOnLowSaturation = builder.resetBrightnessOnLowSaturation;
}

@SuppressLint("ClickableViewAccessibility")
Expand Down Expand Up @@ -322,6 +328,16 @@ private boolean onTouchReceived(final MotionEvent event) {
this.selectedPoint = PointMapper.getColorPoint(this, new Point(snapPoint.x, snapPoint.y));
setCoordinate(snapPoint.x, snapPoint.y);

// When selecting a color with very low saturation on an HSV palette,
// automatically set brightness slider to maximum to allow selecting white.
if (resetBrightnessOnLowSaturation && isHuePalette() && brightnessSlider != null) {
float[] hsv = new float[3];
Color.colorToHSV(pixelColor, hsv);
if (hsv[1] < 0.05f) {
brightnessSlider.setSelectorByHalfSelectorPosition(1.0f);
}
}

if (actionMode == ActionMode.LAST) {
notifyToFlagView(this.selectedPoint);
if (event.getAction() == MotionEvent.ACTION_UP) {
Expand Down Expand Up @@ -813,9 +829,39 @@ public void setSelectorDrawable(Drawable drawable) {

/**
* selects the center of the palette manually.
* For HSV palettes, this also resets the brightness slider to maximum
* to ensure the selected color is white.
*/
public void selectCenter() {
setSelectorPoint(getWidth() / 2, getMeasuredHeight() / 2);
if (isHuePalette() && brightnessSlider != null) {
brightnessSlider.setSelectorByHalfSelectorPosition(1.0f);
fireColorListener(getColor(), false);
}
}

/**
* Selects white color on the HSV palette.
*
* <p>This moves the selector to the center of the palette (saturation = 0),
* sets the brightness slider to maximum (value = 1), and optionally sets
* the alpha slider to maximum (alpha = 255).
*
* <p>This method only works with HSV palettes. For custom palette drawables,
* use {@link #setSelectorPoint(int, int)} instead.
*/
public void selectWhite() {
if (!isHuePalette()) {
return;
}
setSelectorPoint(getWidth() / 2, getMeasuredHeight() / 2);
if (brightnessSlider != null) {
brightnessSlider.setSelectorByHalfSelectorPosition(1.0f);
}
if (alphaSlideBar != null) {
alphaSlideBar.setSelectorByHalfSelectorPosition(1.0f);
}
fireColorListener(getColor(), false);
}

/**
Expand Down Expand Up @@ -962,6 +1008,33 @@ public void setSelectorPointValidation(boolean enabled) {
this.selectorPointValidation = enabled;
}

/**
* Returns whether brightness slider auto-reset on low saturation is enabled.
*
* <p>When enabled, the brightness slider automatically resets to maximum when selecting
* colors with very low saturation (near the center of the HSV palette), allowing easy
* selection of white.
*
* @return true if auto-reset is enabled, false otherwise.
*/
public boolean isResetBrightnessOnLowSaturationEnabled() {
return resetBrightnessOnLowSaturation;
}

/**
* Sets whether brightness slider auto-reset on low saturation is enabled.
*
* <p>When enabled (default), the brightness slider automatically resets to maximum when
* selecting colors with very low saturation (near the center of the HSV palette), allowing
* easy selection of white (#FFFFFF).
* When disabled, the brightness slider position is preserved regardless of the selected color.
*
* @param enabled true to enable auto-reset, false to disable.
*/
public void setResetBrightnessOnLowSaturation(boolean enabled) {
this.resetBrightnessOnLowSaturation = enabled;
}

/**
* sets the {@link LifecycleOwner}.
*
Expand Down Expand Up @@ -1024,6 +1097,7 @@ public static class Builder {
private String preferenceName;
private LifecycleOwner lifecycleOwner;
private boolean selectorPointValidation = true;
private boolean resetBrightnessOnLowSaturation = true;

public Builder(Context context) {
this.context = context;
Expand Down Expand Up @@ -1124,6 +1198,11 @@ public Builder setSelectorPointValidation(boolean enabled) {
return this;
}

public Builder setResetBrightnessOnLowSaturation(boolean enabled) {
this.resetBrightnessOnLowSaturation = enabled;
return this;
}

public ColorPickerView build() {
ColorPickerView colorPickerView = new ColorPickerView(context);
colorPickerView.onCreateByBuilder(this);
Expand Down
2 changes: 2 additions & 0 deletions colorpickerview/src/main/res/values/attrs_colorpicker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
</attr>
<!-- enables selector point validation (approximation to find valid color). when disabled, uses exact coordinates without approximation. -->
<attr name="selectorPointValidation" format="boolean" />
<!-- when enabled, automatically resets brightness slider to maximum when selecting colors with low saturation (near center of HSV palette), allowing easy selection of white. -->
<attr name="resetBrightnessOnLowSaturation" format="boolean" />
</declare-styleable>
<declare-styleable name="AlphaSlideBar">
<!-- sets a customized selector drawable. -->
Expand Down