-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor Color to its own namespace, add fromJson
- Loading branch information
Showing
8 changed files
with
42 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Json } from "./Json.js" | ||
|
||
export type Color = [number, number, number] | ||
|
||
export namespace Color { | ||
export function fromJson(obj: unknown): Color | undefined { | ||
const packed = Json.readNumber(obj) | ||
if (packed) return intToRgb(packed) | ||
const array = Json.readArray(obj, o => Json.readNumber(o) ?? 0) | ||
if (array === undefined || array.length !== 3) return undefined | ||
return array as [number, number, number] | ||
} | ||
|
||
export function intToRgb(n: number): Color { | ||
const r = (n >> 16) & 255 | ||
const g = (n >> 8) & 255 | ||
const b = n & 255 | ||
return [r / 255, g / 255, b / 255] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './Color.js' | ||
export * from './Json.js' | ||
export * from './StringReader.js' | ||
export * from './Util.js' | ||
|