11// @flow
2-
32import type { MainLayoutState , Action } from "../../MainLayout/types"
43import { moveRegion } from "../../ImageCanvas/region-tools.js"
54import { getIn , setIn , updateIn } from "seamless-immutable"
65import moment from "moment"
76import isEqual from "lodash/isEqual"
87import getActiveImage from "./get-active-image"
98import { saveToHistory } from "./history-handler.js"
9+ import colors from "../../colors"
1010
1111const 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-
2413export 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" ] , {
0 commit comments