# Python code conversion to processing Java

**URL:** <https://discourse.processing.org/t/python-code-conversion-to-processing-java/2438>\
**Category:** Coding Questions\
**Created:** [August 7, 2018, 7:59am UTC](https://discourse.processing.org/t/python-code-conversion-to-processing-java/2438 "2018-08-07T07:59:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fun](https://avatars.discourse-cdn.com/v4/letter/f/ac8455/32.png) [@fun](https://discourse.processing.org/u/fun)\
**Post date:** [August 7, 2018, 7:59am UTC](https://discourse.processing.org/t/python-code-conversion-to-processing-java/2438/1 "2018-08-07T07:59:27Z")

</div>

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:

> **[The incredibly challenging task of sorting colours - Alan Zucconi](https://www.alanzucconi.com/2015/09/30/colour-sorting/)**
>
> Sorting colours is a surprisingly challenging task. This tutorial will explain how you can do it. Code and images provided.

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

**python code** from Alan Zucconi:

```auto
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:

```auto
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);
  }
}

```

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.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 7, 2018, 8:24am UTC](https://discourse.processing.org/t/python-code-conversion-to-processing-java/2438/2 "2018-08-07T08:24:01Z")

</div>

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

That **step()** function relies on a method called **rgb\_to\_hsv()** from a class called colorsys. 🌈

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

Maybe Java’s class ColorSpace from package `java.awt.color` w/ its method **fromRGB​()** can do that: 🤔  
[Docs.Oracle.com/javase/10/docs/api/java/awt/color/ColorSpace.html#fromRGB(float[])](http://Docs.Oracle.com/javase/10/docs/api/java/awt/color/ColorSpace.html#fromRGB(float%5B%5D))

---

<div class="post-metadata">

**Author:** ![fun](https://avatars.discourse-cdn.com/v4/letter/f/ac8455/32.png) [@fun](https://discourse.processing.org/u/fun)\
**Post date:** [August 7, 2018, 8:45am UTC](https://discourse.processing.org/t/python-code-conversion-to-processing-java/2438/3 "2018-08-07T08:45:59Z")

</div>

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:

```auto
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 ?

???

---

<div class="post-metadata">

**Author:** ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)\
**Post date:** [August 7, 2018, 8:54am UTC](https://discourse.processing.org/t/python-code-conversion-to-processing-java/2438/4 "2018-08-07T08:54:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 7, 2018, 9:00am UTC](https://discourse.processing.org/t/python-code-conversion-to-processing-java/2438/5 "2018-08-07T09:00:54Z")

</div>

[Docs.Python.org/3/library/stdtypes.html#list.sort](http://Docs.Python.org/3/library/stdtypes.html#list.sort)

[Docs.Oracle.com/javase/10/docs/api/java/util/Arrays.html#sort(T[],java.util.Comparator)](http://Docs.Oracle.com/javase/10/docs/api/java/util/Arrays.html#sort(T%5B%5D,java.util.Comparator))

---

<div class="post-metadata">

**Author:** ![fun](https://avatars.discourse-cdn.com/v4/letter/f/ac8455/32.png) [@fun](https://discourse.processing.org/u/fun)\
**Post date:** [August 7, 2018, 9:11am UTC](https://discourse.processing.org/t/python-code-conversion-to-processing-java/2438/6 "2018-08-07T09:11:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)\
**Post date:** [August 7, 2018, 10:19am UTC](https://discourse.processing.org/t/python-code-conversion-to-processing-java/2438/7 "2018-08-07T10:19:44Z")

</div>

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,

```python
f = lambda x: 2*x

```

is similar to python’s def:

```python
def f(x):
    return 2*x

```

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

```java
float f(float x) {
  return 2*x;
}

```

So, the function lambda that you got here:

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

```

is similar to:

```python
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:

```python
names = ["Stephanie","Sarah","Abe","Al"]
names.sort()

```

you would get them back in alphabetical order:

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

```

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

```python
    names = ["Stephanie","Sarah","Abe","Al"]
    age = {"Stephanie":26,
           "Sarah":28,
           "Abe":25,
           "Al":24}

```

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

```python
    names = ["Stephanie","Sarah","Abe","Al"]
    age = {"Stephanie":26,
           "Sarah":28,
           "Abe":25,
           "Al":24}
    names.sort(key=lambda x: age[x])
    print(names)

```

which returns:

```python
['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.

---

<div class="post-metadata">

**Author:** ![fun](https://avatars.discourse-cdn.com/v4/letter/f/ac8455/32.png) [@fun](https://discourse.processing.org/u/fun)\
**Post date:** [August 7, 2018, 1:52pm UTC](https://discourse.processing.org/t/python-code-conversion-to-processing-java/2438/8 "2018-08-07T13:52:45Z")

</div>

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.
