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

Saving Selected Color in input #123

Closed
varunsridharan opened this issue Jul 17, 2019 · 19 comments
Closed

Saving Selected Color in input #123

varunsridharan opened this issue Jul 17, 2019 · 19 comments
Labels
feature request New feature requested improvement Something could be improved

Comments

@varunsridharan
Copy link

Hi Again,!!

i used this library in my framework and found that it dose not support input[type=text] element.
so i created a div and was able to get it working.

and then i found i can't even link input field to this library so it can update the selected color.

so for that i created a small workaround as below

let $save_color = ( $color, $instance ) => {
	let $value = $color.toHEXA().toString();
		if( !window.wponion._.isUndefined( $instance._representation ) ) {
			switch( $instance._representation ) {
				case 'HEXA':
					$value = $color.toHEXA().toString();
					break;
				case 'RGBA':
					$value = $color.toRGBA().toString();
					break;
				case 'HSLA':
					$value = $color.toHSLA().toString();
					break;
				case 'HSVA':
					$value = $color.toHSVA().toString();
					break;
				case 'CMYK':
					$value = $color.toCMYK().toString();
					break;
			}
		}
	this.element.find( 'input.wponion-color-picker-element' ).val( $value );
};

$instance.on( 'save', $save_color );
$instance.on( 'change', $save_color );
$instance.on( 'swatchselect', $save_color );

So i would like to know if there is a chance that you could support input field or even provide a custom argument where i can link my input field to get the values updated.

@simonwep
Copy link
Owner

simonwep commented Jul 17, 2019

You can shorten you code by using template strings:

let $save_color = ($color, {_representation}) => {
    let $value = $color.toHEXA().toString();

    if (!window.wponion._.isUndefined(_representation)) {
        $value = $color[`to${_representation}`].toString();
    }

    this.element.find('input.wponion-color-picker-element').val($value);
};

$instance.on('save', $save_color);
$instance.on('change', $save_color);
$instance.on('swatchselect', $save_color);

I basically don't want to "polyfill" any native HTML5 input element, but I can add a getCurrentRepresentation function to access what the user currently use. That would simplify to current code to (change get's at least one time fired before save with the current color):

let $save_color = (color, pickr) => {
    this.element.find('input.wponion-color-picker-element')
        .val($value[`to${pickr.getCurrentRepresetation()}`].toString(0));
};

$instance.on('change', $save_color);
$instance.on('swatchselect', $save_color);

swatchselect should also fire change, but it doesn't. I'll fix that and you final could would be

$instance.on('change', (color, pickr) => {
    this.element.find('input.wponion-color-picker-element')
        .val($value[`to${pickr.getCurrentRepresetation()}`].toString(0));
});

@simonwep simonwep added feature request New feature requested improvement Something could be improved labels Jul 17, 2019
@varunsridharan
Copy link
Author

@simonwep you proved it again you are a super human 😄
Yea the feature idea which you said would work i think and also thanks for the short code 😄

@simonwep
Copy link
Owner

simonwep commented Jul 17, 2019

Oh lol, there is already getColorRepresentation().
But it's not mentioned in the docs whoops

@varunsridharan
Copy link
Author

lol sorry my bad. i think i am out of energy today !!!!

@simonwep
Copy link
Owner

Haha no problem, If it isn't mentioned anywhere it does basically not exist :D

@simonwep
Copy link
Owner

I've released 1.2.2 for now, I know there's the thing with themes - I'll take care of that in the next release, I promise :)

@varunsridharan
Copy link
Author

@simonwep sorry to bother you again

Error : https://vsp.ams3.cdn.digitaloceanspaces.com/sshots/i/2019/Jul/17/1563371916-177.gif

Code

$instance.on( 'change', ( $color, { _representation } ) => {
				let $value = $color.toHEXA().toString();

				if( !window.wponion._.isUndefined( _representation ) ) {
					$value = $color[ `to${_representation}` ].toString();
				}

				this.element.find( 'input.wponion-color-picker-element' ).val( $value );
			} );

$instance.on('change', (color, pickr) => {
    this.element.find('input.wponion-color-picker-element')
        .val($value[`to${pickr.getCurrentRepresetation()}`].toString(0));
});

Below error for the above code
image

@simonwep
Copy link
Owner

It's getColorRepresentation not getCurrentRepresentation :) The function exists but yeah, it has another name haha. It'd be

$instance.on('change', (color, pickr) => {
    this.element.find('input.wponion-color-picker-element')
        .val($value[`to${pickr.getColorRepresentation()}`].toString(0));
});

@simonwep
Copy link
Owner

The zero in toString means on which decimal place it should be rounded, safari got some serious problems with floating point color stuff so you'd need to pass a zero into it :/

@varunsridharan
Copy link
Author

cool thanks for your faster reply and is it $value or color in the function above ?

@simonwep
Copy link
Owner

Whoops yeah it's color. The dollar sign confuses me:

$instance.on('change', (color, pickr) => {
    this.element.find('input.wponion-color-picker-element')
        .val(color[`to${pickr.getColorRepresentation()}`].toString(0));
});

@varunsridharan
Copy link
Author

@simonwep nope your code is not working i get the below value

function(){var t=function(t,e,n){var r,o=w(t,e,n),i=o[0]/255,a=o[1]/255,c=o[2]/255;return[100*(1===(r=m(1-i,1-a,1-c))?0:(1-i-r)/(1-r)),100*(1===r?0:(1-a-r)/(1-r)),100*(1===r?0:(1-c-r)/(1-r)),100*r]}(i.h,i.s,i.v);return t.toString=o(t,function(t){return"cmyk(".concat(t[0],"%, ").concat(t[1],"%, ").concat(t[2],"%, ").concat(t[3],"%)")}),t}

@varunsridharan
Copy link
Author

Also Note : pickr.getColorRepresentation() returns undefined if user did not select which color type they need eg( RBGA) and so on

@simonwep
Copy link
Owner

If you haven't set defaultRepresentation pickr don't automatically know's which you wanna use. It only assumes it based on default. I can change that, would be better i guess. Checkout this fiddle: https://jsfiddle.net/xoamd8ge/

@simonwep
Copy link
Owner

simonwep commented Jul 17, 2019

And I forgot you only resolve the name with the template string, It's a function so you need to call it.
Update fiddle: https://jsfiddle.net/s092ufox/
Your code:

$instance.on('change', (color, pickr) => {
    this.element.find('input.wponion-color-picker-element')
        .val(color[`to${pickr.getColorRepresentation()}`]().toString(0));
});

At the end there is color.to[RGBA|HEXA...]().toString()

@varunsridharan
Copy link
Author

@simonwep thanks :-)

@simonwep
Copy link
Owner

I'm closing it for now, since 1.2.3 change will be fired in swatchselect :) Reopen if there are any more issues.

@sergeizhukov
Copy link

sergeizhukov commented Aug 13, 2019

@simonwep Hi. I spent couple hours for understand that for changestop event this: color.toHEXA() doesn't work =/

@simonwep
Copy link
Owner

simonwep commented Aug 13, 2019

@sergeizhukov Whoops typo in the README. You only got the instance as argument. I've updated the README, thx :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature requested improvement Something could be improved
Projects
None yet
Development

No branches or pull requests

3 participants