pickColor.js 3.23 KB
Newer Older
wuhao's avatar
wuhao committed
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 117 118 119 120 121 122
export default function generateColor(baseColor) {
  // 生成背景色
  const backgroundColor = {
    r: 255,
    g: 255,
    b: 255,
  };

  // 计算附加色 1
  const color1 = {
    r: 255,
    g: 255,
    b: 255,
  };
  color1.r -= Math.round((color1.r - baseColor.r) * 0.25);
  color1.g -= Math.round((color1.g - baseColor.g) * 0.25);
  color1.b -= Math.round((color1.b - baseColor.b) * 0.25);

  // 计算附加色 2
  const color2 = {
    r: 255,
    g: 255,
    b: 255,
  };
  color2.r -= Math.round((color2.r - baseColor.r) * 0.5);
  color2.g -= Math.round((color2.g - baseColor.g) * 0.5);
  color2.b -= Math.round((color2.b - baseColor.b) * 0.5);

  // 计算警告色
  const warningColor = {
    r: 255,
    g: 255,
    b: 255,
  };
  warningColor.r -= Math.round((warningColor.r - baseColor.r) * 0.3);
  warningColor.g -= Math.round((warningColor.g - baseColor.g) * 0.3);
  warningColor.b -= Math.round((warningColor.b - baseColor.b) * 0.3);

  // 计算成功色
  const successColor = {
    r: 255,
    g: 255,
    b: 255,
  };
  successColor.r -= Math.round((successColor.r - baseColor.r) * 0.2);
  successColor.g -= Math.round((successColor.g - baseColor.g) * 0.2);
  successColor.b -= Math.round((successColor.b - baseColor.b) * 0.2);

  // 计算失败色
  const failureColor = {
    r: 255,
    g: 255,
    b: 255,
  };
  failureColor.r -= Math.round((failureColor.r - baseColor.r) * 0.2);
  failureColor.g -= Math.round((failureColor.g - baseColor.g) * 0.2);
  failureColor.b -= Math.round((failureColor.b - baseColor.b) * 0.2);

  // 转换为十六进制颜色值
  function rgbToHex(rgbColor) {
    const { r, g, b } = rgbColor;
    const red = Math.round(r);
    const green = Math.round(g);
    const blue = Math.round(b);

    const hexRed = red.toString(16).padStart(2, "0");
    const hexGreen = green.toString(16).padStart(2, "0");
    const hexBlue = blue.toString(16).padStart(2, "0");

    return `#${hexRed}${hexGreen}${hexBlue}`;
  }

  const hexBaseColor = baseColor;
  const hexBackgroundColor = rgbToHex(backgroundColor);
  const hexColor1 = rgbToHex(color1);
  const hexColor2 = rgbToHex(color2);
  const hexWarningColor = rgbToHex(warningColor);
  const hexSuccessColor = rgbToHex(successColor);
  const hexFailureColor = rgbToHex(failureColor);

  return {
    baseColor: hexBaseColor,
    backgroundColor: hexBackgroundColor,
    color1: hexColor1,
    color2: hexColor2,
    warningColor: hexWarningColor,
    successColor: hexSuccessColor,
    failureColor: hexFailureColor,
  };
}

function generateColorScheme(baseColor) {
  // 将基础色转换为 RGB 格式
  const rgbBaseColor = hexToRGB(baseColor);

  // 生成配色方案
  const colorScheme = {
    baseColor: baseColor,
    backgroundColor: rgbBaseColor,
    color1: "",
    color2: "",
    warningColor: "",
    successColor: "",
    failureColor: "",
  };

  // 生成其他颜色
  const colors = [
    { name: "color1", offset: 0.25 },
    { name: "color2", offset: 0.5 },
    { name: "warningColor", offset: 0.3 },
    { name: "successColor", offset: 0.2 },
    { name: "failureColor", offset: 0.2 },
  ];

  for (const color of colors) {
    const newColor = generateOffsetColor(rgbBaseColor, color.offset);
    colorScheme[color.name] = rgbToHex(newColor);
  }

  return colorScheme;
}