ny
23 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<script lang="ts">
import type { PropType } from 'vue';
 
import { defineComponent, ref, watch, watchEffect } from 'vue';
 
import Color from '../lib/color';
import { useOptions } from '../useOptions';
 
export default defineComponent({
  name: 'PreDefine',
  props: {
    color: {
      required: true,
      type: Object as PropType<Color>,
    },
    colors: {
      required: true,
      type: Array,
    },
  },
  setup(props) {
    const currentColor: any = useOptions()?.currentColor;
 
    // data
    const rgbaColors = ref(parseColors(props.colors, props.color));
 
    // watch
    watch(
      () => currentColor.value,
      (val) => {
        const color = new Color();
        color.fromString(val);
 
        rgbaColors.value.forEach((item) => {
          item.selected = color.compare(item);
        });
      },
    );
    watchEffect(() => {
      rgbaColors.value = parseColors(props.colors, props.color);
    });
 
    // methods
    function handleSelect(index) {
      props.color.fromString(props.colors[index]);
    }
    function parseColors(colors, color) {
      return colors.map((value) => {
        const c = new Color();
        c.enableAlpha = true;
        c.format = 'rgba';
        c.fromString(value);
        c.selected = c.value === color.value;
        return c;
      });
    }
 
    return {
      handleSelect,
      rgbaColors,
    };
  },
});
</script>
 
<template>
  <div class="ant-color-predefine">
    <div class="ant-color-predefine__colors">
      <div
        v-for="(item, index) in rgbaColors"
        :key="rgbaColors[index]"
        :class="{ selected: item.selected, 'is-alpha': item._alpha < 100 }"
        class="ant-color-predefine__color-selector"
        @click="handleSelect(index)">
        <div :style="{ backgroundColor: item.value }"></div>
      </div>
    </div>
  </div>
</template>
 
<style lang="scss" scoped>
.ant-color-predefine {
  display: flex;
  width: 280px;
  margin-top: 8px;
  font-size: 12px;
 
  .ant-color-predefine__colors {
    display: flex;
    flex: 1;
    flex-wrap: wrap;
  }
 
  .ant-color-predefine__color-selector {
    width: 20px;
    height: 20px;
    margin: 0 0 8px 8px;
    cursor: pointer;
    border-radius: 4px;
 
    &:nth-child(10n + 1) {
      margin-left: 0;
    }
 
    > div {
      display: flex;
      height: 100%;
      border-radius: 3px;
    }
 
    &.selected {
      box-shadow: 0 0 3px 2px #40a9ff;
    }
  }
}
</style>