-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
sNotzColor: Use own color picker
Which is a modernized, simplified version of colorpicker library by @QuadFlask (ref: https://github.com/QuadFlask/colorpicker/). Due credits to the original author. Signed-off-by: sunilpaulmathew <[email protected]>
1 parent
5039cb0
commit 49364a7
Showing
25 changed files
with
1,251 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
app/src/main/java/com/sunilpaulmathew/snotz/colorpicker/ColorPickerDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package com.sunilpaulmathew.snotz.colorpicker; | ||
|
||
import android.content.Context; | ||
import android.content.DialogInterface; | ||
import android.graphics.Color; | ||
import android.view.Gravity; | ||
import android.view.ViewGroup; | ||
|
||
import androidx.appcompat.widget.LinearLayoutCompat; | ||
|
||
import com.google.android.material.dialog.MaterialAlertDialogBuilder; | ||
import com.sunilpaulmathew.snotz.R; | ||
import com.sunilpaulmathew.snotz.colorpicker.interfaces.ColorPickerClickListener; | ||
import com.sunilpaulmathew.snotz.colorpicker.interfaces.OnColorSelectedListener; | ||
import com.sunilpaulmathew.snotz.colorpicker.views.AlphaSlider; | ||
import com.sunilpaulmathew.snotz.colorpicker.views.ColorPickerView; | ||
import com.sunilpaulmathew.snotz.colorpicker.views.LightnessSlider; | ||
|
||
public class ColorPickerDialog { | ||
|
||
private final ColorPickerView colorPickerView; | ||
private final LinearLayoutCompat pickerContainer; | ||
private final MaterialAlertDialogBuilder builder; | ||
|
||
private final Integer[] initialColor = new Integer[] { | ||
null, null, null, null, null | ||
}; | ||
|
||
private ColorPickerDialog(Context context) { | ||
this(context, 0); | ||
} | ||
|
||
private ColorPickerDialog(Context context, int theme) { | ||
int defaultMargin = getDimensionAsPx(context, R.dimen.default_slider_margin); | ||
int defaultMarginTop = getDimensionAsPx(context, R.dimen.default_margin_top); | ||
|
||
builder = new MaterialAlertDialogBuilder(context, theme); | ||
pickerContainer = new LinearLayoutCompat(context); | ||
pickerContainer.setOrientation(LinearLayoutCompat.VERTICAL); | ||
pickerContainer.setGravity(Gravity.CENTER_HORIZONTAL); | ||
pickerContainer.setPadding(defaultMargin, defaultMarginTop, defaultMargin, 0); | ||
|
||
LinearLayoutCompat.LayoutParams layoutParamsForColorPickerView = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0); | ||
layoutParamsForColorPickerView.weight = 1; | ||
colorPickerView = new ColorPickerView(context); | ||
|
||
pickerContainer.addView(colorPickerView, layoutParamsForColorPickerView); | ||
|
||
builder.setView(pickerContainer); | ||
} | ||
|
||
public static ColorPickerDialog with(Context context) { | ||
return new ColorPickerDialog(context); | ||
} | ||
|
||
public static ColorPickerDialog with(Context context, int theme) { | ||
return new ColorPickerDialog(context, theme); | ||
} | ||
|
||
public ColorPickerDialog setTitle(String title) { | ||
builder.setTitle(title); | ||
return this; | ||
} | ||
|
||
public ColorPickerDialog setTitle(int titleId) { | ||
builder.setTitle(titleId); | ||
return this; | ||
} | ||
|
||
public ColorPickerDialog initialColor(int initialColor) { | ||
this.initialColor[0] = initialColor; | ||
return this; | ||
} | ||
|
||
public ColorPickerDialog density(int density) { | ||
colorPickerView.setDensity(density); | ||
return this; | ||
} | ||
|
||
public ColorPickerDialog setOnColorSelectedListener(OnColorSelectedListener onColorSelectedListener) { | ||
colorPickerView.addOnColorSelectedListener(onColorSelectedListener); | ||
return this; | ||
} | ||
|
||
public ColorPickerDialog setPositiveButton(CharSequence text, final ColorPickerClickListener onClickListener) { | ||
builder.setPositiveButton(text, (dialog, which) -> positiveButtonOnClick(dialog, onClickListener)); | ||
return this; | ||
} | ||
|
||
public ColorPickerDialog setPositiveButton(int textId, final ColorPickerClickListener onClickListener) { | ||
builder.setPositiveButton(textId, (dialog, which) -> positiveButtonOnClick(dialog, onClickListener)); | ||
return this; | ||
} | ||
|
||
public ColorPickerDialog setNegativeButton(CharSequence text, DialogInterface.OnClickListener onClickListener) { | ||
builder.setNegativeButton(text, onClickListener); | ||
return this; | ||
} | ||
|
||
public ColorPickerDialog setNegativeButton(int textId, DialogInterface.OnClickListener onClickListener) { | ||
builder.setNegativeButton(textId, onClickListener); | ||
return this; | ||
} | ||
|
||
public MaterialAlertDialogBuilder build() { | ||
Context context = builder.getContext(); | ||
colorPickerView.setInitialColors(initialColor, getStartOffset(initialColor)); | ||
boolean isBorderEnabled = true; | ||
colorPickerView.setShowBorder(isBorderEnabled); | ||
|
||
LinearLayoutCompat.LayoutParams layoutParamsForLightnessBar = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getDimensionAsPx(context, R.dimen.default_slider_height)); | ||
LightnessSlider lightnessSlider = new LightnessSlider(context); | ||
lightnessSlider.setLayoutParams(layoutParamsForLightnessBar); | ||
pickerContainer.addView(lightnessSlider); | ||
colorPickerView.setLightnessSlider(lightnessSlider); | ||
lightnessSlider.setColor(getStartColor(initialColor)); | ||
lightnessSlider.setShowBorder(isBorderEnabled); | ||
|
||
LinearLayoutCompat.LayoutParams layoutParamsForAlphaBar = new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getDimensionAsPx(context, R.dimen.default_slider_height)); | ||
AlphaSlider alphaSlider = new AlphaSlider(context); | ||
alphaSlider.setLayoutParams(layoutParamsForAlphaBar); | ||
pickerContainer.addView(alphaSlider); | ||
colorPickerView.setAlphaSlider(alphaSlider); | ||
alphaSlider.setColor(getStartColor(initialColor)); | ||
alphaSlider.setShowBorder(isBorderEnabled); | ||
|
||
return builder; | ||
} | ||
|
||
private Integer getStartOffset(Integer[] colors) { | ||
int start = 0; | ||
for (int i = 0; i < colors.length; i++) { | ||
if (colors[i] == null) { | ||
return start; | ||
} | ||
start = (i + 1) / 2; | ||
} | ||
return start; | ||
} | ||
|
||
private int getStartColor(Integer[] colors) { | ||
Integer startColor = getStartOffset(colors); | ||
return startColor == null ? Color.WHITE : colors[startColor]; | ||
} | ||
|
||
private static int getDimensionAsPx(Context context, int rid) { | ||
return (int) (context.getResources().getDimension(rid) + .5f); | ||
} | ||
|
||
private void positiveButtonOnClick(DialogInterface dialog, ColorPickerClickListener onClickListener) { | ||
int selectedColor = colorPickerView.getSelectedColor(); | ||
Integer[] allColors = colorPickerView.getAllColors(); | ||
onClickListener.onClick(dialog, selectedColor, allColors); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/sunilpaulmathew/snotz/colorpicker/License.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
## License | ||
|
||
``` | ||
Copyright (C) 2014-2017 QuadFlask | ||
Copyright (C) 2025-2026 sunilpaulmathew <[email protected]> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
``` |
7 changes: 7 additions & 0 deletions
7
.../main/java/com/sunilpaulmathew/snotz/colorpicker/interfaces/ColorPickerClickListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.sunilpaulmathew.snotz.colorpicker.interfaces; | ||
|
||
import android.content.DialogInterface; | ||
|
||
public interface ColorPickerClickListener { | ||
void onClick(DialogInterface d, int lastSelectedColor, Integer[] allColors); | ||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/sunilpaulmathew/snotz/colorpicker/interfaces/ColorWheelRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.sunilpaulmathew.snotz.colorpicker.interfaces; | ||
|
||
import android.graphics.Canvas; | ||
|
||
import com.sunilpaulmathew.snotz.colorpicker.utils.ColorCircle; | ||
|
||
import java.util.List; | ||
|
||
public interface ColorWheelRenderer { | ||
float GAP_PERCENTAGE = 0.025f; | ||
|
||
void draw(int density, float maxRadius, float lightness, float alpha, float strokeWidth, float cSize, Canvas targetCanvas); | ||
|
||
List<ColorCircle> getColorCircleList(); | ||
} |
Oops, something went wrong.