Skip to content

Commit cb19704

Browse files
authored
Merge pull request #182 from skydoves/feat/selectorPointValidation
Implement selectorPointValidation
2 parents 8a3ff15 + 4e493c8 commit cb19704

5 files changed

Lines changed: 44 additions & 52 deletions

File tree

.github/workflows/publish-snapshot.yml

Lines changed: 0 additions & 38 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,7 @@ Add the dependency below to your module's `build.gradle` file:
3131

3232
```gradle
3333
dependencies {
34-
implementation "com.github.skydoves:colorpickerview:2.3.0"
35-
}
36-
```
37-
38-
## SNAPSHOT
39-
[![ColorPickerView](https://img.shields.io/static/v1?label=snapshot&message=ColorPickerView&logo=apache%20maven&color=C71A36)](https://oss.sonatype.org/content/repositories/snapshots/com/github/skydoves/colorpickerview/) <br>
40-
Snapshots of the current development version of ColorPickerView are available, which track [the latest versions](https://oss.sonatype.org/content/repositories/snapshots/com/github/skydoves/colorpickerview/).
41-
42-
```Gradle
43-
repositories {
44-
maven {
45-
url 'https://oss.sonatype.org/content/repositories/snapshots/'
46-
}
34+
implementation("com.github.skydoves:colorpickerview:2.3.0")
4735
}
4836
```
4937

colorpickerview/src/main/java/com/skydoves/colorpickerview/ColorPickerView.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public class ColorPickerView extends FrameLayout implements LifecycleObserver {
104104
private boolean VISIBLE_FLAG = false;
105105

106106
private String preferenceName;
107+
private boolean selectorPointValidation = true;
107108
private final ColorPickerPreferenceManager preferenceManager =
108109
ColorPickerPreferenceManager.getInstance(getContext());
109110

@@ -173,6 +174,10 @@ private void getAttrs(AttributeSet attrs) {
173174
if (a.hasValue(R.styleable.ColorPickerView_initialColor)) {
174175
setInitialColor(a.getColor(R.styleable.ColorPickerView_initialColor, Color.WHITE));
175176
}
177+
if (a.hasValue(R.styleable.ColorPickerView_selectorPointValidation)) {
178+
this.selectorPointValidation =
179+
a.getBoolean(R.styleable.ColorPickerView_selectorPointValidation, selectorPointValidation);
180+
}
176181
} finally {
177182
a.recycle();
178183
}
@@ -278,6 +283,7 @@ protected void onCreateByBuilder(Builder builder) {
278283
if (builder.preferenceName != null) setPreferenceName(builder.preferenceName);
279284
if (builder.initialColor != 0) setInitialColor(builder.initialColor);
280285
if (builder.lifecycleOwner != null) setLifecycleOwner(builder.lifecycleOwner);
286+
this.selectorPointValidation = builder.selectorPointValidation;
281287
}
282288

283289
@SuppressLint("ClickableViewAccessibility")
@@ -929,6 +935,33 @@ public void setPreferenceName(@Nullable String preferenceName) {
929935
}
930936
}
931937

938+
/**
939+
* Returns whether selector point validation (approximation) is enabled.
940+
*
941+
* <p>When enabled, the selector point is approximated to find the nearest valid color,
942+
* which handles edge cases where users touch outside the image matrix.
943+
* When disabled, exact coordinates are used without approximation.
944+
*
945+
* @return true if selector point validation is enabled, false otherwise.
946+
*/
947+
public boolean isSelectorPointValidationEnabled() {
948+
return selectorPointValidation;
949+
}
950+
951+
/**
952+
* Sets whether selector point validation (approximation) is enabled.
953+
*
954+
* <p>When enabled (default), the selector point is approximated to find the nearest valid color,
955+
* which handles edge cases where users touch outside the image matrix.
956+
* When disabled, exact coordinates are used without approximation, which is useful
957+
* when precise point restoration is needed (e.g., saving and restoring selector positions).
958+
*
959+
* @param enabled true to enable validation, false to disable.
960+
*/
961+
public void setSelectorPointValidation(boolean enabled) {
962+
this.selectorPointValidation = enabled;
963+
}
964+
932965
/**
933966
* sets the {@link LifecycleOwner}.
934967
*
@@ -990,6 +1023,7 @@ public static class Builder {
9901023
private int height = LayoutParams.MATCH_PARENT;
9911024
private String preferenceName;
9921025
private LifecycleOwner lifecycleOwner;
1026+
private boolean selectorPointValidation = true;
9931027

9941028
public Builder(Context context) {
9951029
this.context = context;
@@ -1085,6 +1119,11 @@ public Builder setLifecycleOwner(LifecycleOwner lifecycleOwner) {
10851119
return this;
10861120
}
10871121

1122+
public Builder setSelectorPointValidation(boolean enabled) {
1123+
this.selectorPointValidation = enabled;
1124+
return this;
1125+
}
1126+
10881127
public ColorPickerView build() {
10891128
ColorPickerView colorPickerView = new ColorPickerView(context);
10901129
colorPickerView.onCreateByBuilder(this);

colorpickerview/src/main/java/com/skydoves/colorpickerview/PointMapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ protected static Point getColorPoint(ColorPickerView colorPickerView, Point poin
2626
Point center =
2727
new Point(colorPickerView.getWidth() / 2, colorPickerView.getMeasuredHeight() / 2);
2828
if (colorPickerView.isHuePalette()) return getHuePoint(colorPickerView, point);
29+
if (!colorPickerView.isSelectorPointValidationEnabled()) return point;
2930
return approximatedPoint(colorPickerView, point, center);
3031
}
3132

colorpickerview/src/main/res/values/attrs_colorpicker.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<attr name="selector" format="integer" />
2323
<!-- sets a width/height square size of the selector. -->
2424
<attr name="selector_size" format="dimension" />
25-
<!-- sets an alpha of thr selector. -->
25+
<!-- sets an alpha of the selector. -->
2626
<attr name="selector_alpha" format="float" />
2727
<!-- sets an alpha of the flag. -->
2828
<attr name="flag_alpha" format="float" />
@@ -41,6 +41,8 @@
4141
<!-- trigger the listener only the last color when user release tapping. -->
4242
<enum name="last" value="1" />
4343
</attr>
44+
<!-- enables selector point validation (approximation to find valid color). when disabled, uses exact coordinates without approximation. -->
45+
<attr name="selectorPointValidation" format="boolean" />
4446
</declare-styleable>
4547
<declare-styleable name="AlphaSlideBar">
4648
<!-- sets a customized selector drawable. -->

0 commit comments

Comments
 (0)