【发布时间】:2019-01-23 04:37:28
【问题描述】:
我一直在尝试将此 Python 代码转换为 Processing / Java。没看懂。
def step (r,g,b, repetitions=1):
lum = math.sqrt( .241 * r + .691 * g + .068 * b )
h, s, v = colorsys.rgb_to_hsv(r,g,b)
h2 = int(h * repetitions)
lum2 = int(lum * repetitions)
v2 = int(v * repetitions)
return (h2, lum, v2)
colours.sort(key=lambda (r,g,b): step(r,g,b,8) )
这是 Alan Zucconi 网站上的代码:The incredibly challenging task of sorting colours。
这是我目前所拥有的:
color [] colours = new color[1000];
float lum;
int repetitions = 0;
void setup() {
size(1024, 500);
// pick 1000 random color and put in array
for (int i=0; i<colours.length; i++) {
colours[i]= color(random(255), random(255), random(255));
}
printBar(0);
//simple rgb sort
colours = sort(colours);
printBar(50);
// step sort
for (int j=0; j<colours.length; j++) {
lum = sqrt(.241 * red(colours[j]) + .691 * green(colours[j]) + .068 * blue(colours[j]));
int h2 = int(hue(colours[j]) * repetitions);
int lum2 = int(lum * repetitions);
int v2 = int(brightness(colours[j]) * repetitions);
//I am stuck.......
}
printBar(100);
}
void draw() {
}
void printBar(int step) {
//show all the random colors in a bar
for (int j=0; j<colours.length; j++) {
//strokeWeight(2);
stroke(colours[j]);
line(j+5, 20+step, j+5, 50+step);
}
}
我知道 HSB 和 HSV 颜色系统会有一些问题,但我会解决的。我没有得到的是 Python 中的 stepsorting 行。 有任何想法吗 ?谢谢
【问题讨论】:
-
您面临的具体问题是什么?你的代码哪里出错了?
-
最后一行 Python 代码有问题,不知道如何翻译成 java。这实际上返回了一个感知上很好的排序。显示在网站上。
-
看看Collections.sort()方法和java Comparators,顺便说一句,可以使用java的lambdas实现>=1.8
标签: java python sorting processing color-space