java根据value值 计算线性渐变色
绘制出的图片效果

颜色插值实现部分代码
/*
传入的参数格式
String[] breakArray = "-999,0,3,5,10,15,20,30".split(",");
String colorStr = "[[0,0,0,0],[74,145,148,1],[77,141,124,1],[76,164,76,1],[161,135,63,1],[161,108,91,1],[151,75,144,1],[95,99,159,1]]";
JSONArray colorArr = JSONArray.parseArray(colorStr);
*/
/**
* 计算线性渐变色
*
* @param value value
* @param colorArr 颜色数组
* @param breakArray 颜色分级
* @return int[]
*/
static int[] getlinearColor(double value, JSONArray colorArr, String[] breakArray) {
for (int i = 0; i < breakArray.length; i++) {
double breakValue = Double.parseDouble(breakArray[i]);
JSONArray rgba = colorArr.getJSONArray(i);
if (value <= breakValue) {
if (i == 0) {
return arrayToPixel(rgba);
} else {
double lastBreak = Double.parseDouble(breakArray[i - 1]);
JSONArray lastrgba = colorArr.getJSONArray(i - 1);
int[] pixel = new int[4];
//计算RGB的线性值
pixel[0] = (int) linearInterpolation(value, lastBreak, breakValue, lastrgba.getFloat(0), rgba.getFloat(0));
pixel[1] = (int) linearInterpolation(value, lastBreak, breakValue, lastrgba.getFloat(1), rgba.getFloat(1));
pixel[2] = (int) linearInterpolation(value, lastBreak, breakValue, lastrgba.getFloat(2), rgba.getFloat(2));
pixel[3] = (int) linearInterpolation(value, lastBreak, breakValue, lastrgba.getFloat(3), rgba.getFloat(3));
return pixel;
}
}
}
return arrayToPixel(colorArr.getJSONArray(colorArr.size() - 1));
}
/**
* 线性插值
*
* @param x 插值点坐标
* @param x1 顶点坐标
* @param x2 顶点坐标
* @param x1value 顶点数值
* @param x2value 顶点数值
* @return 插值后的数值
*/
static float linearInterpolation(double x, double x1, double x2, float x1value, float x2value) {
return (float) (((x2 - x) / (x2 - x1) * x1value) + ((x - x1) / (x2 - x1) * x2value));
}
/**
* array转int[] 获取像素点颜色
*
* @param rgba rgbaArray
* @return int[]
*/
static int[] arrayToPixel(JSONArray rgba) {
int[] pixel = new int[4];
pixel[0] = rgba.getInteger(0);
pixel[1] = rgba.getInteger(1);
pixel[2] = rgba.getInteger(2);
pixel[3] = rgba.getInteger(3);
return pixel;
}