mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-17 07:04:36 +00:00
add multiple color schemes
This commit is contained in:
1 parent
1c40cf6eae
commit
d018e946a5
17 files changed
+12991
-8
No files matched your search
+28
-1
@@ -14,11 +14,31 @@ import { Spectrogram } from './modes/speak/extras/Spectrogram';
|
||||
|
||||
interface Props {}
|
||||
|
||||
enum SpectrogramMode {
|
||||
VIRIDIS,
|
||||
CIVIDIS,
|
||||
PLASMA,
|
||||
INFERNO,
|
||||
MAGMA,
|
||||
GIST_HEAT,
|
||||
AFMHOT,
|
||||
PINK,
|
||||
BLACK_WHITE,
|
||||
BONE,
|
||||
COPPER,
|
||||
JET,
|
||||
RDBU,
|
||||
RDGY,
|
||||
SPRING,
|
||||
COOL,
|
||||
}
|
||||
|
||||
interface State {
|
||||
mode: Mode,
|
||||
extrasMode: ExtrasMode,
|
||||
speaker: Speaker,
|
||||
currentSpectrogram?: Spectrogram,
|
||||
spectrogramMode: SpectrogramMode,
|
||||
}
|
||||
|
||||
class App extends React.Component<Props, State> {
|
||||
@@ -29,6 +49,7 @@ class App extends React.Component<Props, State> {
|
||||
mode: Mode.SPEAK_MODE,
|
||||
extrasMode: ExtrasMode.SPEAKER_INFO,
|
||||
speaker: SPEAKERS[0],
|
||||
spectrogramMode: SpectrogramMode.VIRIDIS,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -72,6 +93,10 @@ class App extends React.Component<Props, State> {
|
||||
});
|
||||
}
|
||||
|
||||
setSpectrogramMode = (spectrogramMode: SpectrogramMode) : void => {
|
||||
this.setState({ spectrogramMode: spectrogramMode });
|
||||
}
|
||||
|
||||
public render() {
|
||||
let component;
|
||||
switch (this.state.mode) {
|
||||
@@ -83,6 +108,8 @@ class App extends React.Component<Props, State> {
|
||||
changeSpeakerCallback={this.setSpeakerBySlug}
|
||||
changeSpectrogramCallback={this.updateSpectrogram}
|
||||
changeExtrasModeCallback={this.switchExtrasMode}
|
||||
spectrogramMode={this.state.spectrogramMode}
|
||||
changeSpectrogramMode={this.setSpectrogramMode}
|
||||
/>;
|
||||
break;
|
||||
case Mode.USAGE_MODE:
|
||||
@@ -110,4 +137,4 @@ class App extends React.Component<Props, State> {
|
||||
}
|
||||
}
|
||||
|
||||
export default App;
|
||||
export { App, SpectrogramMode };
|
||||
@@ -1,7 +1,7 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import './index.css';
|
||||
import App from './App';
|
||||
import { App } from './App';
|
||||
import * as serviceWorker from './serviceWorker';
|
||||
|
||||
ReactDOM.render(
|
||||
|
||||
@@ -6,9 +6,11 @@ import { createColorMap, linearScale } from "@colormap/core";
|
||||
//import { VIRIDIS, CIVIDIS, PLASMA, INFERNO, MAGMA, BLACK_WHITE, } from "@colormap/presets";
|
||||
import { MAGMA } from "@colormap/presets";
|
||||
import { Spectrogram } from './extras/Spectrogram';
|
||||
import { SpectrogramMode } from '../../App';
|
||||
|
||||
interface Props {
|
||||
currentSpeaker: Speaker,
|
||||
spectrogramMode: SpectrogramMode,
|
||||
clearStatusCallback: () => void,
|
||||
setHintMessage: (message: string) => void,
|
||||
onSpeakRequestCallback: () => void,
|
||||
@@ -137,7 +139,9 @@ class Form extends React.Component<Props, State> {
|
||||
|
||||
let bytes = base64ToArrayBuffer(res.spectrogram.bytes_base64);
|
||||
|
||||
const spectrogram = new Spectrogram(bytes, res.spectrogram.width, res.spectrogram.height);
|
||||
let base64ByteString = res.spectrogram.bytes_base64;
|
||||
|
||||
const spectrogram = new Spectrogram(base64ByteString, bytes, res.spectrogram.width, res.spectrogram.height);
|
||||
this.props.updateSpectrogramCallback(spectrogram);
|
||||
|
||||
const sound = new Howl.Howl({
|
||||
|
||||
@@ -6,6 +6,7 @@ import { SpeakerDropdown } from './SpeakerDropdown';
|
||||
import { Speaker } from '../../Speakers';
|
||||
import { ExtrasComponent, ExtrasMode } from './extras/ExtrasComponent';
|
||||
import { Spectrogram } from './extras/Spectrogram';
|
||||
import { SpectrogramMode } from '../../App';
|
||||
|
||||
enum StatusState {
|
||||
NONE,
|
||||
@@ -18,9 +19,11 @@ interface Props {
|
||||
extrasMode: ExtrasMode,
|
||||
currentSpeaker: Speaker,
|
||||
currentSpectrogram?: Spectrogram,
|
||||
spectrogramMode: SpectrogramMode,
|
||||
changeSpeakerCallback: (slug: string) => void,
|
||||
changeSpectrogramCallback: (spectrogram: Spectrogram) => void,
|
||||
changeExtrasModeCallback: (extrasMode: ExtrasMode) => void,
|
||||
changeSpectrogramMode: (spectrogramMode: SpectrogramMode) => void,
|
||||
}
|
||||
|
||||
interface State {
|
||||
@@ -123,7 +126,9 @@ class SpeakComponent extends React.Component<Props, State> {
|
||||
extrasMode={this.props.extrasMode}
|
||||
currentSpeaker={this.props.currentSpeaker}
|
||||
currentSpectrogram={this.props.currentSpectrogram}
|
||||
spectrogramMode={this.props.spectrogramMode}
|
||||
changeExtrasModeCallback={this.props.changeExtrasModeCallback}
|
||||
changeSpectrogramMode={this.props.changeSpectrogramMode}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -135,6 +140,7 @@ class SpeakComponent extends React.Component<Props, State> {
|
||||
currentSpeaker={this.props.currentSpeaker}
|
||||
clearStatusCallback={this.clearMessage}
|
||||
setHintMessage={this.setHintMessage}
|
||||
spectrogramMode={this.props.spectrogramMode}
|
||||
onSpeakRequestCallback={this.onSpeakRequest}
|
||||
onSpeakSuccessCallback={this.onSpeakSuccess}
|
||||
onSpeakErrorCallback={this.onSpeakError}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Speaker } from '../../../Speakers';
|
||||
import { SpeakerInfo } from './SpeakerInfo';
|
||||
import { Spectrogram } from './Spectrogram';
|
||||
import { SpectrogramComponent } from './SpectrogramComponent';
|
||||
import { SpectrogramMode } from '../../../App';
|
||||
|
||||
enum ExtrasMode {
|
||||
SPEAKER_INFO,
|
||||
@@ -13,7 +14,9 @@ interface Props {
|
||||
extrasMode: ExtrasMode,
|
||||
currentSpeaker: Speaker,
|
||||
currentSpectrogram?: Spectrogram,
|
||||
spectrogramMode: SpectrogramMode,
|
||||
changeExtrasModeCallback: (extrasMode: ExtrasMode) => void,
|
||||
changeSpectrogramMode: (spectrogramMode: SpectrogramMode) => void,
|
||||
}
|
||||
|
||||
interface State {
|
||||
@@ -34,7 +37,11 @@ class ExtrasComponent extends React.Component<Props, State> {
|
||||
modeComponent = <SpeakerInfo currentSpeaker={this.props.currentSpeaker} />
|
||||
break;
|
||||
case ExtrasMode.SPECTROGRAM:
|
||||
modeComponent = <SpectrogramComponent currentSpectrogram={this.props.currentSpectrogram} />
|
||||
modeComponent = <SpectrogramComponent
|
||||
currentSpectrogram={this.props.currentSpectrogram}
|
||||
spectrogramMode={this.props.spectrogramMode}
|
||||
changeSpectrogramMode={this.props.changeSpectrogramMode}
|
||||
/>
|
||||
break;
|
||||
}
|
||||
return (
|
||||
|
||||
@@ -1,14 +1,105 @@
|
||||
import { ColorMap, createColorMap, linearScale, RGBColor } from "@colormap/core";
|
||||
import { SpectrogramMode } from "../../../App";
|
||||
import { VIRIDIS, INFERNO, MAGMA, BLACK_WHITE, CIVIDIS, PLASMA, } from "@colormap/presets";
|
||||
import { BONE } from "./colormap-bone";
|
||||
import { COPPER } from "./colormap-copper";
|
||||
import { JET } from "./colormap-jet";
|
||||
import { RDBU } from "./colormap-rdbu";
|
||||
import { RDGY } from "./colormap-rdgy";
|
||||
import { GIST_HEAT } from "./colormap-gist_heat";
|
||||
import { PINK } from "./colormap-pink";
|
||||
import { AFMHOT } from "./colormap-afmhot";
|
||||
import { COOL } from "./colormap-cool";
|
||||
import { SPRING } from "./colormap-spring";
|
||||
|
||||
class Spectrogram {
|
||||
base64bytes: string;
|
||||
pixels: Uint8ClampedArray;
|
||||
width: number;
|
||||
height: number;
|
||||
|
||||
constructor(pixels: Uint8ClampedArray, width: number, height: number) {
|
||||
constructor(base64bytes: string, pixels: Uint8ClampedArray, width: number, height: number) {
|
||||
this.base64bytes = base64bytes;
|
||||
this.pixels = pixels;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
calculatePixelsForMode(spectrogramMode: SpectrogramMode) : Uint8ClampedArray {
|
||||
let scale = linearScale([0, 255], [0, 1]);
|
||||
|
||||
let colors = this.spectrogramModeToColors(spectrogramMode);
|
||||
let colorMap = createColorMap(colors, scale);
|
||||
|
||||
return base64ToArrayBuffer(this.base64bytes, colorMap);
|
||||
}
|
||||
|
||||
spectrogramModeToColors(spectrogramMode: SpectrogramMode) : RGBColor[] {
|
||||
switch (spectrogramMode) {
|
||||
case SpectrogramMode.VIRIDIS:
|
||||
return VIRIDIS;
|
||||
case SpectrogramMode.CIVIDIS:
|
||||
return CIVIDIS;
|
||||
case SpectrogramMode.PLASMA:
|
||||
return PLASMA;
|
||||
case SpectrogramMode.INFERNO:
|
||||
return INFERNO;
|
||||
case SpectrogramMode.MAGMA:
|
||||
return MAGMA;
|
||||
case SpectrogramMode.BLACK_WHITE:
|
||||
return BLACK_WHITE;
|
||||
case SpectrogramMode.BONE:
|
||||
return BONE;
|
||||
case SpectrogramMode.COPPER:
|
||||
return COPPER;
|
||||
case SpectrogramMode.JET:
|
||||
return JET;
|
||||
case SpectrogramMode.RDBU:
|
||||
return RDBU;
|
||||
case SpectrogramMode.RDGY:
|
||||
return RDGY;
|
||||
case SpectrogramMode.GIST_HEAT:
|
||||
return GIST_HEAT
|
||||
case SpectrogramMode.PINK:
|
||||
return PINK
|
||||
case SpectrogramMode.AFMHOT:
|
||||
return AFMHOT
|
||||
case SpectrogramMode.COOL:
|
||||
return COOL
|
||||
case SpectrogramMode.SPRING:
|
||||
return SPRING
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { Spectrogram };
|
||||
function nextSpectrogramMode(spectrogramMode: SpectrogramMode) : SpectrogramMode {
|
||||
const index = Number(spectrogramMode);
|
||||
const keys = Object.keys(SpectrogramMode).filter(k => typeof SpectrogramMode[k as any] === "number");
|
||||
const length = keys.length;
|
||||
let next = (index + 1) % length;
|
||||
return next as SpectrogramMode;
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/a/21797381
|
||||
function base64ToArrayBuffer(base64string: string, colorMap: ColorMap) : Uint8ClampedArray {
|
||||
var binary_string = window.atob(base64string);
|
||||
var len = binary_string.length * 4;
|
||||
let bytes = new Uint8ClampedArray(len);
|
||||
for (let i = 0, j = 0; i < len; i++, j += 4) {
|
||||
let val = binary_string.charCodeAt(i);
|
||||
if (isNaN(val)) {
|
||||
val = 0;
|
||||
}
|
||||
let mapped = colorMap(val);
|
||||
let r = Math.floor(mapped[0] * 255)
|
||||
let g = Math.floor(mapped[1] * 255)
|
||||
let b = Math.floor(mapped[2] * 255)
|
||||
bytes[j+0] = r;
|
||||
bytes[j+1] = g;
|
||||
bytes[j+2] = b;
|
||||
bytes[j+3] = 255;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
export { Spectrogram, nextSpectrogramMode };
|
||||
@@ -1,8 +1,11 @@
|
||||
import React from 'react';
|
||||
import { Spectrogram } from './Spectrogram';
|
||||
import { Spectrogram, nextSpectrogramMode } from './Spectrogram';
|
||||
import { SpectrogramMode } from '../../../App';
|
||||
|
||||
interface Props {
|
||||
currentSpectrogram?: Spectrogram,
|
||||
spectrogramMode: SpectrogramMode,
|
||||
changeSpectrogramMode: (spectrogramMode: SpectrogramMode) => void,
|
||||
}
|
||||
|
||||
interface State {
|
||||
@@ -34,7 +37,9 @@ class SpectrogramComponent extends React.Component<Props, State> {
|
||||
let width = this.props.currentSpectrogram!.width;
|
||||
let height = this.props.currentSpectrogram!.height;
|
||||
|
||||
var image = new ImageData(this.props.currentSpectrogram!.pixels, width, height);
|
||||
let pixels = this.props.currentSpectrogram.calculatePixelsForMode(this.props.spectrogramMode);
|
||||
|
||||
var image = new ImageData(pixels, width, height);
|
||||
|
||||
createImageBitmap(image).then(renderer => {
|
||||
ctx.drawImage(renderer, 0, 0, width * 3, height * 3)
|
||||
@@ -55,9 +60,12 @@ class SpectrogramComponent extends React.Component<Props, State> {
|
||||
// TODO: This needs to go way up the tree.
|
||||
let canvas = <canvas ref="canvas" width={width} height={height} id="spectrogram" />
|
||||
|
||||
let nextMode = nextSpectrogramMode(this.props.spectrogramMode);
|
||||
|
||||
return (
|
||||
<div>
|
||||
{canvas}
|
||||
<button onClick={() => this.props.changeSpectrogramMode(nextMode)}>Next</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user