Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
fix err
Browse files Browse the repository at this point in the history
  • Loading branch information
slmjkdbtl committed Oct 10, 2023
1 parent cdfd199 commit 1ad32c1
Showing 1 changed file with 45 additions and 45 deletions.
90 changes: 45 additions & 45 deletions src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,31 @@ export class Color {
}
}

// TODO: use range of [0, 360] [0, 100] [0, 100]?
static fromHSL(h: number, s: number, l: number) {
return Color.fromArray(hsl2rgb(h, s, l))

if (s == 0){
return new Color(255 * l, 255 * l, 255 * l)
}

const hue2rgb = (p, q, t) => {
if (t < 0) t += 1
if (t > 1) t -= 1
if (t < 1 / 6) return p + (q - p) * 6 * t
if (t < 1 / 2) return q
if (t < 2 / 3) return p + (q - p) * (2/3 - t) * 6
return p
}

const q = l < 0.5 ? l * (1 + s) : l + s - l * s
const p = 2 * l - q
const r = hue2rgb(p, q, h + 1 / 3)
const g = hue2rgb(p, q, h)
const b = hue2rgb(p, q, h - 1 / 3)

return new Color(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255))


}

static RED = new Color(255, 0, 0)
Expand Down Expand Up @@ -258,7 +281,26 @@ export class Color {
}

toHSL(): [number, number, number] {
return rgb2hsl(this.r, this.g, this.b)
const r = this.r / 255
const g = this.g / 255
const b = this.b / 255
const max = Math.max(r, g, b), min = Math.min(r, g, b)
let h = (max + min) / 2
let s = h
const l = h
if (max == min) {
h = s = 0
} else {
const d = max - min
s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break
case g: h = (b - r) / d + 2; break
case b: h = (r - g) / d + 4; break
}
h /= 6
}
return [ h, s, l ]
}

eq(other: Color): boolean {
Expand All @@ -280,8 +322,6 @@ export class Color {

export function rgb(...args): Color {
if (args.length === 0) {
return new Color(255, 255, 255)
} else if (args.length === 1) {
if (args[0] instanceof Color) {
return args[0].clone()
} else if (typeof args[0] === "string") {
Expand All @@ -294,47 +334,7 @@ export function rgb(...args): Color {
return new Color(...args)
}

export const hsl2rgb = (h, s, l) => {
if (s == 0) return [255 * l, 255 * l, 255 * l]
const hue2rgb = (p, q, t) => {
if (t < 0) t += 1
if (t > 1) t -= 1
if (t < 1 / 6) return p + (q - p) * 6 * t
if (t < 1 / 2) return q
if (t < 2 / 3) return p + (q - p) * (2/3 - t) * 6
return p
}
const q = l < 0.5 ? l * (1 + s) : l + s - l * s
const p = 2 * l - q
const r = hue2rgb(p, q, h + 1 / 3)
const g = hue2rgb(p, q, h)
const b = hue2rgb(p, q, h - 1 / 3)
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)]
}

// TODO: use range of [0, 360] [0, 100] [0, 100]?
export const rgb2hsl = (r, g, b): [number, number, number] => {
r /= 255
g /= 255
b /= 255
const max = Math.max(r, g, b), min = Math.min(r, g, b)
let h = (max + min) / 2
let s = h
const l = h
if (max == min) {
h = s = 0
} else {
const d = max - min
s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break
case g: h = (b - r) / d + 2; break
case b: h = (r - g) / d + 4; break
}
h /= 6
}
return [ h, s, l ]
}
export const hsl2rgb = (h, s, l) => Color.fromHSL(h, s, l)

export class Quad {
x: number = 0
Expand Down

0 comments on commit 1ad32c1

Please sign in to comment.