Skip to content

Commit d3c8650

Browse files
committed
deterministic colors
1 parent 6fc49cb commit d3c8650

File tree

6 files changed

+71
-41
lines changed

6 files changed

+71
-41
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"@semantic-release/git": "^9.0.0",
1111
"get-image-data": "^3.0.1",
1212
"material-survey": "^1.0.34",
13-
"mmgc1-cpp": "^1.0.24",
13+
"mmgc1-cpp": "^1.0.25",
1414
"moment": "^2.23.0",
1515
"react-full-screen": "^0.2.4",
1616
"react-hotkeys": "^2.0.0",

src/Annotator/reducers/general-reducer.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
// @flow
2-
32
import type { MainLayoutState, Action } from "../../MainLayout/types"
43
import { moveRegion } from "../../ImageCanvas/region-tools.js"
54
import { getIn, setIn, updateIn } from "seamless-immutable"
65
import moment from "moment"
76
import isEqual from "lodash/isEqual"
87
import getActiveImage from "./get-active-image"
98
import { saveToHistory } from "./history-handler.js"
9+
import colors from "../../colors"
1010

1111
const getRandomId = () => Math.random().toString().split(".")[1]
1212

13-
const getRandomInt = (min, max) => {
14-
return Math.floor(Math.random() * (max - min + 1)) + min
15-
}
16-
17-
const getRandomColor = () => {
18-
const h = getRandomInt(0, 360)
19-
const s = 100
20-
const l = 50
21-
return `hsl(${h},${s}%,${l}%)`
22-
}
23-
2413
export default (state: MainLayoutState, action: Action) => {
2514
// Throttle certain actions
2615
if (action.type === "MOUSE_MOVE") {
@@ -306,9 +295,14 @@ export default (state: MainLayoutState, action: Action) => {
306295
}
307296
}
308297

309-
let defaultRegion = undefined
310-
if (activeImage) {
311-
defaultRegion = activeImage.regions.slice(-1)[0].cls
298+
let defaultRegionCls = undefined,
299+
defaultRegionColor = "#333333"
300+
if (activeImage && (activeImage.regions || []).length > 0) {
301+
defaultRegionCls = activeImage.regions.slice(-1)[0].cls
302+
const clsIndex = (state.regionClsList || []).indexOf(defaultRegionCls)
303+
if (clsIndex !== -1) {
304+
defaultRegionColor = colors[clsIndex % colors.length]
305+
}
312306
}
313307

314308
switch (state.selectedTool) {
@@ -320,9 +314,9 @@ export default (state: MainLayoutState, action: Action) => {
320314
y,
321315
highlighted: true,
322316
editingLabels: true,
323-
color: getRandomColor(),
317+
color: defaultRegionColor,
324318
id: getRandomId(),
325-
cls: defaultRegion,
319+
cls: defaultRegionCls,
326320
}
327321
break
328322
}
@@ -336,7 +330,8 @@ export default (state: MainLayoutState, action: Action) => {
336330
h: 0.01,
337331
highlighted: true,
338332
editingLabels: false,
339-
color: getRandomColor(),
333+
color: defaultRegionColor,
334+
cls: defaultRegionCls,
340335
id: getRandomId(),
341336
}
342337
state = unselectRegions(state)
@@ -361,7 +356,8 @@ export default (state: MainLayoutState, action: Action) => {
361356
],
362357
open: true,
363358
highlighted: true,
364-
color: getRandomColor(),
359+
color: defaultRegionColor,
360+
defaultRegionCls: defaultRegionCls,
365361
id: getRandomId(),
366362
}
367363
state = setIn(state, ["mode"], {

src/ImageCanvas/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,11 @@ export const ImageCanvas = ({
384384
topLeft: mat.clone().inverse().applyToPoint(0, 0),
385385
bottomRight: mat.clone().inverse().applyToPoint(iw, ih),
386386
}
387-
387+
388388
const classPoints = useMemo(() => {
389-
console.log("TODO this shouldn't be called on mouse moves")
390-
return regions.filter(region => region.type === "point")
391-
}
392-
,[regions])
389+
// This shouldn't be called on mouse moves
390+
return regions.filter((region) => region.type === "point")
391+
}, [regions])
393392

394393
return (
395394
<div
@@ -482,13 +481,14 @@ export const ImageCanvas = ({
482481
{...mouseEvents}
483482
>
484483
<>
485-
{fullImageSegmentationMode && <ImageMask
486-
imagePosition={imagePosition}
487-
regionClsList={regionClsList}
488-
imageSrc={imageSrc}
489-
classPoints={classPoints
490-
}
491-
/>}
484+
{fullImageSegmentationMode && (
485+
<ImageMask
486+
imagePosition={imagePosition}
487+
regionClsList={regionClsList}
488+
imageSrc={imageSrc}
489+
classPoints={classPoints}
490+
/>
491+
)}
492492
<canvas className={classes.canvas} ref={canvasEl} />
493493
<VideoOrImageCanvasBackground
494494
videoPlaying={videoPlaying}

src/ImageMask/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @flow
22

33
import React, { useState, useEffect, useMemo, useRef } from "react"
4+
import { colorInts } from "../colors"
45

56
import MMGC_INIT from "mmgc1-cpp"
67

@@ -60,20 +61,27 @@ export default ({
6061
const imageAddress = mmgc.getImageAddr()
6162
mmgc.HEAPU8.set(sampleImageData.data, imageAddress)
6263
mmgc.computeSuperPixels()
64+
for (let i = 0; i < colorInts.length; i++) {
65+
mmgc.setClassColor(i, colorInts[i])
66+
}
6367
superPixelsGenerated.current = "done"
6468
}
6569
if (superPixelsGenerated.current !== "done") return
6670

67-
mmgc.setClassColor(0, 0xff0000ff)
68-
mmgc.setClassColor(1, 0xffff00ff)
69-
mmgc.setClassColor(2, 0xff00ffff)
7071
// mmgc.setVerboseMode(true)
7172
mmgc.clearClassPoints()
7273
for (const classPoint of classPoints) {
7374
if (!classPoint.cls) continue
74-
if (classPoint.x < 0) continue
75+
if (classPoint.x < 0 || classPoint.x >= 1) continue
76+
if (classPoint.y < 0 || classPoint.y >= 1) continue
77+
const clsIndex = regionClsList.indexOf(classPoint.cls)
78+
if (clsIndex > colorInts.length) {
79+
console.log("Too many classes to draw on mask!")
80+
continue
81+
}
82+
7583
mmgc.addClassPoint(
76-
regionClsList.indexOf(classPoint.cls),
84+
clsIndex,
7785
Math.floor(classPoint.y * sampleImageData.height),
7886
Math.floor(classPoint.x * sampleImageData.width)
7987
)

src/colors.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// @flow
2+
3+
import * as muiColors from "@material-ui/core/colors"
4+
5+
export const colors = [
6+
muiColors.red[500],
7+
muiColors.blue[500],
8+
muiColors.green[500],
9+
muiColors.orange[800],
10+
muiColors.brown[500],
11+
muiColors.lightGreen[700],
12+
muiColors.pink[500],
13+
muiColors.purple[500],
14+
muiColors.indigo[500],
15+
muiColors.teal[500],
16+
muiColors.lime[500],
17+
muiColors.blueGrey[500],
18+
]
19+
20+
export const colorInts: Array<number> = colors.map(
21+
(c) => (parseInt(c.substr(1), 16) | 0xff000000) >>> 0
22+
)
23+
24+
console.log(colorInts)
25+
26+
export default colors

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10820,10 +10820,10 @@ mkdirp@0.5.1, mkdirp@0.5.x, mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@
1082010820
dependencies:
1082110821
minimist "0.0.8"
1082210822

10823-
mmgc1-cpp@^1.0.24:
10824-
version "1.0.24"
10825-
resolved "https://registry.yarnpkg.com/mmgc1-cpp/-/mmgc1-cpp-1.0.24.tgz#37354150355486a10f304aa72dc9716ad4032d0a"
10826-
integrity sha512-vj19i2gpa9rT+byptCX6Qqb2h8BpQqAAOXhVJXM527KKiU1YDG6fv83DQ9RSRPJTX7RGPYKtb0mwJkPm8pomIA==
10823+
mmgc1-cpp@^1.0.25:
10824+
version "1.0.25"
10825+
resolved "https://registry.yarnpkg.com/mmgc1-cpp/-/mmgc1-cpp-1.0.25.tgz#f5c79f0c4aa45be8496e5d8b6889e3fee207f53b"
10826+
integrity sha512-IepiEIBW8MCc2WJsj1i68gdbZKdEcPeyE7iLtJComPQDHNOCJ3xKZk7gPsw5meSbqKhMOfa2B81jXuLEqwtiRA==
1082710827

1082810828
moment@^2.23.0:
1082910829
version "2.24.0"

0 commit comments

Comments
 (0)