/*<Header>==============================================================
*
*	FILE MANAGER / "EIN_PAL.C"
*
*		[ EIN(tm) project : パレット操作補助関数群 ]
*
*	COPYRIGHT  Nam  1994, All rights reserved.
*
*-----------------------------------------------------------------------
*	V1.0L01α	94.07.06/Nam	プロトタイプ
*	V1.0L01β	94.08.31/Nam	32K色での色ずれ修正
*</Header>==============================================================*/
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<snd.h>
#include	<winb.h>
#include	<te.h>
#include	<fntb.h>
#include	<gui.h>
#include	<egb.h>
#include	<msdos.cf>
#include	<loader.h>
#include	<math.h>
#include	<io.h>
#include	<guidbg.h>

#define	EIN_PAL
#include	"..\eintm.h"

#ifndef NOERR
#define	NOERR	(0)
#endif
#ifndef ERROR
#define	ERROR	(1)
#endif

/*<Func>================================================================
*	int	EIN_initGuiColor()
*
*	【概  要】	GUIパレットテーブルをメニュー色に合わせる
*
*	【機  能】	
*
*	【入  力】	なし
*
*	【出  力】	なし
*
*	【関数値】	== 0		正常終了
*
*	【注  意】	初期化時に一度だけ実行してください。
*				タイミングは'MMI_Init()'の後で、かつGUI部品を表示する前です。
*
*	【参  照】	
*
*-----------------------------------------------------------------------
*	V11L10	1994.07.06/Nam
*</Func>==============================================================*/
int	EIN_initGuiColor()
{
	static	int		ct32k[16] = {
//		0x00000000, 0x5caf5caf, 0x02200220, 0x02310231,
//		0x63186318, 0x640e640e, 0x63186318, 0x42104210,
//		0x04210421, 0x76377637, 0x03e003e0, 0x03ff03ff,
//		0x7c007c00, 0x7c1f7c1f, 0x07fa07fa, 0x7fff7fff
		0x00000000, 0x3cb73cb7, 0x37293729, 0x67f567f5,
		0x4e734e73, 0x640f640f, 0x67396739, 0x3def3def,
		0x14a514a5, 0x5e3b5e3b, 0x03600360, 0x00150015,
		0x2d6b2d6b, 0x7c1f7c1f, 0x6fe06fe0, 0x7fff7fff
	};
	static	int		ct16m[16] = {
		0x00000000, 0x00b02070, 0x0040c060, 0x00a0f0c0,
		0x00909090, 0x007000c0, 0x00c0c0c0, 0x00707070,
		0x00202020, 0x00e080b0, 0x0000d000, 0x00a00000,
		0x00505050, 0x00f000f0, 0x0000f0d0, 0x00ffffff
	};

	typedef struct __PALVAL{
	    unsigned int    colorNo;        /* 読み取ったメニュー色番号         */
	    char            blue;           /* 青の階調(0〜255)                 */
	    char            red;            /* 赤の階調(0〜255)                 */
	    char            green;          /* 緑の階調(0〜255)                 */
	    char            eop;            /* データ終了(0)                    */
	}_PALVAL;
	typedef struct __MENUPAL{
	    unsigned int    maxcolor;       /* 読み取ったメニュー色の総数       */
	    _PALVAL         c[16];          /* パレットデータ                   */
	}_MENUPAL;
	
	_MENUPAL		pal;
	unsigned int	*tbl;
	register int	i, j, ret;

	//	メニュー色を取得
	pal.maxcolor = 0;
	ret = EGB_getTmenuPalette( &pal ) ;
	#ifdef DEBUG
	printf("EIN_setGuiColor: ret=%d,  palmax=%d\n", ret, pal.maxcolor);
	#endif
	
	// 256色の場合
	tbl = MG_getColorTable( 1 ) ;	/* 256 */
	for( i = 0 ; i < 16 ; i++ ) {
		tbl[i] = i + (i << 8) + (i<<16) + (i << 24 ) ;
	}
	
	// 32kの場合
	tbl = MG_getColorTable( 2 ) ;	/* 32k */
	for( i=0; i<16; i++ ){
		tbl[i] = ct32k[i];
	}
	
	// 16Mの場合
	tbl = MG_getColorTable( 3 ) ;	/* 16M */
	for( i=0; i<16; i++ ){
		tbl[i] = ct16m[i];
	}
	
	return NOERR;
}
