-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* コンストラクタで不正な値を丸める * chroma-jsのserialize実装 * vitestのconfigにaliasがついてなかったのを修正 * superclassのコンストラクタで代入しているのになぜか子クラスでも代入してた * 循環参照が発生するのでdeserializer.tsに移動 * d3-chromaticのserialize/deserialize処理追加 * 色計算時にp5に依存しないようにしたいので名前を変える * p5jsの実装を排除できた * p5Instanceを渡す必要はなくなった * 特にexportする必要なかった * OthersPaletteにserialize/deserialize実装 * copilot君に任せたらsafeParseIntを使ってくれていなかった * テストの実装
- Loading branch information
Showing
12 changed files
with
403 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { Hsv, convertHsvToRgb, samples } from "culori"; | ||
import { | ||
BasePalette, | ||
Palette, | ||
RGB, | ||
buildRGB32Byte, | ||
clampedPaletteParams, | ||
} from "."; | ||
import { safeParseInt } from "@/math"; | ||
|
||
type OthersInterpolator = (t: number) => Hsv; | ||
|
||
const interpolators: Record<string, OthersInterpolator> = { | ||
hue360: (t) => { | ||
// hue 0~360 | ||
const hue = Math.floor(t * 360); | ||
return { mode: "hsv", h: hue, s: 0.75, v: 1 }; | ||
}, | ||
monochrome: (t) => { | ||
// monochrome | ||
const brightness = t * 0.8 + 0.2; | ||
return { mode: "hsv", s: 0, v: brightness }; | ||
}, | ||
fire: (t) => { | ||
// fire | ||
const brightness = t * 0.7 + 0.3; | ||
const hue = Math.floor(t * 90) - 30; | ||
return { mode: "hsv", h: hue, s: 0.9, v: brightness }; | ||
}, | ||
}; | ||
|
||
const getInterpolatorFromName = (name: string): ((t: number) => Hsv) => { | ||
const interpolator = interpolators[name]; | ||
return interpolator ?? interpolators.hue360; | ||
}; | ||
|
||
const getInterpolatorName = (interpolator: OthersInterpolator): string => { | ||
for (const [name, func] of Object.entries(interpolators)) { | ||
if (func === interpolator) { | ||
return name; | ||
} | ||
} | ||
return "hue360"; | ||
}; | ||
|
||
export class OthersPalette extends BasePalette { | ||
private interpolator: (t: number) => Hsv; | ||
colors: Hsv[] = []; | ||
|
||
constructor( | ||
length: number, | ||
interpolator: (t: number) => Hsv, | ||
mirrored = true, | ||
offset = 0, | ||
) { | ||
const { colorLength, offsetIndex } = clampedPaletteParams(length, offset); | ||
|
||
super(colorLength, mirrored, offsetIndex); | ||
|
||
this.interpolator = interpolator; | ||
|
||
this.buildColors(); | ||
} | ||
|
||
buildColors(): void { | ||
this.colors = samples(this.colorLength) | ||
.map((t) => this.interpolator(t)) | ||
.filter((v): v is NonNullable<typeof v> => v != null); | ||
} | ||
|
||
getRGBFromColorIndex(index: number): RGB { | ||
return buildRGB32Byte(convertHsvToRgb(this.colors[index])); | ||
} | ||
|
||
serialize(): string { | ||
const result = ["others"]; | ||
result.push(getInterpolatorName(this.interpolator)); | ||
result.push(`${this.mirrored ? 1 : 0}`); | ||
result.push(`${this.colorLength}`); | ||
result.push(`${this.offsetIndex}`); | ||
|
||
return result.join(","); | ||
} | ||
|
||
static deserialize(serialized: string): OthersPalette { | ||
const [, rawInterpolate, rawMirrored, rawLength, rawOffset] = | ||
serialized.split(","); | ||
|
||
const length = safeParseInt(rawLength); | ||
const offset = safeParseInt(rawOffset); | ||
const mirrored = rawMirrored === "1"; | ||
|
||
const interpolator = getInterpolatorFromName(rawInterpolate); | ||
|
||
return new OthersPalette(length, interpolator, mirrored, offset); | ||
} | ||
} | ||
|
||
export const othersPalettes = [ | ||
new OthersPalette(128, interpolators.hue360), | ||
new OthersPalette(128, interpolators.monochrome), | ||
new OthersPalette(128, interpolators.fire), | ||
] satisfies Palette[]; |
This file was deleted.
Oops, something went wrong.
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,19 @@ | ||
import { Palette } from "."; | ||
import { ChromaJsPalette } from "./color-chromajs"; | ||
import { D3ChromaticPalette } from "./color-d3-chromatic"; | ||
import { OthersPalette } from "./color-others"; | ||
|
||
export const deserializePalette = (serialized: string): Palette => { | ||
const [type] = serialized.split(","); | ||
|
||
switch (type) { | ||
case "chroma-js": | ||
return ChromaJsPalette.deserialize(serialized); | ||
case "d3-chromatic": | ||
return D3ChromaticPalette.deserialize(serialized); | ||
case "others": | ||
return OthersPalette.deserialize(serialized); | ||
default: | ||
throw new Error(`Unknown palette type: ${type}`); | ||
} | ||
}; |
Oops, something went wrong.