# Better Way To Determine Pixel's Hue Range

**URL:** <https://discourse.processing.org/t/better-way-to-determine-pixels-hue-range/20842>\
**Category:** Coding Questions\
**Created:** [May 13, 2020, 7:20pm UTC](https://discourse.processing.org/t/better-way-to-determine-pixels-hue-range/20842 "2020-05-13T19:20:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![derivativejojomemes](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/derivativejojomemes/32/8899_2.png) [@derivativejojomemes](https://discourse.processing.org/u/derivativejojomemes)\
**Post date:** [May 13, 2020, 7:20pm UTC](https://discourse.processing.org/t/better-way-to-determine-pixels-hue-range/20842/1 "2020-05-13T19:20:31Z")

</div>

Hi!

I’m working on an application that assigns color names based on the hue of a pixel in an image. The method I am currently using is inelegant:

1. Create a String array that holds the names of colors that I want to use.
2. Get the hue at pixels[loc].
3. Use a billion “if” statements to check which range the pixel’s hue falls into.
4. Print the color name (to console, for now).

Here is an example of the code:

```auto
if ( 0.0 <= hue && hue < 30.0) {println(colorNameArray[0]);} // Red
else if ( 30.0 <= hue && hue < 60.0) {println(colorNameArray[1]);} // Orange
else if ( 60.0 <= hue && hue < 90.0) {println(colorNameArray[2]);} // Yellow

```

Is there a neater way to do this? Thank you!

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [May 13, 2020, 7:35pm UTC](https://discourse.processing.org/t/better-way-to-determine-pixels-hue-range/20842/2 "2020-05-13T19:35:31Z")

</div>

Maybe. Is each section 30 hue-units big? If so, just divide:

`println(colorNameArray[int(hue/30)]);`

If they’re not all 30, then you’ll have to put their amounts into an array. Then you can loop over it:

```auto
int[] bounds = {0, 30, 40, 50, 90, 120};

for( int i = 0; i < bounds.length-1; i++){
  if( bounds[i] <= hue && hue < bounds[i+1]){
    println(colorNameArray[i]);
  }
}

```

---

<div class="post-metadata">

**Author:** ![derivativejojomemes](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/derivativejojomemes/32/8899_2.png) [@derivativejojomemes](https://discourse.processing.org/u/derivativejojomemes)\
**Post date:** [May 13, 2020, 7:37pm UTC](https://discourse.processing.org/t/better-way-to-determine-pixels-hue-range/20842/3 "2020-05-13T19:37:17Z")

</div>

Perfect, exactly what I was looking for.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 13, 2020, 8:21pm UTC](https://discourse.processing.org/t/better-way-to-determine-pixels-hue-range/20842/4 "2020-05-13T20:21:03Z")

</div>

You might also be interested in

> <https://stackoverflow.com/questions/4126029/convert-rgb-values-to-color-name/20670056#20670056>

Java also has built-in names for a small palette of colors:

[https://docs.oracle.com/javase/8/docs/api/java/awt/Color.html](https://docs.oracle.com/javase/8/docs/api/java/awt/Color.html)

so you could iterate over those and compare if you didn’t want to provide your own.

[https://www.quora.com/Java-How-to-get-the-name-of-colors-in-Java-from-Hex-or-RGB-values](https://www.quora.com/Java-How-to-get-the-name-of-colors-in-Java-from-Hex-or-RGB-values)

---

<div class="post-metadata">

**Author:** ![derivativejojomemes](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/derivativejojomemes/32/8899_2.png) [@derivativejojomemes](https://discourse.processing.org/u/derivativejojomemes)\
**Post date:** [May 13, 2020, 9:12pm UTC](https://discourse.processing.org/t/better-way-to-determine-pixels-hue-range/20842/5 "2020-05-13T21:12:05Z")

</div>

These were great reads, I learned something new and feel closer to solving my problem.

Thank you very much for your reply and your time!
