I was developing a simple console application, where you enter the hex code of an ARGB value, and the application will tell you, out of 255, the Red, Green, Blue, and Alpha contents of the value.
For example:
Enter a 32-bit RGBA color value in hexadecimal (e.g. FF7F3300): FF7F3300
Your color contains:
255 of 255 red
127 of 255 green
51 of 255 blue
0 of 255 alpha
Theoretically, that is how the application should work. Yet it doesn't. It displays 0 for every colour.
I used the same code that I used for this application in a C# application, except syntax-adjusted to fit the language, and it worked properly.
However, due to my lack of C++ knowledge, I am unable to properly debug this application, even after half an hour of thought.
My C++ code that I used for this is as follows:
#include "stdafx.h" //Always imported for VisualStudio
#include <iostream> //For input/output operations
using namespace std; //To simplify code - no conflicting namespaces are being used, and yet the std namespace is used often
void introduce()
{
cout << "Welcome to 'ARGB to Decimal'! This program will take a hex ARGB colour value (such as FF7F3300) and output the colour parameters that this value stores.";
}
uint32_t getValue()
{
cout << "\n\nPlease enter an ARGB value: ";
uint32_t value;
cin >> hex >> value;
cout << hex << "You have selected an ARGB value of " << value;
return value; //Converted to hex due to bitwise operations that will be performed on this value
}
int main()
{
introduce();
uint32_t ARGBValue{ getValue() };
uint32_t bitComparison{ 0xFF000000 }; //Used as the right operand of a bitwise AND operator to single out the bits for each byte of the ARGB value (with its bits being shifted 8 bits to the right before the 2nd, 3rd, and 4th comparison, and so display the appropriate byte value for that parameter
cout << "\n\nThe selected value has the following parameter values (out of 255):\n- Alpha:\t"
<< ((ARGBValue & bitComparison) >> 24)
<< "\n- Red:\t\t" << ((ARGBValue & (bitComparison >>= 8)) >> 16)
<< "\n- Green:\t" << ((ARGBValue & (bitComparison >>= 8)) >> 8)
<< "\n- Blue:\t\t" << (ARGBValue & (bitComparison >>= 8))
<< "\n\n";
system("pause");
return 0;
}
This singles out the bits for each parameter in the ARGB value by using the bitwise AND operator with the parameter value, and a bitComparison value, which has a set of 8 bits turned on at the respective location at which the bits for the current parameter being singled out are located.
The bitComparison bits are shifted within the cout
statement, as it is being executed.
I would appreciate it if someone could tell me how to fix this.
Here is the sample output, which doesn't work:
Welcome to 'ARGB to Decimal'! This program will take a hex ARGB colour value (such as FF7F3300) and output the colour parameters that this value stores.
Please enter an ARGB value: FF7F3300
You have selected an ARGB value of ff7f3300
The selected value has the following parameter values (out of 255):
- Alpha: 0
- Red: 0
- Green: 0
- Blue: 0
Press any key to continue . . .
This compile fine and give the expected output (Compiled with g++ c++11 support):
#include <iostream> //For input/output operations
#include <cstdint>
void introduce()
{
std::cout << "Welcome to 'ARGB to Decimal'! This program will take a hex ARGB colour value (such as FF7F3300) and output the colour parameters that this value stores.";
}
uint32_t getValue()
{
std::cout << "\n\nPlease enter an ARGB value: ";
std::uint32_t value;
std::cin >> std::hex >> value;
std::cout << std::hex << "You have selected an ARGB value of " << value;
return value; //Converted to hex due to bitwise operations that will be performed on this value
}
int main()
{
introduce();
std::uint32_t ARGBValue{ getValue() };
std::cout << std::dec << "\n\nThe selected value has the following parameter values (out of 255):\n- Alpha:\t"
<< ((ARGBValue >> 24) & 0xFF)
<< "\n- Red:\t\t"
<< ((ARGBValue >> 16) & 0xFF)
<< "\n- Green:\t"
<< ((ARGBValue >> 8) & 0xFF)
<< "\n- Blue:\t\t"
<< (ARGBValue & 0xFF)
<< "\n\n";
return 0;
}