#RGB TO HEXA & HEXA TO RGB C++

1 messages · Page 1 of 1 (latest)

hollow gale
#

hi guys, i need help to convert from rgb to hexadecimal and from hexadecimal to rgb in c++, can someone help me? "Design a COLOR object with whatever methods and attributes you think are relevant.
Allow your data to be loaded in RGB or HEX, and return both as well" thats the statement

upbeat bobcatBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

wide willowBOT
#
dot
Just Post Your Question

Don't ask to ask a question, don't ask if anyone is familiar with XYZ. Just ask your question.

Someone is bound to know the answer and will be happy to help.

Learn why asking to ask is not a good idea

hollow gale
#

"Design a COLOR object with whatever methods and attributes you think are relevant.
Allow your data to be loaded in RGB or HEX, and return both as well" thats the statement

olive solar
#

so what exactly do you expect as a response to that?

#

that we just do your homework for you? 😛

hollow gale
#

nope, i have that code i maded

#

/*
Diseñe un objeto COLOR con los métodos y atributos que considere pertinente.
Permita que se puedan cargar sus datos en RGB o HEXADECIMAL, y que devuelva ambos también
*/

#include <iostream>
#include <sstream>
using namespace std;

/*
Transformar RGB a HEXADECIMAL
*/

string rgbahexa(int r, int g, int b, bool a = false);
string rgbahexa(int r, int g, int b, bool a)
{
stringstream ss;
if (a)
ss << "#";
ss << hex << (r << 16 | g << 8 | b);
return ss.str();
}

int main(int argc, char *argv[]) {

int r,g,b;
cout << "Ingrese el valor RGB en este orden (Ejemplo: 22 10 22): ";
cin >> r >> g >> b;
cout << rgbahexa(r,b,g,true) << endl;

return 0;

}

#

im spanish btw

#

thats code works for rgb to hexa, but in a wrong way

wide willowBOT
#
How to Format Code on Discord

• type three "backticks" (not quotes/apostrophes, on QWERTY layout, left of 1-key)
• on the same line, type the file extension for that language (c or cpp)
• enter your code on a new line and put another three backticks at the end

Example Input

```cpp
int main() {
    return 0;
}
```

Example Output
int main() {
    return 0;
} ```
olive solar
hollow gale
#

this result need to be showed #160A16 but this is showed "#16160a" i used a rgb to hexa converter and this is wrong

olive solar
#

well

#

what do you think happens when you >> an int from 160A16?

hollow gale
#

and I don't know very well how to go from hexa to rgb (it's also part of the task)

hollow gale
#

what happens?

olive solar
#

well what do you think happens?

#

your code is doing that

#
cin >> r >> g >> b;
#

what do you think this does?

#

i have to go afk for a while i'm afraid

hollow gale
#

oh, actually i solved it

#

i just changed

#

cin >> b >> g >> r;
cout << rgbahexa(b,g,r,true) << endl;

#

and worked

#

thanks a lot

#

I think it's fine, right?

#

now i need to convert from hexadecimal to rgb but i don't know well how to do it😢

#

/*
Diseñe un objeto COLOR con los métodos y atributos que considere pertinente.
Permita que se puedan cargar sus datos en RGB o HEXADECIMAL, y que devuelva ambos también
*/

#include <iostream>
#include <sstream>
using namespace std;

/*
Transformar RGB a HEXADECIMAL
*/

string rgbahexa(int r, int g, int b, bool a = false);
string rgbahexa(int r, int g, int b, bool a)
{
stringstream ss;
if (a)
ss << "#";
ss << hex << (r << 16 | g << 8 | b);
return ss.str();
}

int main(int argc, char *argv[]) {

int r,g,b;
cout << "Ingrese el valor RGB en este orden (Ejemplo: 22 10 22): ";
cin >> b >> g >> r;
cout << rgbahexa(b,g,r,true) << endl;

return 0;

}

#

that is my code to go from RGB to HEXA

upbeat bobcatBOT
#

@hollow gale Has your question been resolved? If so, run !solved :)

olive solar
#

I don't see how cin >> b >> g >> r would work here

upbeat bobcatBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.