The function "red(int)" does not exist?

hello!
i have the code below to keep sum the rbg values every 16 pixels. However, i’m getting an error saying that red(int), green(int), and blue(int) does not exist?? the argument i passed into the function is a color, so it should work. please help, thank you so much!

public void averageColor(PImage img) {
    for (int i = 0; i < emojiNames.size(); i++) {
      Integer[] Color = new Integer[3];
      int times = 0;
      for (int r = 0; r < img.width; r += 16) {
        for (int c = 0; c < img.height; c += 16) {
          color currentColor = img.get(r, c);
          Color[0] += (int) red(currentColor);
          Color[1] += (int) green(currentColor);
          Color[2] += (int) blue(currentColor);
          times++;
        }
      }
      Color[0] = Color[0]/times;
      Color[1] = Color[1]/times;
      Color[2] = Color[2]/times;
      colorList.add(Color);
    }
  }

Hello @flice,

Your Integer array (objects) is not initialized (contains null elements) and you can’t add to a value in there.

Try an int array (initialized to 0) instead of Integer (null values initially).

Try this:

Reference:

:)

1 Like

unfortunately it still doesn’t seem to work… the error is still there :sob:

OR you incidentally wrote a function that has the name red() elsewhere?

1 Like

Also, try rename Color to colorList

Looks suspicious

1 Like

There are other problems in the list (in the image) you provided and you did not share the related code.
Once you correct the existing problems you will see other problems and that is where my post will help.

Guidelines—Asking Questions < Can you post an MCVE?

The code below works with Processing 4.3 on W10 and does not report any problems.

Try this (your code with additions by me) and report the result on your end:

import java.util.*;
// If using int:
List<int[]> cl = new ArrayList<int[]>();

// If using Integer:
//List<Integer[]> cl = new ArrayList<Integer[]>(); // If using Integer

//PImage sf;

void setup()
  {
  size(200, 200);  
  PImage sf = loadImage("http://learningprocessing.com/code/assets/sunflower.jpg");
  averageColor(sf);
  noLoop();
  }

public void averageColor(PImage img) 
  {
  for (int i = 0; i < 3; i++) 
    {
    // If using int:
    int [] Color = new int[3];
    
    // If using Integer:
    //Integer[] Color = new Integer[3];    
    //for(int j=0; j<3; j++)
    //  {
    //  Color[j] = 0;
    //  }
 
    int times = 0;    
    
    for (int r = 0; r < img.width; r += 16) 
      {
      for (int c = 0; c < img.height; c += 16) 
        {
        color currentColor = img.get(r, c);
        Color[0] += (int) red(currentColor);
        Color[1] += (int) green(currentColor);
        Color[2] += (int) blue(currentColor);
        times++;
        }
      }
    Color[0] = Color[0]/times;
    Color[1] = Color[1]/times;
    Color[2] = Color[2]/times;
    cl.add(Color);
    printArray(cl.get(i));
    //println(hex(cl.get(i)[0]), hex(cl.get(i)[1]), hex(cl.get(i)[2]));
    }
  }

What version of Processing are you using?
What operating system?

:)