android - 找到三种颜色之间的比例

标签 android colors

我不是 Android 开发人员,但我的团队成员需要与我在 Web 上所做的相同的事情。我需要一个函数,我可以将任意三种颜色(例如红色、蓝色、绿色)传递给它,然后我将传递一个计数,例如100.

函数定义

function getColorArray(mincolor,midcolor,maxcolor,100){
    return colorarray;
}

当我必须调用函数时:

getColorArray(red,yellow,green,100)

所以它会给出一个包含 100 种颜色的数组,来自红色、蓝色、绿色色标。

我是用 Javascript 做的。这是 fiddle link .

我希望在 Android 中有相同的输出。

最佳答案

此代码执行简单的线插值 (c1 - c2, c2 - c3) 。您的示例 JS 代码具有比这个简单示例(非线性插值)更丰富的选项,但我认为这应该可以帮助您入门。

如果您要让用户命名颜色,您可能应该定义一些自定义颜色 - 系统颜色的默认范围非常有限(至少对于 java.awt.Color 预定义颜色,即)。

import java.awt.*;
import javax.swing.*;
import java.lang.reflect.Field;

public class ColorTest {
    public static void main(String[] args) {
        int n = args.length > 0 ? Integer.parseInt(args[0]) : 5;
        Color[] test = getColorArray("red", "green", "blue", n);
        for(Color c : test) {
            System.out.println(c);
        }
    }
    
    public static Color[] getColorArray(String c1, String c2, String c3, int n) {
        Color[] inputColors = new Color[3];
        try {
            Field field1 = Color.class.getField(c1);
            Field field2 = Color.class.getField(c2);
            Field field3 = Color.class.getField(c3);
        
            inputColors[0] = (Color) field1.get(null); 
            inputColors[1] = (Color) field2.get(null);
            inputColors[2] = (Color) field3.get(null);
        } catch (Exception e) {
            System.err.println("One of the color values is not defined!");
            System.err.println(e.getMessage());
            return null;
        }
    
        Color[] result = new Color[n];
    
        int[] c1RGB = { inputColors[0].getRed(), inputColors[0].getGreen(), inputColors[0].getBlue() };
        int[] c2RGB = { inputColors[1].getRed(), inputColors[1].getGreen(), inputColors[1].getBlue() };
        int[] c3RGB = { inputColors[2].getRed(), inputColors[2].getGreen(), inputColors[2].getBlue() };
        int[] tmpRGB = new int[3];
    
        tmpRGB[0] = c2RGB[0] - c1RGB[0];
        tmpRGB[1] = c2RGB[1] - c1RGB[1];
        tmpRGB[2] = c2RGB[2] - c1RGB[2];
        float mod = n/2.0f; 
        for (int i = 0; i < n/2; i++) {
            result[i] = new Color(
                (int) (c1RGB[0] + i/mod*tmpRGB[0]) % 256, 
                (int) (c1RGB[1] + i/mod*tmpRGB[1]) % 256,
                (int) (c1RGB[2] + i/mod*tmpRGB[2]) % 256
            );
        }
    
        tmpRGB[0] = c3RGB[0] - c2RGB[0];
        tmpRGB[1] = c3RGB[1] - c2RGB[1];
        tmpRGB[2] = c3RGB[2] - c2RGB[2];
        for (int i = 0; i < n/2 + n%2; i++) {
            result[i+n/2] = new Color(
                (int) (c2RGB[0] + i/mod*tmpRGB[0]) % 256, 
                (int) (c2RGB[1] + i/mod*tmpRGB[1]) % 256,
                (int) (c2RGB[2] + i/mod*tmpRGB[2]) % 256
            );
        }
    
        return result;
    }
}

关于android - 找到三种颜色之间的比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37591689/

相关文章:

java - 如何根据自定义标准保持排序的数据结构

image - Matlab 将图像恢复为原始颜色

javascript - Chart.js Line,负点的不同填充颜色

颜色动画的 jQuery 数学 - 特定颜色范围

java - 如何使用 getPurchases() - Android

java - Android应用程序可实时录制声音并识别频率

android - 多屏支持和无法生成 R.java

android - 为什么 Android 文档中有不同的 id 语法?

graphics - 如何处理 PNG 文件格式中的色彩空间和 Gamma ?

xcode - 混合 Objective-C/Swift 项目中 Storyboard颜色的问题