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

Holo Color Picker fails to give exact color #31

Open
huteri opened this issue Dec 25, 2013 · 5 comments
Open

Holo Color Picker fails to give exact color #31

huteri opened this issue Dec 25, 2013 · 5 comments

Comments

@huteri
Copy link

huteri commented Dec 25, 2013

Hi, I have issue with this holo color picker, I set the old color with int -6697984 but when I get the color from the picker, it returns -6632448. I have added saturation, value, opacity and even sv bar, but it still does not give exact color int as I set before.
Do you know what is the problem and what probably could be the solution?

@mg6maciej
Copy link

If you want to return the exact same color you send, you need to have both SaturationBar and ValueBar assigned to ColorPicker.

colorPicker.addSaturationBar(saturationBar);
colorPicker.addValueBar(valueBar);

This is because HSV colors consist of these three values.

I'm not sure what is SVBar for here. I initially wanted to use that and had the same issue. It could be replaced with SVTriangle like on this image: http://pl.wikipedia.org/wiki/Plik:HSV_cone.jpg

@LarsWerkman Very nice design, thank you for putting it on maven central.

@mg6maciej
Copy link

Oh. I translated your colors to hex (#99CC00 and #9ACC00) and I think the reason here is different. I haven't looked at the code yet, but I guess color is kept inside the library as HSV, when you always set and get it as RGB. Some bits are simply lost in the translation.

@LarsWerkman
Copy link
Owner

@mg6maciej yes that is exactly what is happening. the reason for this is when setting a color in the color picker you have to translate this to a radial value for the colorpicker and a value for the bars.
and then when you get a value the request the radial value from the colorpicker. thats why you lose some accuracy of the value you put in.
@huteri

@mg6maciej
Copy link

I'm not sure this should be closed without resolving.

When using ColorPicker.setColor followed by ColorPicker.getColor, I would expect to get the exact same value at least when all additional Views (saturation, value) are attached.

It's even more important if you use ColorPicker together with 3 NumberPickers which enable you to set any RGB value. When you change one of the RGB values, it updates ColorPicker and OnColorChangeListener is fired to update NumberPickers with value often different than original by 1 bit. This blocks user from swiping up/down to change RGB value.

Relevant code:

    colorPicker.setOnColorChangedListener(new ColorPicker.OnColorChangedListener() {
        @Override
        public void onColorChanged(int color) {
            redPicker.setValue(Color.red(color));
            greenPicker.setValue(Color.green(color));
            bluePicker.setValue(Color.blue(color));
        }
    });
    NumberPicker.OnValueChangeListener listener = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker numberPicker, int oldValue, int newValue) {
            int color = Color.rgb(redPicker.getValue(), greenPicker.getValue(), bluePicker.getValue());
            colorPicker.setColor(color);
        }
    };
    redPicker.setOnValueChangedListener(listener);
    greenPicker.setOnValueChangedListener(listener);
    bluePicker.setOnValueChangedListener(listener);

I have made a quick fix for this case: mg6maciej@6f8dd87. It's not a complete solution, but works well with saturation + value bars.

Ideally ColorPicker should keep the value in RGB and only use HSV for displaying purposes.

@LarsWerkman LarsWerkman reopened this Mar 11, 2014
@mreinerth
Copy link

here is my version of the quickfix from mg6maciej https://github.com/mreinerth/HoloColorPicker/compare/hotfix/SetColor_GetColor_consistency. Works perfect for me to fix the problem that u dont get the right color u set before. I use the Colorpicker in a dialog and have additionally a gridview under the picker and bars with predefined colors where a click set the color of the picker and the ok button of the dialog save the color (obtained by getColor) to use as a draw color. When i open the Dialog with the picker i set the oldCentercolor and the color to my saved color value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants