EyeDropper
Sample any pixel on the screen - even outside the browser window - with the EyeDropper API and get the color back as an sRGB hex string, straight into C#.
@inject Bit.Butil.EyeDropper eyeDropperMDN reference
IsSupported.
Open must be called from a user-gesture handler such as a button click - the
browser rejects the request otherwise. The user can cancel the picker at any time with
Esc, in which case Open returns null.
Returns true when the runtime exposes window.EyeDropper. During prerender/SSR the check returns false rather than throwing, so defer it to OnAfterRenderAsync.
@inject Bit.Butil.EyeDropper eyeDropper
var supported = await eyeDropper.IsSupported();Opens the eyedropper cursor and resolves with the picked sRGB color in hex form (for example #1f2937). The cursor can sample any pixel on the screen, including other applications. A null result means the user cancelled or the runtime couldn't show the picker.
var hex = await eyeDropper.Open();
if (hex is not null)
{
// hex == "#1f2937" - feed it straight into a style or a theme
}Unlike the raw JS API, which rejects the promise on cancel, Butil normalizes both cancellation and picker failures to a null return - so no try/catch is needed for the common flow. A try/catch remains a good idea for unexpected interop failures.
var hex = await eyeDropper.Open();
if (hex is null)
{
// user pressed Esc, or the picker couldn't be shown
return;
}
ApplyAccentColor(hex);API reference
ValueTask<bool> IsSupported()ValueTask<string?> Open()