Python code conversion to processing Java

I have been fighting with some python code I found on the web, and I dont get it. How can I do this in Processing / Java ?

Interesting webpage with super ideas:

There is a nice kind of step sorting, that kind of does the job best.

python code from Alan Zucconi:

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) )

This is what I have in Processing so far:

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));

  //simple rgb sort
  colours = sort(colours);

  // 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.......


void draw() {

void printBar(int step) {
  //show all the random colors in a bar
  for (int j=0; j<colours.length; j++) {
    line(j+5, 20+step, j+5, 50+step);

Can this code be converted to Java / Processing ? Looks like the variable repititions is an other loop from 0 to 7 ? I dont get it. I need help.

1 Like

step() is a function in that Python code. So you should make it a function in Java too! :coffee:

That step() function relies on a method called rgb_to_hsv() from a class called colorsys. :rainbow:

You’re gonna need to find a corresponding Java library which does the same thing as that method. :open_book:

Maybe Java’s class ColorSpace from package java.awt.color w/ its method fromRGB​() can do that: :thinking:[])

Thank you for you answer.
I would risk to use the processing internal conversion to HSB (hue,saturation, brightness). This should only give a different direction from light to dark. that would be fine.

What I really dont get at all is the line:

colours.sort(key=lambda (r,g,b): step(r,g,b,8) )

Is this a second loop, and a sorting depending on what ?
lambda seems to be a function for short time usage and is deleted after using ?


‘Colors’ is a list of rgb tuples

Sort is a method available to list objects.

By default, python has a particular way of sorting tuples, so you’re overriding that by using ‘key’ argument that transforms the tuples into a different set of tuples. And then sorting the original list based on the ordering of the new list.

Lamda is used here as a throw away function. It’s a way of programming new simple functions in place rather than breaking your flow while you code and write a def function outside, then rewriting it inside of key.

You can probably use this code as is in Python Mode processing. I need to verify if colorsys module is available to be imported first.

Hope this clarifies it. Let me know if it’s still confusing.[],java.util.Comparator)

Thank you again for both answers, still confused.
Python seems to have very elegant and short ways.

I had the python code running in an atom in python mode. But I need it in processing java, as I plan to use the sorted colors for other things.

I will try to figure out the lambda sorting line. This could be big portion of code in processing.
Lucky python users…

will have to dig in the Python docs.

Let me try to clarify it for you. lambda is a shorter way of writing functions, and all of the following are equivalent ways of writing it. For example,

f = lambda x: 2*x

is similar to python’s def:

def f(x):
    return 2*x

which is similar to java’s way of defining functions:

float f(float x) {
  return 2*x;

So, the function lambda that you got here:

lambda (r,g,b): step(r,g,b,8)

is similar to:

def f(tuple):
    return step(tuple[0],tuple[1],tuple[2],8)

Sort is a list method that usually modifies a list so that its elements are in increasing order, like from lowest to highest, or from A to Z. For instance, in python, if you tried to sort the following list:

names = ["Stephanie","Sarah","Abe","Al"]

you would get them back in alphabetical order:

['Abe', 'Al', 'Sarah', 'Stephanie']

But what if you wanted to sort them by their age instead? Let’s assume you have the following:

    names = ["Stephanie","Sarah","Abe","Al"]
    age = {"Stephanie":26,

Then I would want to sort the names based on their ages, which you can do by setting the key argument as follows:

    names = ["Stephanie","Sarah","Abe","Al"]
    age = {"Stephanie":26,
    names.sort(key=lambda x: age[x])

which returns:

['Al', 'Abe', 'Stephanie', 'Sarah']

key argument kind of maps a function to each element in the list. In this case, I used a lambda function because I needed it for a simple purpose.

Going back to the original topic to translating it to Java, you essentially need to sort a list of tuples of (R,G,B) colors based on the order you would get if you had sorted a list of those same colors after performing step() function on the elements of that list. I’m sure gotoloop has suggestions on how to do that in java.

1 Like

Thank you so much for your effort. This helps a lot.

This is such a clever little piece of code.

So can I say that in the python code the first 7 lines are one function. I pop in an r,g,b and get a hue, a perceived luminosity and a value ?
Does repetitions go up ? What does it do ?

Then I use this function in stead of the normal sorting and say I want 8 steps.
How does python know where red starts and purple ends in hsv ?

My brain is exploding. This shortening in python is tricky.