Echarts颜色工具类渐变

  // 处理颜色字段
        updatedJson = ColorUtils.processChartColors(JSON.toJSONString(updatedJson), getdata.size());

        // 计算 size 为 seriesName 的大小
        int size = seriesName.size();
        // 初始化 ObjectMapper 和 JsonNode
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
        // 获取 color 字段
        JsonNode colorNode = jsonNode.path("color");
        // 创建一个 List<String> 来存放 color 字段值
        List<String> baseColors = new ArrayList<>();

        // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
        if (colorNode.isArray()) {
            for (JsonNode color : colorNode) {
                String colorValue = color.asText();
                baseColors.add(colorValue);
            }
        }
        // 输出 color 字段的值
        System.out.println("我是color字段: " + baseColors);
        // 生成渐变颜色列表
        List<String> gradientColors = ColorUtils.generateGradientColors(baseColors, size);
        // 用 gradientColors 替换原来的 color 字段
        ArrayNode gradientColorsNode = objectMapper.valueToTree(gradientColors);
        jsonNode.set("color", gradientColorsNode);
package com.chart.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;

/**
 * 生成主题颜色
 * 
 * @author chart
 */
public class ColorUtils
{
    public static Set<String> getColors(int num, Set<String> colorSet) {
        Random random = new Random();
        num=colorSet.size()+num;
        while (colorSet.size() < num) {
            // 生成红、绿、蓝分量的随机十六进制值
            String red = Integer.toHexString(random.nextInt(256)).toUpperCase(); // 0 到 255
            String green = Integer.toHexString(random.nextInt(256)).toUpperCase();
            String blue = Integer.toHexString(random.nextInt(256)).toUpperCase();

            // 确保每个分量的十六进制字符串长度为2(不足两位前面补0)
            red = padZero(red);
            green = padZero(green);
            blue = padZero(blue);

            // 构造颜色代码
            String colorCode = "#" + red + green + blue;

            // 将颜色代码添加到Set中,确保不重复
            colorSet.add(colorCode);
        }
        return colorSet;
    }


    public static List<String> generateGradientColors(String color1, String color2, int numGradients) {
        List<String> colorList = new ArrayList<>();
        int[] rgb1 = hexToRGB(color1);
        int[] rgb2 = hexToRGB(color2);

        // 将起始颜色添加到列表中
        colorList.add(color1);

        // 计算每个颜色分量的增量
        for (int i = 1; i <= numGradients; i++) {
            float ratio = (float) i / (numGradients + 1); // 计算当前颜色在渐变中的位置
            int[] newRGB = new int[3];
            for (int j = 0; j < 3; j++) {
                newRGB[j] = (int) (rgb1[j] + ratio * (rgb2[j] - rgb1[j])); // 线性插值
            }
            String newColor = rgbToHex(newRGB);
            colorList.add(newColor);
        }

        // 将结束颜色添加到列表中
        colorList.add(color2);

        return colorList;
    }

    //获取颜色过渡色
    public static List<String> generateGradientColors(List<String> baseColors, int totalColors) {
        List<String> colorList = new ArrayList<>();
        int numBaseColors = baseColors.size();
        int numSegments = numBaseColors - 1;
        int colorsPerSegment = (totalColors - numBaseColors) / numSegments;
        int remainder = (totalColors - numBaseColors) % numSegments;

        for (int i = 0; i < numBaseColors - 1; i++) {
            String startColor = baseColors.get(i);
            String endColor = baseColors.get(i + 1);
            colorList.add(startColor);

            int extraColor = (i < remainder) ? 1 : 0;
            int numColorsToGenerate = colorsPerSegment + extraColor;

            for (int j = 1; j <= numColorsToGenerate; j++) {
                float ratio = (float) j / (numColorsToGenerate + 1);
                String newColor = interpolateColor(startColor, endColor, ratio);
                colorList.add(newColor);
            }
        }
        colorList.add(baseColors.get(numBaseColors - 1));

        return colorList;
    }

    private static String interpolateColor(String color1, String color2, float ratio) {
        int[] rgb1 = hexToRGB(color1);
        int[] rgb2 = hexToRGB(color2);
        int[] rgb = new int[3];

        for (int i = 0; i < 3; i++) {
            rgb[i] = (int) (rgb1[i] + ratio * (rgb2[i] - rgb1[i]));
        }

        return rgbToHex(rgb);
    }

    private static int[] hexToRGB(String hexColor) {
        return new int[] {
                Integer.parseInt(hexColor.substring(1, 3), 16),
                Integer.parseInt(hexColor.substring(3, 5), 16),
                Integer.parseInt(hexColor.substring(5, 7), 16)
        };
    }

    private static String rgbToHex(int[] rgb) {
        return String.format("#%02X%02X%02X", rgb[0], rgb[1], rgb[2]);
    }


    private static String padZero(String hex) {
        return hex.length() == 1 ? "0" + hex : hex;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值