feat: 优先从10种初始颜色中获取随机颜色
This commit is contained in:
parent
ac9817acd3
commit
e8a242f8d1
|
@ -101,16 +101,10 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import { getColor } from '../../js/color.js'
|
||||||
|
|
||||||
// 是否是单机版
|
// 是否是单机版
|
||||||
const isLocal = false
|
const isLocal = false
|
||||||
function getColor () {
|
|
||||||
const idxs = '0123456789abcdef'
|
|
||||||
let color = '#'
|
|
||||||
for (let i = 0; i < 6; i += 1) {
|
|
||||||
color += idxs[Math.random() * idxs.length | 0]
|
|
||||||
}
|
|
||||||
return color
|
|
||||||
}
|
|
||||||
|
|
||||||
function get (url, cb) {
|
function get (url, cb) {
|
||||||
query('GET', url, '', cb)
|
query('GET', url, '', cb)
|
||||||
|
@ -376,7 +370,7 @@ export default {
|
||||||
if (!newType) return false
|
if (!newType) return false
|
||||||
if (that.types[newType]) return false
|
if (that.types[newType]) return false
|
||||||
that.$set(that.types, newType, {
|
that.$set(that.types, newType, {
|
||||||
color: getColor()
|
color: getColor(that.types)
|
||||||
})
|
})
|
||||||
that.typeList.push(newType)
|
that.typeList.push(newType)
|
||||||
updateType2Server(that.projectName, that.typeList, that.types)
|
updateType2Server(that.projectName, that.typeList, that.types)
|
||||||
|
|
|
@ -61,14 +61,8 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
function getColor () {
|
import { getColor } from '../../js/color'
|
||||||
const idxs = '0123456789abcdef'
|
|
||||||
let color = '#'
|
|
||||||
for (let i = 0; i < 6; i += 1) {
|
|
||||||
color += idxs[Math.random() * idxs.length | 0]
|
|
||||||
}
|
|
||||||
return color
|
|
||||||
}
|
|
||||||
function get (url, cb) {
|
function get (url, cb) {
|
||||||
query('GET', url, '', cb)
|
query('GET', url, '', cb)
|
||||||
}
|
}
|
||||||
|
@ -197,7 +191,7 @@ export default {
|
||||||
if (!newType) return false
|
if (!newType) return false
|
||||||
if (that.types[newType]) return false
|
if (that.types[newType]) return false
|
||||||
that.$set(that.types, newType, {
|
that.$set(that.types, newType, {
|
||||||
color: getColor()
|
color: getColor(that.types)
|
||||||
})
|
})
|
||||||
that.typeList.push(newType)
|
that.typeList.push(newType)
|
||||||
console.log(that.typeList)
|
console.log(that.typeList)
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
const COLORS = [
|
||||||
|
'#e12d2d', // 红
|
||||||
|
'#f5a314', // 橙
|
||||||
|
'#fff838', // 黄
|
||||||
|
'#72ca2b', // 绿
|
||||||
|
'#529dff', // 蓝
|
||||||
|
'#647cf7', // 靛
|
||||||
|
'#a855ec', // 紫
|
||||||
|
'#eb98de', // 粉
|
||||||
|
'#c08c8e', // 褐
|
||||||
|
'#d2ff8f' // 青
|
||||||
|
]
|
||||||
|
|
||||||
|
function getColor (types = {}) {
|
||||||
|
const alreadyColorDic = {}
|
||||||
|
Object.keys(types).forEach(type => {
|
||||||
|
const { color } = types[type]
|
||||||
|
alreadyColorDic[color] = true
|
||||||
|
})
|
||||||
|
const remainingColors = []
|
||||||
|
COLORS.forEach(color => {
|
||||||
|
if (!alreadyColorDic[color]) remainingColors.push(color)
|
||||||
|
})
|
||||||
|
if (remainingColors.length) {
|
||||||
|
// 优先选用未选择的初始颜色
|
||||||
|
const idx = Math.random() * remainingColors.length | 0
|
||||||
|
return remainingColors[idx]
|
||||||
|
} else {
|
||||||
|
const idxs = '0123456789abcdef'
|
||||||
|
let color = '#'
|
||||||
|
for (let i = 0; i < 6; i += 1) {
|
||||||
|
color += idxs[Math.random() * idxs.length | 0]
|
||||||
|
}
|
||||||
|
return color
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
getColor
|
||||||
|
}
|
Loading…
Reference in New Issue