// info  nam="SAMPLE05C"  tit="カラーチャート"  dsc="既定パレットのHTMLチャート作成"  pro=2

typedef struct {
  BYTE  B;
  BYTE  G;
  BYTE  R;
  BYTE  A;
} RGBQ;

RGBC BGR2RGB(BGRC cr)
{ // R⇔B交代色(COLORREF→RGBQUAD変換)
  int t;

  t = ((RGBQ *)&cr)->R;
  ((RGBQ *)&cr)->R = ((RGBQ *)&cr)->B;
  ((RGBQ *)&cr)->B = t;
  return cr;
}

typedef struct {
  BYTE  R;
  BYTE  G;
  BYTE  B;
  BYTE  A;
} PALI;
  // 既定のパレット
PALI PAL[64] = {
  { 0x00, 0x00, 0x00, 'K' }, { 0x00, 0x00, 0xff, 'B' }, { 0xff, 0x00, 0x00, 'R' }, { 0xff, 0x00, 0xff, 'F' },
  { 0x00, 0xff, 0x00, 'L' }, { 0x00, 0xff, 0xff, 'A' }, { 0xff, 0xff, 0x00, 'Y' }, { 0xff, 0xff, 0xff, 'W' },
  { 0xa0, 0xa0, 0xa0, 's' }, { 0x00, 0x00, 0xc0, 'b' }, { 0xc0, 0x00, 0x00, 'r' }, { 0xc0, 0x00, 0xc0, 'f' },
  { 0x00, 0xc0, 0x00, 'l' }, { 0x00, 0xc0, 0xc0, 'a' }, { 0xc0, 0xc0, 0x00, 'y' }, { 0xf0, 0xf0, 0xf0, ' ' },
  { 0x69, 0x69, 0x69, 'D' }, { 0x00, 0x00, 0x80, 'N' }, { 0x80, 0x00, 0x00, 'M' }, { 0x80, 0x00, 0x80, 'P' },
  { 0x00, 0x80, 0x00, 'G' }, { 0x00, 0x80, 0x80, 'T' }, { 0x80, 0x80, 0x00, 'O' }, { 0xff, 0xff, 0xff, 'H' },
  { 0xd0, 0xff, 0xe0, 'd' }, { 0x00, 0x00, 0x40, 'n' }, { 0x40, 0x00, 0x00, 'm' }, { 0x40, 0x00, 0x40, 'p' },
  { 0x00, 0x40, 0x00, 'g' }, { 0x00, 0x40, 0x40, 't' }, { 0x40, 0x40, 0x00, 'o' }, { 0xd0, 0xd0, 0xd0, 'h' },
  { 0x00, 0x78, 0xd7, 'E' }, { 0xc0, 0xd0, 0xff, 'U' }, { 0xff, 0xc0, 0xd0, 'I' }, { 0xff, 0xd0, 0xff, 'V' },
  { 0xd0, 0xff, 0xd0, 'J' }, { 0xd0, 0xff, 0xff, 'C' }, { 0xff, 0xff, 0xd0, 'Z' }, { 0xff, 0xe0, 0xc0, 'Q' },
  { 0xff, 0xff, 0xff, 'e' }, { 0x90, 0xa0, 0xff, 'u' }, { 0xff, 0x90, 0xa0, 'i' }, { 0xff, 0xa0, 0xff, 'v' },
  { 0xa0, 0xff, 0xa0, 'j' }, { 0xa0, 0xff, 0xff, 'c' }, { 0xff, 0xff, 0xa0, 'z' }, { 0xff, 0xb0, 0x90, 'q' },
  { 0xe3, 0xe3, 0xe3, 'S' }, { 0x00, 0x00, 0x00, '0' }, { 0x20, 0x20, 0x20, '1' }, { 0x40, 0x40, 0x40, '!' },
  { 0x50, 0x50, 0x50, '2' }, { 0x60, 0x60, 0x60, '#' }, { 0x70, 0x70, 0x70, '3' }, { 0x80, 0x80, 0x80, '$' },
  { 0x90, 0x90, 0x90, '4' }, { 0xa0, 0xa0, 0xa0, '%' }, { 0xb0, 0xb0, 0xb0, '5' }, { 0xc0, 0xc0, 0xc0, '6' },
  { 0xd0, 0xd0, 0xd0, '7' }, { 0xe0, 0xe0, 0xe0, '8' }, { 0xf0, 0xf0, 0xf0, '9' }, { 0xff, 0xff, 0xff, 'w' },
};

#define  CID_WINDOWTEXT     0
#define  CID_RED            2
#define  CID_WINDOW         7
#define  CID_3DSHADOW       8
#define  CID_3DFACE        15
#define  CID_3DDKSHADOW    16
#define  CID_3DHILIGHT     23
#define  CID_CELLPTR       24
#define  CID_INDEXPTR      31
#define  CID_HIGHLIGHT     32
#define  CID_HIGHLIGHTTEXT 40
#define  CID_3DLIGHT       48
#define  CID_BLACK         49
#define  CID_GRAY          55
#define  CID_WHITE         63
#define  CID_GRAYTEXT      16
#define  CID_INFOBK        38
#define  CID_APPWORKSPACE  58
#define  CID_ACTBORDER     16
#define  CID_INACTBORDER   60

