• Stars
    star
    124
  • Rank 279,321 (Top 6 %)
  • Language
    HTML
  • License
    Other
  • Created about 3 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A proposal and draft spec for a Color object for the Web Platform, loosely influenced by the Color.js work. Heavily WIP, if you landed here randomly, please move along.

A native Color object for the Web Platform (Slides)

Use cases and motivation

  • A format for APIs to expose colors to the developer. Some of the APIs in need for this are:
  • Lossless color space conversion (e.g. LCH → P3) by default, optional gamut mapping.
  • Color manipulation (e.g. making a color darker by reducing its LCH lightness) with choice of manipulation color space
  • Difference between two colors (ΔE)
  • String parsing (e.g. what color is rebeccapurple?)
  • WCAG 2.1 (or it's successor) color contrast (for any color space, not just sRGB)
  • Prototyping new functionality for incubation, before standardization

Use cases postponed to L2

  • Interpolation (e.g. mixing two colors, compositing, generating color scales) with choice of interpolation color space
  • Compositing and blending
  • Parsing and serialization of custom formats

Audience

Web developers with varying levels of Color Science knowledge. Usable without error by those with little, powerful for those with much.

Goals

  • Usability as a priority
    • Common things should be easy, complex things should be possible
    • Learnability: don't require a ton of color science knowledge to use
      • Handle linearization, chromatic adaptation automatically when needed
      • Consistent API shape independent of input syntax
    • Efficiency: Avoid verbosity, have sensible defaults
    • Safety: Avoid error conditions if possible
    • Liberal in what is accepted (for arguments)
  • Color-space agnostic
    • API should make no assumptions about number, names, or ranges of components
      • Ok to only support color spaces with numeric components
    • Should not privilege certain color spaces over others, whenever possible
    • Authors should be able to register new color spaces, either via a JS version of @color-profile or by directly providing conversion code to and from a supported color space.
    • Should be able to support HDR color spaces, and SDR → HDR conversion
    • No hidden gamut mapping or clipping
  • D65 relative CIE XYZ connection space for SDR
    • (extended rec2020-linear, as used in Canvas HDR will give same result)
    • Configurable media white level for HDR (203cd/m² default for absolute)
  • Extensibility
    • sufficient power to allow polyfilling and experimentation
    • introspection would be good

Predefined color spaces

This set covers the union of spaces from CSS Color 4 and Canvas HDR. All RGB spaces are defined over the extended range.

SDR

  • srgb (Web legacy compatibility)
  • srgb-linear (as used in Canvas HDR, some GPU operations, native APIs)
  • display-p3 (new Web)
  • a98-rgb (?? needed, nowadays?)
  • prophoto-rgb (from raw digital photos)
  • rec2020 (streaming and broadcast)
  • rec2020-linear (canvas uses as connection space)
  • xyz (relative, D65) (for linear-light calculations)
  • lab (D50) (perceptual calculations)
  • lch (D50) (perceptual, chroma-preserving)
  • oklab (D65) (perceptual calculations)
  • oklch (D65) (perceptual, chroma-preserving)

HDR

  • rec2100-pq (Netflix, Canvas HDR)
  • rec2100-hlg (BBC, Canvas HDR)

API sketch

Sample WebIDL and algorithms moved to the draft spec.

Example usage

Reading coordinates

For ease of use and widest applicability, coordinates are plain JavaScript number (for a single coordinate), or an array of number (for all coordinates in a given colorspace).

let color = new Color("rebeccapurple");

// Get individual coord in other color space
color.get("lch", "l"); // 32.4

// Get individual coord in current color space
color.get("r"); // 0.4

// Get all coords in another color space
color.to("lch").coords; // [32.4, 61.2, 309]

Parsing color and converting to a specific color space

Converting in place:

let color = new Color("rebeccapurple");
color.colorSpace; // "srgb";
color.coords; // [0.4, 0.2, 0.6]
color.colorSpace = "lch";
color.coords; // [32.4, 61.2, 309]

By creating a new object:

let color = new Color("rebeccapurple");
color.colorSpace; // "srgb";
color.coords; // [0.4, 0.2, 0.6]
let lchColor = color.to("lch");
lchColor.coords; // [32.4, 61.2, 309]

Lightening a color without changing its color space

color.set("lch", "l", color.get("lch", "l") * 1.2);

Another possibility for relative manipulations:

color.set("lch", "l", l => l * 1.2);

Calculating WCAG 2.1 contrast

This is straightforward, but could also be built-in as a contrast method.

let contrast;
let fg = new Color("display-p3" [1, 1, 0]); // P3 yellow
let bg = new Color("sienna"); // sRGB named color

let l1 = fg.get("xyz", "y");
let l2 = bg.get("xyz", "y");

if (l1 > l2) {
    [l1, l2] = [l2, l1];
}

contrast = (l2 + 0.05) / (l1 + 0.05);

Defining new color spaces

// TBD

Color spaces from ICC profiles

let cmyk = ColorSpace.fromICCProfile("./cmyk-profile.icc", {
	name: "fogra-coated",
	coords: {
		c: { min: 0, max: 100 },
		m: { min: 0, max: 100 },
		y: { min: 0, max: 100 },
		k: { min: 0, max: 100 },
	}
});
let magenta = new Color(cmyk, [0, 100, 0, 0]);
let lightMagenta = magenta.set("oklch", "l", l => l * 1.2);

Decisions

Can a color space become unregistered?

No, and this is by design. It complicates implementation if color spaces can "stop" being supported. What happens with all existing colors created?

Define colors over an extended range

Ths simplifies use of HDR, especially on platforms like WebGPU or WebGL which are not inherently color managed (all operatons happen in a single color space)

ICC Profiles

An earlier version of this draft had iccProfile as a property of ColorSpace objects. However, that would require the entire API to be async, which significantly complicates use cases. Therefore, it was deemed better to have an async ColorSpace.fromICCProfile() method that returns a regular ColorSpace object.

More Repositories

1

webcomponents

Web Components specifications
HTML
4,306
star
2

import-maps

How to control the behavior of JavaScript imports
JavaScript
2,636
star
3

virtual-scroller

1,997
star
4

focus-visible

Polyfill for `:focus-visible`
JavaScript
1,606
star
5

webusb

Connecting hardware to the web.
Bikeshed
1,287
star
6

webpackage

Web packaging format
Go
1,216
star
7

EventListenerOptions

An extension to the DOM event pattern to allow authors to disable support for preventDefault
JavaScript
1,166
star
8

portals

A proposal for enabling seamless navigations between sites or pages
HTML
945
star
9

floc

This proposal has been replaced by the Topics API.
Makefile
933
star
10

inert

Polyfill for the inert attribute and property.
JavaScript
914
star
11

scheduling-apis

APIs for scheduling and controlling prioritized tasks.
HTML
896
star
12

view-transitions

789
star
13

file-system-access

Expose the file system on the user’s device, so Web apps can interoperate with the user’s native applications.
Bikeshed
641
star
14

background-sync

A design and spec for ServiceWorker-based background synchronization
HTML
638
star
15

scroll-to-text-fragment

Proposal to allow specifying a text snippet in a URL fragment
HTML
577
star
16

ua-client-hints

Wouldn't it be nice if `User-Agent` was a (set of) client hints?
Bikeshed
575
star
17

aom

Accessibility Object Model
HTML
553
star
18

kv-storage

[On hold] A proposal for an async key/value storage API for the web
550
star
19

observable

Observable API proposal
Bikeshed
515
star
20

turtledove

TURTLEDOVE
Bikeshed
505
star
21

navigation-api

The new navigation API provides a new interface for navigations and session history, with a focus on single-page application navigations.
Makefile
474
star
22

webmonetization

Proposed Web Monetization standard
HTML
439
star
23

trust-token-api

Trust Token API
Bikeshed
412
star
24

attribution-reporting-api

Attribution Reporting API
Bikeshed
338
star
25

direct-sockets

Direct Sockets API for the web platform
HTML
304
star
26

shape-detection-api

Detection of shapes (faces, QR codes) in images
Bikeshed
299
star
27

display-locking

A repository for the Display Locking spec
HTML
294
star
28

background-fetch

API proposal for background downloading/uploading
Shell
279
star
29

resize-observer

This repository is no longer active. ResizeObserver has moved out of WICG into
HTML
256
star
30

first-party-sets

Bikeshed
255
star
31

serial

Serial ports API for the platform.
HTML
254
star
32

priority-hints

A browser API to enable developers signal the priorities of the resources they need to download.
Bikeshed
228
star
33

dbsc

HTML
227
star
34

is-input-pending

HTML
222
star
35

sanitizer-api

Bikeshed
213
star
36

proposals

A home for well-formed proposed incubations for the web platform. All proposals welcome.
209
star
37

spatial-navigation

Directional focus navigation with arrow keys
JavaScript
199
star
38

js-self-profiling

Proposal for a programmable JS profiling API for collecting JS profiles from real end-user environments
HTML
196
star
39

cq-usecases

Use cases and requirements for standardizing element queries.
HTML
185
star
40

interventions

A place for browsers and web developers to collaborate on user agent interventions.
178
star
41

visual-viewport

A proposal to add explicit APIs to the Web for querying and setting the visual viewport
HTML
174
star
42

frame-timing

Frame Timing API
HTML
170
star
43

layout-instability

A proposal for a Layout Instability specification
Makefile
157
star
44

page-lifecycle

Lifecycle API to support system initiated discarding and freezing
HTML
153
star
45

isolated-web-apps

Repository for explainers and other documents related to the Isolated Web Apps proposal.
Bikeshed
146
star
46

speech-api

Web Speech API
Bikeshed
144
star
47

cookie-store

Asynchronous access to cookies from JavaScript
Bikeshed
141
star
48

nav-speculation

Proposal to enable privacy-enhanced preloading
HTML
141
star
49

construct-stylesheets

API for constructing CSS stylesheet objects
Bikeshed
137
star
50

webhid

Web API for accessing Human Interface Devices (HID)
HTML
135
star
51

devtools-protocol

DevTools Protocol
JavaScript
120
star
52

fenced-frame

Proposal for a strong boundary between a page and its embedded content
Bikeshed
118
star
53

sms-one-time-codes

A way to format SMS messages for use with browser autofill features such as HTML’s autocomplete=one-time-code.
Makefile
109
star
54

bundle-preloading

Bundles of multiple resources, to improve loading JS and the Web.
HTML
103
star
55

netinfo

HTML
95
star
56

intrinsicsize-attribute

Proposal to add an intrinsicsize attribute to media elements
94
star
57

window-controls-overlay

HTML
94
star
58

container-queries

HTML
92
star
59

animation-worklet

🚫 Old repository for AnimationWorklet specification ➡️ New repository: https://github.com/w3c/css-houdini-drafts
Makefile
92
star
60

manifest-incubations

Before install prompt API for installing web applications
HTML
90
star
61

async-append

A way to create DOM and add it to the document without blocking the main thread.
HTML
87
star
62

privacy-preserving-ads

Privacy-Preserving Ads
86
star
63

indexed-db-observers

Prototyping and discussion around indexeddb observers.
WebIDL
83
star
64

shared-storage

Explainer for proposed web platform Shared Storage API
Bikeshed
82
star
65

compression

Standard text for CompressionStream and DecompressionStream API
HTML
81
star
66

file-handling

API for web applications to handle files
81
star
67

compression-dictionary-transport

80
star
68

canvas-color-space

Proposed web platform feature to add color management, wide gamut and high bit-depth support to the <canvas> element.
78
star
69

canvas-formatted-text

HTML
77
star
70

local-font-access

Web API for enumerating fonts on the local system
Bikeshed
75
star
71

performance-measure-memory

performance.measureMemory API
HTML
73
star
72

starter-kit

A simple starter kit for incubations
JavaScript
72
star
73

handwriting-recognition

Handwriting Recognition Web API Proposal
Makefile
72
star
74

css-parser-api

This is the repo where the CSS Houdini parser API will be worked on
HTML
72
star
75

ContentPerformancePolicy

A set of policies that a site guarantees to adhere to, browsers enforce, and embedders can count on.
HTML
72
star
76

web-app-launch

Web App Launch Handler
HTML
72
star
77

pwa-url-handler

71
star
78

eyedropper-api

HTML
70
star
79

idle-detection

A proposal for an idle detection and notification API for the web
Bikeshed
67
star
80

close-watcher

A web API proposal for watching for close requests (e.g. Esc, Android back button, ...)
Makefile
67
star
81

storage-foundation-api-explainer

Explainer showcasing a new web storage API, NativeIO
65
star
82

video-editing

64
star
83

uuid

UUID V4
63
star
84

client-hints-infrastructure

Specification for the Client Hints infrastructure - privacy preserving proactive content negotiation
Bikeshed
61
star
85

sparrow

59
star
86

element-timing

A proposal for an Element Timing specification.
Bikeshed
59
star
87

local-peer-to-peer

↔️ Proposal for local communication between browsers without the aid of a server.
Bikeshed
53
star
88

digital-credentials

Digital Credentials, like driver's licenses
HTML
53
star
89

video-rvfc

video.requestVideoFrameCallback() incubation
HTML
53
star
90

time-to-interactive

Repository for hosting TTI specification and discussions around it.
52
star
91

digital-goods

Makefile
49
star
92

private-network-access

HTML
49
star
93

raw-clipboard-access

An explainer for the Raw Clipboard Access feature
45
star
94

document-picture-in-picture

Bikeshed
45
star
95

admin

👋 Ask your questions here! 👋
HTML
42
star
96

soft-navigations

Heuristics to detect Single Page Apps soft navigations
Bikeshed
42
star
97

pending-beacon

A better beaconing API
Bikeshed
40
star
98

webcrypto-secure-curves

Proposal for the addition of Curve25519 and Curve448 to the Web Cryptography API
HTML
40
star
99

entries-api

Spec defining browser support for file/directory upload by drag-and-drop
Bikeshed
40
star
100

transfer-size

38
star