I need help with some conversions

Hello !
I don't know the names of the procedures in English, because I haven't studied in English so I'll try and exemplify what code I have and what I need my code to do.

I have this:
1
2
3
4
5
6
7
struct Color {
  Color() : r(0), g(0), b(0) {}
  Color(uint8_t rr, uint8_t gg, uint8_t bb) : r(rr), g(gg), b(bb) {}
  uint8_t r;
  uint8_t g;
  uint8_t b;
};


and this:
1
2
3
4
5
6
7
8
static void Text_HasArrived(RGBMatrix *canvas, const char * line1, const char * line2, int spacing,
                            rgb_matrix::Color color1 = RED, rgb_matrix::Color color2 = RED) {
  canvas->Clear();
  canvas->SetBrightness(brightness);

  rgb_matrix::DrawText(canvas, font_Arial21, 10, font_Arial21.baseline()-5, color1, &BLACK, line1, spacing);
  rgb_matrix::DrawText(canvas, font_Arial20, -2, font_Arial20.baseline()*2-5, color2, &BLACK, line2, spacing);
}



I want to make a function that parses a std::string into a rgb_matrix::Color color(); in some way.

I have tried this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//Global Declarations
rgb_matrix::Color RED(255, 0, 0); 

rgb_matrix::Color textColor;
// rgb_matrix::Color textColor();
// rgb_matrix::Color textColor(int,int,int);
// rgb_matrix::Color textColor(0,0,0);

//void getRGB(std::string string, rgb_matrix::Color (*color)(int,int,int)) {
//   std::string rgb[3];
//   std::istringstream ss(string);
//   int i = 0;
//   do {
//     ss >> rgb[i++];
//   } while (ss);  

//  (*color)(std::stoi(rgb[0]),std::stoi(rgb[1]),std::stoi(rgb[2]));
//}

void getRGB(std::string string, rgb_matrix::Color color(int,int,int)) {
  std::string rgb[3];
  std::istringstream ss(string);
  int i = 0;
  do {
    ss >> rgb[i++];
  } while (ss);  

  color(std::stoi(rgb[0]),std::stoi(rgb[1]),std::stoi(rgb[2]));
}

int main() {
.
.
.
// I have my inputs/colors in a std::string array
getRGB(inputs[0],textColor)
Text_HasArrived(canvas, inputs[0],inputs[1],-2, textColor);
.
.
.
}



I always get an errors similar to:
1
2
3
no suitable constructor exists to convert from "rgb_matrix::Color (int, int, int)" to "rgb_matrix::Color"

argument of type "rgb_matrix::Color (*)()" is incompatible with parameter of type "rgb_matrix::Color (*)(int, int, int)"
Last edited on
After some tinkering I managed to fix it !

I declared my colors like this:

1
2
3
4
rgb_matrix::Color ArrivingIn_Color;
rgb_matrix::Color Counter_Color;
rgb_matrix::Color TheFerry_Color;
rgb_matrix::Color HasArrived_Color;


I've changed the getRGB() function like this:
1
2
3
4
5
6
7
8
9
10
11
void getRGB(std::string& string, rgb_matrix::Color &color) {
  std::string rgb[3];
  std::istringstream ss(string);
  int i = 0;
  do {
    ss >> rgb[i++];
  } while (ss);

  rgb_matrix::Color temp(std::stoi(rgb[0]),std::stoi(rgb[1]),std::stoi(rgb[2]));
  color = temp;
}


And it works perfectly:

1
2
3
4
5
if(inputs[2].length() != 0 && inputs[3].length() !=0) {
        getRGB(inputs[2], ArrivingIn_Color);
        getRGB(inputs[3], Counter_Color);
        TextLine_Counter(canvas, inputs[0], std::stoi(inputs[1]),ArrivingIn_Color,Counter_Color);
}

1
2
3
4
5
6
7
8
9
10
rgb_matrix::Color getRGB(const std::string& str) {
    int r, g, b;
    std::istringstream ss(str);
    if (!(ss >> r >> g >> b))
        ; // non-integer encountered or premature eof
    return rgb_matrix::Color(r, g, b);
}


ArrivingIn_Color = getRGB(inputs[2]);

Or to read directly from the input:

1
2
3
4
5
std::istream& getRGB(std::istream& in, rgb_matrix::Color& color) {
    return in >> color.r >> color.g >> color.b;
}

std::cin >> ArrivingIn_Color;

Last edited on
Topic archived. No new replies allowed.