본문 바로가기
Borland

HexStr을 TColor로 변경 하는 함수

by leo21c 2010. 4. 22.

TColor __fastcall TLayerForm::HexStrToTColor(AnsiString Hex)
{
  TColor color = 0;
  int length = StrLen(Hex.c_str());
  int value = 0;
  char ch1, ch2;
  unsigned char array[3] = {0};

  for (int i = 0, j = 0; i < length; i+=2, j++) {
    ch1 = Hex.c_str()[i];
    ch2 = Hex.c_str()[i+1];
    if(isdigit(ch1)) value = ch1 - '0';
    else if(ch1>='A' && ch1<='F') value = ch1 - 'A' + 10;
    else if(ch1>='a' && ch1<='f') value = ch1 - 'a' + 10;
    value *= 16;

    if(isdigit(ch2)) value += ch2 - '0';
    else if(ch2>='A' && ch2<='F') value += ch2 - 'A' + 10;
    else if(ch2>='a' && ch2<='f') value += ch2 - 'a' + 10;

    array[j] = value;
    value = 0;
  }
  color += array[0] << 16;
  color += array[1] << 8;
  color += array[2];
  return color;
}