Hey Guys,
i'm trying to convert a COLORREF to a Hex-CString and then back from Hex-CString into COLORREF and i've run into a small problem.
Here some Code for Converting from COLORREF to a CString (Hexadecimal):
This part isn't the problem.
COLORREF crefColor = RGB(128,0,0);
DWORD dwR = GetRValue(crefColor);
DWORD dwG = GetGValue(crefColor);
DWORD dwB = GetBValue(crefColor);
CString sValue;
sValue.Format(_T("#%02X%02X%02X"), dwR, dwG, dwB);
ASSERT(sValue == _T("#800000"));
Now the more annoying part.
Back from CString to COLORREF:
CString sValue(_T("#800000"));
LPCTSTR pszTmp = sValue;
pszTmp++;
COLORREF crefColor;
COLORREF crefColor2;
sscanf_s(pszTmp, "%x", &crefColor); crefColor2 = strtol(pszTmp, NULL, 16); ASSERT(crefColor == crefColor2);
INT nR = GetRValue(crefColor);
ASSERT(nR == 128);
INT nG = GetGValue(crefColor);
ASSERT(nG == 0);
INT nB = GetBValue(crefColor);
ASSERT(nB == 0);
Could anyone suggest me, why the RGB is reversed?
I mean, if i know this problem, i could fix it and simlpy reverse it back:
crefColor = RGB(nB, nG, nR);
But i think the result should be in the right direction after sscanf_s / strtol
Hope anyone could explain me the reason for this and show me the mistake i've done with my code.
Thank you so much guys for helping me!