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

Set hex color from input #150

Closed
jcrawford opened this issue Dec 10, 2020 · 3 comments
Closed

Set hex color from input #150

jcrawford opened this issue Dec 10, 2020 · 3 comments

Comments

@jcrawford
Copy link

I have been looking through the documentation and I cannot find a way to set a color as the active color. For instance if I have an input box with an on change event and I enter #ffffff it does not update the selected color in the wheel.

If there is not a way can you implement that feature?

@jaames
Copy link
Owner

jaames commented Dec 10, 2020

Ah, you might be looking for the Working with Colors section in the documentation? The TL;DR is each color picker has a color prop, which has a bunch of properties on it for both setting and getting the currently selected color in a variety of different formats. For setting the color from a hex value, you can use something like colorPicker.color.hexString = '#ff0000'.

Here's an example for hooking up an input element to a color picker's hexString value, and vice versa:

<div id="colorPicker"></div>
<input id="hexInput"></input>
var colorPicker = new iro.ColorPicker('#colorPicker');
var hexInput = document.getElementById('hexInput');

// whenever the input is committed, update the color picker's selected color from the value
hexInput.addEventListener('change', function() {
  // (maybe do some regex validation here to make sure the input is a valid hex string first...)
  colorPicker.color.hexString = this.value;
});

// whenever the color picker is updated, update the input value to match it
colorPicker.on('change', function(color) {
  hexInput.value = color.hexString;
});

I also just quickly updated the Codepen demo to include an input element if you want to play around with it:
https://codepen.io/rakujira/pen/WZOeNq?editors=1010

@jaames jaames closed this as completed Dec 10, 2020
@jcrawford
Copy link
Author

Ahh thank you very much, I was trying to do colorPicker.color = '#FFFFFF';
Appreciate the clarification.

@jaames
Copy link
Owner

jaames commented Dec 10, 2020

No problem! Maybe I ought to improve the documentation there to make it a bit clearer.

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

2 participants