Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 869

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T02:52:08+00:00 2024-11-25T02:52:08+00:00

The EyeDropper API: Pick colors from anywhere on your screen

  • 62k

With the new EyeDropper API in Chromium, websites can let visitors pick colors from anywhere on their screen, adding another
feature to the web that used to require hacky solutions and is now just a few lines of code. The API is
clean and modern and easy to use. In this article we'll discuss how to set it up, handle edge cases and additional features
we hope will land in future updates.

We've been following the EyeDropper API since it was first proposed and have been experimenting with it as different
parts became available as well as providing input while the feature was being developed. In Polypane 7
we started using it extensively for the new color picker and new palettes.

How to use the EyeDropper API

The API adds a new global, EyeDropper (or window.EyeDropper) that you can use to set up a new eyedropper object:

const eyeDropper = new EyeDropper(); 
Enter fullscreen mode Exit fullscreen mode

This eyeDropper object has one function, eyeDropper.open(). This starts the color picker and changes the users cursor
into a color picker, complete with magnified area and a highlighted pixel. This function returns a promise, so you can
use it either with await or as a promise.

One gotcha is that it only works when called from a user-initiated event. This is part of the security model, to
prevent websites from potentially scraping pixels without the user knowing.

Detecting support for the EyeDropper API

Because the API is only available in Chromium you will need to check for support before using it. The most straightforward
way to do that is to only offer your color picking UI when window.EyeDropper is not undefined:

if (window.EyeDropper) {   // Okay to use EyeDropper } else {   // Hide the UI } 
Enter fullscreen mode Exit fullscreen mode

await based version

// won't work const result = await eyeDropper.open();  // works document.queryselector('.colorbutton')   .addEventListener('click', async () => {     const result = await eyeDropper.open();   }); 
Enter fullscreen mode Exit fullscreen mode

The eyeDropper.open() call will resolve in two situations:

  1. The user clicks anywhere on the screen.
  2. The user pressed the Esc key.

In the last situation the eyeDropper will throw an exception, but in the first situation you will get a ColorSelectionResult
object, which has an sRGBHex property containing the picked color in hexadecimal format. In your code you can check if
result.sRGBHex is defined and then do with it what you want.

document.queryselector('.colorbutton')   .addEventListener('click', async () => {     const result = await eyeDropper.open();      if (result.sRGBHex) {       console.log(result.sRGBHex);     }   }); 
Enter fullscreen mode Exit fullscreen mode

You don't have to handle the exception but if you wanted to provide the user feedback that they cancelled the eyedropper,
you need to add a try .. catch to the code:

document.queryselector('.colorbutton')   .addEventListener('click', async () => {     try {       const result = await eyeDropper.open();        if (result.sRGBHex) {         console.log(result.sRGBHex);       }     } catch (e) {       console.log(e);       // "DOMException: The user canceled the selection."     }   }); 
Enter fullscreen mode Exit fullscreen mode

Promise based version

You don't have to use the await version. eyeDropper.open() returns a promise, so adding a .then() and .catch() also works:

document.queryselector('.colorbutton')   .addEventListener('click', () => {     eyeDropper       .open()       .then((result) => {         console.log(result.sRGBHex);       })       .catch((e) => {         console.log(e);         // "DOMException: The user canceled the selection."       });   }); 
Enter fullscreen mode Exit fullscreen mode

Things to keep in mind when using the EyeDropper API

There are two gotchas with the API, at least as it's currently implemented in Chromium that we've found that you should
be aware of.

Color picking does not use the live screen

At least in the current implementation, the color picker get the pixels as shown on the screen when you call .open().
This means that if you're playing video the color picker will show the pixels of the frame that was visible then, not the
live video.

This is dependent on the implementation and we hope a future update of Chromium will allow for live data.

Color picking only works as the result of a user action

As mentioned earlier you need a user initiated event to open the eye dropper. This is to prevent sites from opening the
eyedropper UI to start scraping your screen right on load. Instead the user needs to perform an action for the API to work,
like a click or keypress.

Features we want to see added

The EyeDropper API is still very young and minimal. During our implementation we encountered a number of features that we
would like to see added to the API in future updates.

Live preview of the hovered color

A major component of many eye droppers, like those in design tools, is that they also show a preview swatch of the
hovered color. You can use this to compare it to another swatch or quickly check a HEX code. The current API does not
offer this over security concerns. We have filed an issue against the EyeDropper API on GitHub for this: #6 Live feedback is needed.

A more extensive color model

Currently, all colors are returned in the sRGB color model. This means the API won't accurately return colors outside
the sRGB spectrum, for example those on Apple's P3 screens. How to deal with this is an open issue.
Work is also happening on a new Color API for the web. The EyeDropper API could use
this Color API when it lands in future versions of browsers.

A more natural way to select multiple colors

Because of the current security model, each time a user picks a color they need to re-initiate a user action which can be tedious.
For example if you want to create a palette of colors in one go, you want to start picking colors, click on all the colors you
want to add and then close out of the eye dropper. We similarly filed an issue for this on Github: #9 Do we expect multiselect to work? and this feature is currently being considered.

For this it would be nice if we could designate a part of the page (like a button) as an area where the EyeDropper
doesn't work, that instead functions as a “done” button. This way users can select multiple colors and then click that
button when they're done.

Other browsers

For now, the API is only available in Chromium based browsers from version 95 on and there has not been a signal from
Safari and Firefox yet. If you want those browsers to support the EyeDropper API as well, add your support to the open issues:
Issue #1728527 for Firefox and Issue #229755 for Safari.

The EyeDropper API is a nice addition to the browser that we hope to see land in more browsers. We make good use of it
in Polypane and would like to see it be developed further.

javascriptuxwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.