Pick Colors from websites with the JavaScript EyeDropper API

Mid-August the WICG published a (unofficial) specification for a Native Eye Dropper — a tool you can use to get the color from the pixel that’s underneath the cursor — on the web. Let’s take a look … Usage The proposal is pretty straightforward: This API enables authors to use a browser supplied eyedropper in …


This content originally appeared on Bram.us and was authored by Bramus!

Mid-August the WICG published a (unofficial) specification for a Native Eye Dropper — a tool you can use to get the color from the pixel that’s underneath the cursor — on the web.

Let’s take a look …

~

Usage

The proposal is pretty straightforward:

This API enables authors to use a browser supplied eyedropper in the construction of custom color pickers.

It describes an EyeDropper and a ColorSelectionResult which you use as follows:

// Create an EyeDropper object
const eyeDropper = new EyeDropper();

document.getElementById("eyedropperButton").addEventListener('click', e => {
    // Enter eyedropper mode
    eyeDropper.open()
        .then(colorSelectionResult => {
            // returns hex color value (#RRGGBB) of the selected pixel
            console.log(colorSelectionResult.sRGBHex);
        })
        .catch(error => {
            // handle the user choosing to exit eyedropper mode without a selection
        });
});

Upon clicking the button in the example above, an EyeDropper will open. After clicking, the color underneath will be captured and its outcome is a ColorSelectionResult, of which you can current get the SRGB Hex Code via colorSelectionResult.sRGBHex

Live updating the color value as you move the cursor is currently not specced — it is, after all, a very young spec. This is considered a limitation and is being discussed in this GitHub Issue.

~

Demo (Recording) + Browser Support

Whilst the API is still unofficial, it’s already supported in Chromium 95 (current Canary). You can see it in action in this video:

In the video I open the EyeDropper via this one-liner on the Console:

await new EyeDropper().open();

Other Browsers (Firefox, Safari) have no support for it (yet). Relevant Issues to track:

~

Standalone Demo

See the Pen
EyeDropper API Demo
by Bramus (@bramus)
on CodePen.

~

To help spread the contents of this post, feel free to retweet the announcement tweet:

~

? Like what you see? Want to stay in the loop? Here's how:

? This post originally was a set of tweets.


This content originally appeared on Bram.us and was authored by Bramus!


Print Share Comment Cite Upload Translate Updates
APA

Bramus! | Sciencx (2021-09-01T08:37:31+00:00) Pick Colors from websites with the JavaScript EyeDropper API. Retrieved from https://www.scien.cx/2021/09/01/pick-colors-from-websites-with-the-javascript-eyedropper-api/

MLA
" » Pick Colors from websites with the JavaScript EyeDropper API." Bramus! | Sciencx - Wednesday September 1, 2021, https://www.scien.cx/2021/09/01/pick-colors-from-websites-with-the-javascript-eyedropper-api/
HARVARD
Bramus! | Sciencx Wednesday September 1, 2021 » Pick Colors from websites with the JavaScript EyeDropper API., viewed ,<https://www.scien.cx/2021/09/01/pick-colors-from-websites-with-the-javascript-eyedropper-api/>
VANCOUVER
Bramus! | Sciencx - » Pick Colors from websites with the JavaScript EyeDropper API. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/01/pick-colors-from-websites-with-the-javascript-eyedropper-api/
CHICAGO
" » Pick Colors from websites with the JavaScript EyeDropper API." Bramus! | Sciencx - Accessed . https://www.scien.cx/2021/09/01/pick-colors-from-websites-with-the-javascript-eyedropper-api/
IEEE
" » Pick Colors from websites with the JavaScript EyeDropper API." Bramus! | Sciencx [Online]. Available: https://www.scien.cx/2021/09/01/pick-colors-from-websites-with-the-javascript-eyedropper-api/. [Accessed: ]
rf:citation
» Pick Colors from websites with the JavaScript EyeDropper API | Bramus! | Sciencx | https://www.scien.cx/2021/09/01/pick-colors-from-websites-with-the-javascript-eyedropper-api/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.