skydoves / ColorPickerView

🎨 Android colorpicker for getting colors from any images by tapping on the desired color.
Apache License 2.0
1.58k stars 212 forks source link

API for setting the colour to start with #41

Closed eku closed 4 years ago

eku commented 5 years ago

When creating ColorPickerView, I want to specify an RGB color initially and expect the selector to be placed on the corresponding color in the color wheel as well as in the brightness slider.

The method setPureColor is not suitable for this.

Persisting via the PreferencesManager is not desired.

skydoves commented 5 years ago

@eku Hi,

I think you can solve it using selectByHsv method. But there are some constraints for using the method. The palette should be used the default palette in the internal ColorPickerView. And here is the demo project.

eku commented 5 years ago

@skydoves thanks for the hint. I'm using the default palette.

There's only one problem. At what point can you call the method to make it work? The calculated radius is always zero, so the point is centered. Even in the onResume of the fragment that uses the ColorPickerView in its layout, this still seems to be too early.

skydoves commented 5 years ago

@eku A view tree observer is used to register listeners that can be notified of global changes in the view tree. You can get notified when view loading finished.

colorPickerView.getViewTreeObserver()
        .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
          colorPickerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        else {
          colorPickerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }

        // doSomething
      }
    });
eku commented 5 years ago

That worked somehow. But the calculation of the position is wrong. colorpicker

arpanbose commented 5 years ago

What if I use the Dialog, what to do then?

domazey commented 4 years ago

For anyone who looks for workaround, I found out that selectByHsv works correctly only, if that palette is used:

image

eku commented 4 years ago

@xinaiz I would prefer a correct calculation rather than another palette image.

lgvalle commented 4 years ago

Is there a way to go around this then? I'd like to set a pure color and get the selector and brightness bar updated correctly

lgvalle commented 4 years ago

Just to explain the problem with code:

        colorPickerView.colorListener = ColorEnvelopeListener { envelope: ColorEnvelope, fromUser ->
            if (fromUser) {
                println("""First color: ${envelope.hexCode}""")
                colorPickerView.selectByHsv(envelope.color)
                println("""Second color: ${colorPickerView.colorEnvelope.hexCode}""")
            }
        }

This will print out two different colors.

Which means if you feed the colorPickerView with the same color it gives you, it produces a different color

barkantoprak commented 4 years ago

Any news about this issue? @eku

eku commented 4 years ago

@barkantoprak I switched to Android-Color-Picker.

dondell commented 4 years ago

colorPickerView.selectByHsv(Color.parseColor("#FF0099CC")); is not working for me. It would be better for this to work so we can add default value.

tfrysinger commented 4 years ago

I would also like to be able to do this. I am using setPureColor just to get the color initially selected correctly but as others have pointed out it doesn't move the selector properly.

teapack commented 4 years ago

@eku The calculation is correct, it's the palette image which needs to be changed.

@skydoves The palette can be circular (it doesn't have to be the hexagon mentioned above), but it cannot be what it is now. It needs to be HSV/HSL color model palette, for example something like this: New color palette

Notice how the colors are laid out a bit differently when compared to the old palette: Original and new palette comparison Red is always on the right side (0°), but then the colors are a bit shifted (e.g. cyan should correctly be exactly at 180°, but it's at cca 225° on the original palette).

I took a look at your code and you're not doing anything wrong. You're calling the android.graphics.Color.colorToHSV(color, hsv); method which converts the given ARGB color (the alpha component is ignored) to its HSV components. You just need to use the HSV palette, otherwise the first HSV component (hue) (hsv[0]) (which is basically the angle) won't be represented correctly on the palette.

I couldn't find the required palette in better resolution, though. I tried making it as xml drawable, but I failed.

eku commented 4 years ago

Is there a way to have the palette drawn by Android at runtime instead of bringing it as a drawable resource?

The above mentioned Android-Color-Picker draws it at runtime.

skydoves commented 4 years ago

Hi, @teapack, @eku! I'm actually already worked on solutions and below functions will be released in the next version.

setHsvPaletteDrawable

The default palette drawable is ColorHsvPalette if not set a palette. This method can be used for changing as ColorHsvPalette from another palette drawable.

selectByHsvColor

changes selector's selected point by a specific color.

selectByHsvColorRes

changes selector's selected point by a specific color resource.

The default palette will be drawn using ColorHuePalette. ColorHuePalette is a default drawable palette build by HSV (hue, saturation, value) color model for alternating representations of the RGB color model.

And now you can build and test using the demo project. Thank you for your issue :)

skydoves commented 4 years ago

Hey guys, It is released a new version 2.1.9! Now we can use the selectByHsvColor(color) and selectByHsvColorRes(resource) properly in the new release. Thank you for your conversations and issues. 👍

eku commented 4 years ago

After all, after more than a year we now have a solution to the problem. Thanks for that.

skydoves commented 4 years ago

Thanks to everyone who made conversations and tried to resolve this issue. 🎉