#define  CID_TXT  CID_WINDOWTEXT
#define  CID_WIN  CID_WINDOW
#define  CID_3DS  CID_3DSHADOW
#define  CID_3DF  CID_3DFACE
#define  CID_3DD  CID_3DDKSHADOW
#define  CID_3DH  CID_3DHILIGHT
#define  CID_PTR  CID_CELLPTR
#define  CID_IDX  CID_INDEXPTR
#define  CID_HIL  CID_HIGHLIGHT
#define  CID_HIT  CID_HIGHLIGHTTEXT
#define  CID_3DL  CID_3DLIGHT
#define  CID_BLK  CID_BLACK
#define  CID_GRY  CID_GRAY
#define  CID_WHI  CID_WHITE
#define  CID_GTX  CID_GRAYTEXT
#define  CID_INF  CID_INFOBK
#define  CID_WRK  CID_APPWORKSPACE
#define  CID_ACT  CID_ACTBORDER
#define  CID_INA  CID_INACTBORDER

#define  Wr  0.299
#define  Wg  0.587
#define  Wb  0.114

BYTE BGR2VAL(BGRC cr)
{ // 輝度
  return (int)(Wr * ((PALI *)&cr)->R + Wg * ((PALI *)&cr)->G + Wb * ((PALI *)&cr)->B + 0.5);
}

BGRC BKG2TXT(BGRC cr)
{ // 背景色に合う文字色
  return (BGR2VAL(cr) <= 128)? 0xffffff: 0;
}

BGRC AppColor(int cid)
{ // 色(COLORREF)
  if ((UINT)cid >= NELEM(PAL)) return 0;
  return *(BGRC *)&PAL[cid] & 0xffffff;
}

char AppColorChar(int cid)
{ // 色を表す文字
  if ((UINT)cid >= NELEM(PAL)) return '*';
  return PAL[cid].A;
}

void CommandMain(HWND hwnd)
{ // カラーチャート
  int i, c;
  HANDLE hf;
  char *pt, bf[256], *nam = "COLOR.HTM";

  if (YesNoBox(hwnd, "カラーチャートを作成します。よろしいですか？", NULL) != IDYES) return;
  hf = open_file(nam, OP_WRI); if (!hf) return;
  write_line(hf, "<HTML>");
  write_line(hf, "<HEAD>");
  write_line(hf, "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html;CHARSET=Shift_JIS\">");
  write_line(hf, "<TITLE>COLOR</TITLE>");
  write_line(hf, "</HEAD>");
  write_line(hf, "");
  write_line(hf, "<BODY BGCOLOR=\"#777777\" TEXT=\"#000000\" LINK=\"#000000\" ALINK=\"#FF0000\" VLINK=\"#000000\">");
  write_line(hf, "");
  write_line(hf, "<CENTER>");
  write_line(hf, "");
  write_line(hf, "<TABLE WIDTH=800 CELLSPACING=4 CELLPADDING=4>");
  for (i = 0; i < NELEM(PAL); i++) {
    if (i % 8 == 0) write_line(hf, "<TR HEIGHT=70>");
    pt = bf;
    c = AppColor(i);
    pt += sprintf(pt, "<TD WIDTH=96 BGCOLOR=\"#%06X\">", BGR2RGB(c));
    pt += sprintf(pt, "<FONT SIZE=-1 COLOR=\"#%06X\">", BGR2RGB(BKG2TXT(c)));
    pt += sprintf(pt, "<B>%03d<BR>'%c'</B><BR>", i, AppColorChar(i));
    switch (i) {
    default: pt += sprintf(pt, "#%06X<BR>", BGR2RGB(c)); break;
    case CID_TXT: pt += sprintf(pt, "CID_TXT<BR>"); break;
    case CID_WIN: pt += sprintf(pt, "CID_WIN<BR>"); break;
    case CID_3DS: pt += sprintf(pt, "CID_3DS<BR>"); break;
    case CID_3DF: pt += sprintf(pt, "CID_3DF<BR>"); break;
    case CID_3DD: pt += sprintf(pt, "CID_3DD<BR>"); break;
    case CID_3DH: pt += sprintf(pt, "CID_3DH<BR>"); break;
    case CID_PTR: pt += sprintf(pt, "CID_PTR<BR>"); break;
    case CID_IDX: pt += sprintf(pt, "CID_IDX<BR>"); break;
    case CID_HIL: pt += sprintf(pt, "CID_HIL<BR>"); break; 
    case CID_HIT: pt += sprintf(pt, "CID_HIT<BR>"); break; 
    case CID_3DL: pt += sprintf(pt, "CID_3DL<BR>"); break;
    }
    pt += sprintf(pt, "%d,%d,%d</FONT></TD>", ((PALI *)&c)->R, ((PALI *)&c)->G, ((PALI *)&c)->B);
    write_line(hf, bf);
    if (i % 8 == 7) write_line(hf, "</TR>");
  }
  write_line(hf, "</TABLE>");
  write_line(hf, "");
  write_line(hf, "");
  write_line(hf, "</CENTER>");
  write_line(hf, "");
  write_line(hf, "</BODY>");
  write_line(hf, "</HTML>");
  close_file(hf);
  OkBox(hwnd, format("'%s'を作成しました。", nam), NULL);
}
