@zag-js/color-utils 0.9.2 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/color.ts +44 -0
- package/src/hsb-color.ts +120 -0
- package/src/hsl-color.ts +115 -0
- package/src/index.ts +3 -0
- package/src/parse-color.ts +20 -0
- package/src/rgb-color.ts +203 -0
- package/src/types.ts +59 -0
- package/src/utils.ts +11 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/color-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.1",
|
|
4
4
|
"description": "Color utilities for zag.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
"repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/color-utils",
|
|
15
15
|
"sideEffects": false,
|
|
16
16
|
"files": [
|
|
17
|
-
"dist
|
|
17
|
+
"dist",
|
|
18
|
+
"src"
|
|
18
19
|
],
|
|
19
20
|
"publishConfig": {
|
|
20
21
|
"access": "public"
|
package/src/color.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ColorType, ColorFormat, ColorChannel, ColorChannelRange, ColorAxes } from "./types"
|
|
2
|
+
|
|
3
|
+
export abstract class Color implements ColorType {
|
|
4
|
+
abstract toFormat(format: ColorFormat): ColorType
|
|
5
|
+
abstract toString(format: ColorFormat | "css"): string
|
|
6
|
+
abstract clone(): ColorType
|
|
7
|
+
abstract getChannelRange(channel: ColorChannel): ColorChannelRange
|
|
8
|
+
abstract getColorSpace(): ColorFormat
|
|
9
|
+
abstract getColorChannels(): [ColorChannel, ColorChannel, ColorChannel]
|
|
10
|
+
|
|
11
|
+
toHexInt(): number {
|
|
12
|
+
return this.toFormat("rgb").toHexInt()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getChannelValue(channel: ColorChannel): number {
|
|
16
|
+
if (channel in this) {
|
|
17
|
+
return this[channel]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
throw new Error("Unsupported color channel: " + channel)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
withChannelValue(channel: ColorChannel, value: number): ColorType {
|
|
24
|
+
if (channel in this) {
|
|
25
|
+
let clone = this.clone()
|
|
26
|
+
clone[channel] = value
|
|
27
|
+
return clone
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
throw new Error("Unsupported color channel: " + channel)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
getColorSpaceAxes(xyChannels: { xChannel?: ColorChannel; yChannel?: ColorChannel }): ColorAxes {
|
|
34
|
+
let { xChannel, yChannel } = xyChannels
|
|
35
|
+
let xCh = xChannel || this.getColorChannels().find((c) => c !== yChannel)
|
|
36
|
+
let yCh = yChannel || this.getColorChannels().find((c) => c !== xCh)
|
|
37
|
+
let zCh = this.getColorChannels().find((c) => c !== xCh && c !== yCh)
|
|
38
|
+
return { xChannel: xCh!, yChannel: yCh!, zChannel: zCh! }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
isEqual(color: ColorType): boolean {
|
|
42
|
+
return this.toHexInt() === color.toHexInt()
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/hsb-color.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Color } from "./color"
|
|
2
|
+
import { HSLColor } from "./hsl-color"
|
|
3
|
+
import { RGBColor } from "./rgb-color"
|
|
4
|
+
import { ColorChannel, ColorChannelRange, ColorFormat, ColorType } from "./types"
|
|
5
|
+
import { clampValue, mod, toFixedNumber } from "./utils"
|
|
6
|
+
|
|
7
|
+
const HSB_REGEX =
|
|
8
|
+
/hsb\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%)\)|hsba\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d(.\d+)?)\)/
|
|
9
|
+
|
|
10
|
+
export class HSBColor extends Color {
|
|
11
|
+
constructor(private hue: number, private saturation: number, private brightness: number, private alpha: number) {
|
|
12
|
+
super()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static parse(value: string): HSBColor | void {
|
|
16
|
+
let m: RegExpMatchArray | null
|
|
17
|
+
if ((m = value.match(HSB_REGEX))) {
|
|
18
|
+
const [h, s, b, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")))
|
|
19
|
+
return new HSBColor(mod(h, 360), clampValue(s, 0, 100), clampValue(b, 0, 100), clampValue(a ?? 1, 0, 1))
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
toString(format: ColorFormat | "css") {
|
|
24
|
+
switch (format) {
|
|
25
|
+
case "css":
|
|
26
|
+
return this.toHSL().toString("css")
|
|
27
|
+
case "hex":
|
|
28
|
+
return this.toRGB().toString("hex")
|
|
29
|
+
case "hexa":
|
|
30
|
+
return this.toRGB().toString("hexa")
|
|
31
|
+
case "hsb":
|
|
32
|
+
return `hsb(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%)`
|
|
33
|
+
case "hsba":
|
|
34
|
+
return `hsba(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%, ${
|
|
35
|
+
this.alpha
|
|
36
|
+
})`
|
|
37
|
+
default:
|
|
38
|
+
return this.toFormat(format).toString(format)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
toFormat(format: ColorFormat): ColorType {
|
|
43
|
+
switch (format) {
|
|
44
|
+
case "hsb":
|
|
45
|
+
case "hsba":
|
|
46
|
+
return this
|
|
47
|
+
case "hsl":
|
|
48
|
+
case "hsla":
|
|
49
|
+
return this.toHSL()
|
|
50
|
+
case "rgb":
|
|
51
|
+
case "rgba":
|
|
52
|
+
return this.toRGB()
|
|
53
|
+
default:
|
|
54
|
+
throw new Error("Unsupported color conversion: hsb -> " + format)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Converts a HSB color to HSL.
|
|
60
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
|
|
61
|
+
* @returns An HSLColor object.
|
|
62
|
+
*/
|
|
63
|
+
private toHSL(): ColorType {
|
|
64
|
+
let saturation = this.saturation / 100
|
|
65
|
+
let brightness = this.brightness / 100
|
|
66
|
+
let lightness = brightness * (1 - saturation / 2)
|
|
67
|
+
saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness)
|
|
68
|
+
|
|
69
|
+
return new HSLColor(
|
|
70
|
+
toFixedNumber(this.hue, 2),
|
|
71
|
+
toFixedNumber(saturation * 100, 2),
|
|
72
|
+
toFixedNumber(lightness * 100, 2),
|
|
73
|
+
this.alpha,
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Converts a HSV color value to RGB.
|
|
79
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
|
|
80
|
+
* @returns An RGBColor object.
|
|
81
|
+
*/
|
|
82
|
+
private toRGB(): ColorType {
|
|
83
|
+
let hue = this.hue
|
|
84
|
+
let saturation = this.saturation / 100
|
|
85
|
+
let brightness = this.brightness / 100
|
|
86
|
+
|
|
87
|
+
let fn = (n: number, k = (n + hue / 60) % 6) =>
|
|
88
|
+
brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0)
|
|
89
|
+
|
|
90
|
+
return new RGBColor(Math.round(fn(5) * 255), Math.round(fn(3) * 255), Math.round(fn(1) * 255), this.alpha)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
clone(): ColorType {
|
|
94
|
+
return new HSBColor(this.hue, this.saturation, this.brightness, this.alpha)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange {
|
|
98
|
+
switch (channel) {
|
|
99
|
+
case "hue":
|
|
100
|
+
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 }
|
|
101
|
+
case "saturation":
|
|
102
|
+
case "brightness":
|
|
103
|
+
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 }
|
|
104
|
+
case "alpha":
|
|
105
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 }
|
|
106
|
+
default:
|
|
107
|
+
throw new Error("Unknown color channel: " + channel)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
getColorSpace(): ColorFormat {
|
|
112
|
+
return "hsb"
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = ["hue", "saturation", "brightness"]
|
|
116
|
+
|
|
117
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
|
|
118
|
+
return HSBColor.colorChannels
|
|
119
|
+
}
|
|
120
|
+
}
|
package/src/hsl-color.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Color } from "./color"
|
|
2
|
+
import { HSBColor } from "./hsb-color"
|
|
3
|
+
import { RGBColor } from "./rgb-color"
|
|
4
|
+
import { ColorChannel, ColorChannelRange, ColorFormat, ColorType } from "./types"
|
|
5
|
+
import { clampValue, mod, toFixedNumber } from "./utils"
|
|
6
|
+
|
|
7
|
+
export const HSL_REGEX =
|
|
8
|
+
/hsl\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%)\)|hsla\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d(.\d+)?)\)/
|
|
9
|
+
|
|
10
|
+
export class HSLColor extends Color {
|
|
11
|
+
constructor(private hue: number, private saturation: number, private lightness: number, private alpha: number) {
|
|
12
|
+
super()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static parse(value: string): HSLColor | void {
|
|
16
|
+
let m: RegExpMatchArray | null
|
|
17
|
+
if ((m = value.match(HSL_REGEX))) {
|
|
18
|
+
const [h, s, l, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")))
|
|
19
|
+
return new HSLColor(mod(h, 360), clampValue(s, 0, 100), clampValue(l, 0, 100), clampValue(a ?? 1, 0, 1))
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
toString(format: ColorFormat | "css") {
|
|
24
|
+
switch (format) {
|
|
25
|
+
case "hex":
|
|
26
|
+
return this.toRGB().toString("hex")
|
|
27
|
+
case "hexa":
|
|
28
|
+
return this.toRGB().toString("hexa")
|
|
29
|
+
case "hsl":
|
|
30
|
+
return `hsl(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%)`
|
|
31
|
+
case "css":
|
|
32
|
+
case "hsla":
|
|
33
|
+
return `hsla(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%, ${
|
|
34
|
+
this.alpha
|
|
35
|
+
})`
|
|
36
|
+
default:
|
|
37
|
+
return this.toFormat(format).toString(format)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
toFormat(format: ColorFormat): ColorType {
|
|
41
|
+
switch (format) {
|
|
42
|
+
case "hsl":
|
|
43
|
+
case "hsla":
|
|
44
|
+
return this
|
|
45
|
+
case "hsb":
|
|
46
|
+
case "hsba":
|
|
47
|
+
return this.toHSB()
|
|
48
|
+
case "rgb":
|
|
49
|
+
case "rgba":
|
|
50
|
+
return this.toRGB()
|
|
51
|
+
default:
|
|
52
|
+
throw new Error("Unsupported color conversion: hsl -> " + format)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Converts a HSL color to HSB.
|
|
58
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
|
|
59
|
+
* @returns An HSBColor object.
|
|
60
|
+
*/
|
|
61
|
+
private toHSB(): ColorType {
|
|
62
|
+
let saturation = this.saturation / 100
|
|
63
|
+
let lightness = this.lightness / 100
|
|
64
|
+
let brightness = lightness + saturation * Math.min(lightness, 1 - lightness)
|
|
65
|
+
saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness)
|
|
66
|
+
return new HSBColor(
|
|
67
|
+
toFixedNumber(this.hue, 2),
|
|
68
|
+
toFixedNumber(saturation * 100, 2),
|
|
69
|
+
toFixedNumber(brightness * 100, 2),
|
|
70
|
+
this.alpha,
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Converts a HSL color to RGB.
|
|
76
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
|
|
77
|
+
* @returns An RGBColor object.
|
|
78
|
+
*/
|
|
79
|
+
private toRGB(): ColorType {
|
|
80
|
+
let hue = this.hue
|
|
81
|
+
let saturation = this.saturation / 100
|
|
82
|
+
let lightness = this.lightness / 100
|
|
83
|
+
let a = saturation * Math.min(lightness, 1 - lightness)
|
|
84
|
+
let fn = (n: number, k = (n + hue / 30) % 12) => lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1)
|
|
85
|
+
return new RGBColor(Math.round(fn(0) * 255), Math.round(fn(8) * 255), Math.round(fn(4) * 255), this.alpha)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
clone(): ColorType {
|
|
89
|
+
return new HSLColor(this.hue, this.saturation, this.lightness, this.alpha)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange {
|
|
93
|
+
switch (channel) {
|
|
94
|
+
case "hue":
|
|
95
|
+
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 }
|
|
96
|
+
case "saturation":
|
|
97
|
+
case "lightness":
|
|
98
|
+
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 }
|
|
99
|
+
case "alpha":
|
|
100
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 }
|
|
101
|
+
default:
|
|
102
|
+
throw new Error("Unknown color channel: " + channel)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
getColorSpace(): ColorFormat {
|
|
107
|
+
return "hsl"
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = ["hue", "saturation", "lightness"]
|
|
111
|
+
|
|
112
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
|
|
113
|
+
return HSLColor.colorChannels
|
|
114
|
+
}
|
|
115
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { HSBColor } from "./hsb-color"
|
|
2
|
+
import { HSLColor } from "./hsl-color"
|
|
3
|
+
import { RGBColor } from "./rgb-color"
|
|
4
|
+
import { ColorType } from "./types"
|
|
5
|
+
|
|
6
|
+
export function parseColor(value: string): ColorType {
|
|
7
|
+
let result = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value)
|
|
8
|
+
if (!result) {
|
|
9
|
+
throw new Error("Invalid color value: " + value)
|
|
10
|
+
}
|
|
11
|
+
return result
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function normalizeColor(v: string | ColorType) {
|
|
15
|
+
if (typeof v === "string") {
|
|
16
|
+
return parseColor(v)
|
|
17
|
+
} else {
|
|
18
|
+
return v
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/rgb-color.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { Color } from "./color"
|
|
2
|
+
import { HSBColor } from "./hsb-color"
|
|
3
|
+
import { HSLColor } from "./hsl-color"
|
|
4
|
+
import { ColorChannel, ColorChannelRange, ColorFormat, ColorType } from "./types"
|
|
5
|
+
import { clampValue, toFixedNumber } from "./utils"
|
|
6
|
+
|
|
7
|
+
export class RGBColor extends Color {
|
|
8
|
+
constructor(private red: number, private green: number, private blue: number, private alpha: number) {
|
|
9
|
+
super()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
static parse(value: string) {
|
|
13
|
+
let colors: (number | undefined)[] = []
|
|
14
|
+
|
|
15
|
+
// matching #rgb, #rgba, #rrggbb, #rrggbbaa
|
|
16
|
+
if (/^#[\da-f]+$/i.test(value) && [4, 5, 7, 9].includes(value.length)) {
|
|
17
|
+
const values = (value.length < 6 ? value.replace(/[^#]/gi, "$&$&") : value).slice(1).split("")
|
|
18
|
+
while (values.length > 0) {
|
|
19
|
+
colors.push(parseInt(values.splice(0, 2).join(""), 16))
|
|
20
|
+
}
|
|
21
|
+
colors[3] = colors[3] !== undefined ? colors[3] / 255 : undefined
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// matching rgb(rrr, ggg, bbb), rgba(rrr, ggg, bbb, 0.a)
|
|
25
|
+
const match = value.match(/^rgba?\((.*)\)$/)
|
|
26
|
+
|
|
27
|
+
if (match?.[1]) {
|
|
28
|
+
colors = match[1]
|
|
29
|
+
.split(",")
|
|
30
|
+
.map((value) => Number(value.trim()))
|
|
31
|
+
.map((num, i) => clampValue(num, 0, i < 3 ? 255 : 1))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//@ts-expect-error
|
|
35
|
+
return colors.length < 3 ? undefined : new RGBColor(colors[0], colors[1], colors[2], colors[3] ?? 1)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
toString(format: ColorFormat | "css") {
|
|
39
|
+
switch (format) {
|
|
40
|
+
case "hex":
|
|
41
|
+
return (
|
|
42
|
+
"#" +
|
|
43
|
+
(
|
|
44
|
+
this.red.toString(16).padStart(2, "0") +
|
|
45
|
+
this.green.toString(16).padStart(2, "0") +
|
|
46
|
+
this.blue.toString(16).padStart(2, "0")
|
|
47
|
+
).toUpperCase()
|
|
48
|
+
)
|
|
49
|
+
case "hexa":
|
|
50
|
+
return (
|
|
51
|
+
"#" +
|
|
52
|
+
(
|
|
53
|
+
this.red.toString(16).padStart(2, "0") +
|
|
54
|
+
this.green.toString(16).padStart(2, "0") +
|
|
55
|
+
this.blue.toString(16).padStart(2, "0") +
|
|
56
|
+
Math.round(this.alpha * 255)
|
|
57
|
+
.toString(16)
|
|
58
|
+
.padStart(2, "0")
|
|
59
|
+
).toUpperCase()
|
|
60
|
+
)
|
|
61
|
+
case "rgb":
|
|
62
|
+
return `rgb(${this.red}, ${this.green}, ${this.blue})`
|
|
63
|
+
case "css":
|
|
64
|
+
case "rgba":
|
|
65
|
+
return `rgba(${this.red}, ${this.green}, ${this.blue}, ${this.alpha})`
|
|
66
|
+
default:
|
|
67
|
+
return this.toFormat(format).toString(format)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
toFormat(format: ColorFormat): ColorType {
|
|
72
|
+
switch (format) {
|
|
73
|
+
case "hex":
|
|
74
|
+
case "hexa":
|
|
75
|
+
case "rgb":
|
|
76
|
+
case "rgba":
|
|
77
|
+
return this
|
|
78
|
+
case "hsb":
|
|
79
|
+
case "hsba":
|
|
80
|
+
return this.toHSB()
|
|
81
|
+
case "hsl":
|
|
82
|
+
case "hsla":
|
|
83
|
+
return this.toHSL()
|
|
84
|
+
default:
|
|
85
|
+
throw new Error("Unsupported color conversion: rgb -> " + format)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
toHexInt(): number {
|
|
90
|
+
return (this.red << 16) | (this.green << 8) | this.blue
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Converts an RGB color value to HSB.
|
|
95
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
96
|
+
* @returns An HSBColor object.
|
|
97
|
+
*/
|
|
98
|
+
private toHSB(): ColorType {
|
|
99
|
+
const red = this.red / 255
|
|
100
|
+
const green = this.green / 255
|
|
101
|
+
const blue = this.blue / 255
|
|
102
|
+
const min = Math.min(red, green, blue)
|
|
103
|
+
const brightness = Math.max(red, green, blue)
|
|
104
|
+
const chroma = brightness - min
|
|
105
|
+
const saturation = brightness === 0 ? 0 : chroma / brightness
|
|
106
|
+
let hue = 0 // achromatic
|
|
107
|
+
|
|
108
|
+
if (chroma !== 0) {
|
|
109
|
+
switch (brightness) {
|
|
110
|
+
case red:
|
|
111
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0)
|
|
112
|
+
break
|
|
113
|
+
case green:
|
|
114
|
+
hue = (blue - red) / chroma + 2
|
|
115
|
+
break
|
|
116
|
+
case blue:
|
|
117
|
+
hue = (red - green) / chroma + 4
|
|
118
|
+
break
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
hue /= 6
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return new HSBColor(
|
|
125
|
+
toFixedNumber(hue * 360, 2),
|
|
126
|
+
toFixedNumber(saturation * 100, 2),
|
|
127
|
+
toFixedNumber(brightness * 100, 2),
|
|
128
|
+
this.alpha,
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Converts an RGB color value to HSL.
|
|
134
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
135
|
+
* @returns An HSLColor object.
|
|
136
|
+
*/
|
|
137
|
+
private toHSL(): ColorType {
|
|
138
|
+
const red = this.red / 255
|
|
139
|
+
const green = this.green / 255
|
|
140
|
+
const blue = this.blue / 255
|
|
141
|
+
const min = Math.min(red, green, blue)
|
|
142
|
+
const max = Math.max(red, green, blue)
|
|
143
|
+
const lightness = (max + min) / 2
|
|
144
|
+
const chroma = max - min
|
|
145
|
+
|
|
146
|
+
let hue = -1
|
|
147
|
+
let saturation = -1
|
|
148
|
+
|
|
149
|
+
if (chroma === 0) {
|
|
150
|
+
hue = saturation = 0 // achromatic
|
|
151
|
+
} else {
|
|
152
|
+
saturation = chroma / (lightness < 0.5 ? max + min : 2 - max - min)
|
|
153
|
+
|
|
154
|
+
switch (max) {
|
|
155
|
+
case red:
|
|
156
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0)
|
|
157
|
+
break
|
|
158
|
+
case green:
|
|
159
|
+
hue = (blue - red) / chroma + 2
|
|
160
|
+
break
|
|
161
|
+
case blue:
|
|
162
|
+
hue = (red - green) / chroma + 4
|
|
163
|
+
break
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
hue /= 6
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return new HSLColor(
|
|
170
|
+
toFixedNumber(hue * 360, 2),
|
|
171
|
+
toFixedNumber(saturation * 100, 2),
|
|
172
|
+
toFixedNumber(lightness * 100, 2),
|
|
173
|
+
this.alpha,
|
|
174
|
+
)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
clone(): ColorType {
|
|
178
|
+
return new RGBColor(this.red, this.green, this.blue, this.alpha)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange {
|
|
182
|
+
switch (channel) {
|
|
183
|
+
case "red":
|
|
184
|
+
case "green":
|
|
185
|
+
case "blue":
|
|
186
|
+
return { minValue: 0x0, maxValue: 0xff, step: 0x1, pageSize: 0x11 }
|
|
187
|
+
case "alpha":
|
|
188
|
+
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 }
|
|
189
|
+
default:
|
|
190
|
+
throw new Error("Unknown color channel: " + channel)
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
getColorSpace(): ColorFormat {
|
|
195
|
+
return "rgb"
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private static colorChannels: [ColorChannel, ColorChannel, ColorChannel] = ["red", "green", "blue"]
|
|
199
|
+
|
|
200
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel] {
|
|
201
|
+
return RGBColor.colorChannels
|
|
202
|
+
}
|
|
203
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export type ColorFormat = "hex" | "hexa" | "rgb" | "rgba" | "hsl" | "hsla" | "hsb" | "hsba"
|
|
2
|
+
|
|
3
|
+
export type ColorChannel = "hue" | "saturation" | "brightness" | "lightness" | "red" | "green" | "blue" | "alpha"
|
|
4
|
+
|
|
5
|
+
export type ColorAxes = { xChannel: ColorChannel; yChannel: ColorChannel; zChannel: ColorChannel }
|
|
6
|
+
|
|
7
|
+
export interface ColorChannelRange {
|
|
8
|
+
/** The minimum value of the color channel. */
|
|
9
|
+
minValue: number
|
|
10
|
+
/** The maximum value of the color channel. */
|
|
11
|
+
maxValue: number
|
|
12
|
+
/** The step value of the color channel, used when incrementing and decrementing. */
|
|
13
|
+
step: number
|
|
14
|
+
/** The page step value of the color channel, used when incrementing and decrementing. */
|
|
15
|
+
pageSize: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ColorType {
|
|
19
|
+
/** Converts the color to the given color format, and returns a new Color object. */
|
|
20
|
+
toFormat(format: ColorFormat): ColorType
|
|
21
|
+
/** Converts the color to a string in the given format. */
|
|
22
|
+
toString(format: ColorFormat | "css"): string
|
|
23
|
+
/** Converts the color to hex, and returns an integer representation. */
|
|
24
|
+
toHexInt(): number
|
|
25
|
+
/**
|
|
26
|
+
* Returns the numeric value for a given channel.
|
|
27
|
+
* Throws an error if the channel is unsupported in the current color format.
|
|
28
|
+
*/
|
|
29
|
+
getChannelValue(channel: ColorChannel): number
|
|
30
|
+
/**
|
|
31
|
+
* Sets the numeric value for a given channel, and returns a new Color object.
|
|
32
|
+
* Throws an error if the channel is unsupported in the current color format.
|
|
33
|
+
*/
|
|
34
|
+
withChannelValue(channel: ColorChannel, value: number): ColorType
|
|
35
|
+
/**
|
|
36
|
+
* Returns the minimum, maximum, and step values for a given channel.
|
|
37
|
+
*/
|
|
38
|
+
getChannelRange(channel: ColorChannel): ColorChannelRange
|
|
39
|
+
/**
|
|
40
|
+
* Returns the color space, 'rgb', 'hsb' or 'hsl', for the current color.
|
|
41
|
+
*/
|
|
42
|
+
getColorSpace(): ColorFormat
|
|
43
|
+
/**
|
|
44
|
+
* Returns the color space axes, xChannel, yChannel, zChannel.
|
|
45
|
+
*/
|
|
46
|
+
getColorSpaceAxes(xyChannels: { xChannel?: ColorChannel; yChannel?: ColorChannel }): ColorAxes
|
|
47
|
+
/**
|
|
48
|
+
* Returns an array of the color channels within the current color space space.
|
|
49
|
+
*/
|
|
50
|
+
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel]
|
|
51
|
+
/**
|
|
52
|
+
* Returns a new Color object with the same values as the current color.
|
|
53
|
+
*/
|
|
54
|
+
clone(): ColorType
|
|
55
|
+
/**
|
|
56
|
+
* Whether the color is equal to another color.
|
|
57
|
+
*/
|
|
58
|
+
isEqual(color: ColorType): boolean
|
|
59
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function mod(n: number, m: number) {
|
|
2
|
+
return ((n % m) + m) % m
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function toFixedNumber(num: number, digits: number) {
|
|
6
|
+
return Math.round(Math.pow(10, digits) * num) / Math.pow(10, digits)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function clampValue(value: number, min: number, max: number) {
|
|
10
|
+
return Math.min(Math.max(value, min), max)
|
|
11
|
+
}
|