gfxtools/Color/src/Color.cpp
changeset 0 f58d6ec98e88
equal deleted inserted replaced
-1:000000000000 0:f58d6ec98e88
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *       
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #pragma warning (disable:4710)
       
    21 #include "ColorImp.h"
       
    22 #include <fstream.h>
       
    23 #include <stdio.h>
       
    24 #include <iostream.h>
       
    25 
       
    26 extern const char* gIntroText;
       
    27 extern const char* gMiddleText;
       
    28 extern const char* gEndText;
       
    29 
       
    30 TColorScheme* Color::CreateColorSchemeL(TSchemeId aScheme)
       
    31 	{
       
    32 	switch (aScheme)
       
    33 		{
       
    34 		case ENokiaBlue:
       
    35 			return new TNokiaBlue;
       
    36 			break;
       
    37 		case ENokiaGreen:
       
    38 			return new TNokiaGreen;
       
    39 			break;
       
    40 		case ENokiaPurple:
       
    41 			return new TNokiaPurple;
       
    42 			break;
       
    43         case ENokiaGrey:
       
    44 			return new TNokiaGrey;
       
    45 			break;
       
    46 		case ESchemeRed:
       
    47 			return new TSchemeRed;
       
    48 			break;
       
    49 		case ESchemePink:
       
    50 			return new TSchemePink;
       
    51 			break;
       
    52 		case ESchemeOrange:
       
    53 			return new TSchemeOrange;
       
    54 			break;
       
    55         case EPalette1:
       
    56 			return new TPalette1;
       
    57 			break;
       
    58         case EPalette2:
       
    59 			return new TPalette2;
       
    60 			break;
       
    61         case EPalette3:
       
    62 			return new TPalette3;
       
    63 			break;
       
    64         case EPalette4:
       
    65 			return new TPalette4;
       
    66 			break;
       
    67         case EPalette5:
       
    68 			return new TPalette5;
       
    69 			break;
       
    70         case EPalette6:
       
    71 			return new TPalette6;
       
    72 			break;
       
    73         case EPalette7:
       
    74 			return new TPalette7;
       
    75 			break;
       
    76         case EPalette8:
       
    77 			return new TPalette8;
       
    78 			break;
       
    79         case EPalette9:
       
    80 			return new TPalette9;
       
    81 			break;
       
    82         case EPalette10:
       
    83 			return new TPalette10;
       
    84 			break;
       
    85         case EPalette11:
       
    86 			return new TPalette11;
       
    87 			break;
       
    88         case EPalette12:
       
    89 			return new TPalette12;
       
    90 			break;
       
    91         case EPalette13:
       
    92 			return new TPalette13;
       
    93 			break;
       
    94 		}
       
    95 	return 0;
       
    96 	}
       
    97 
       
    98 void Color::CreateBmConvPalette(TSchemeId aScheme, char* aFilename)
       
    99 	{
       
   100 	cout << "Creating " << aFilename << endl;
       
   101 
       
   102 	TColorScheme* scheme = CreateColorSchemeL(aScheme);
       
   103 
       
   104 	ofstream out(aFilename);
       
   105 	if (out.is_open())
       
   106 		{
       
   107 		for (int i=0; i<256; i++)
       
   108 			{
       
   109 			char str[32];
       
   110 			sprintf(str, "0x%08X\n", scheme->Color(i));
       
   111 			out << str;
       
   112 			}
       
   113 		}
       
   114 	out.close();
       
   115 
       
   116 	delete scheme;
       
   117 	}
       
   118 
       
   119 void Color::CreatePaletteTable(TSchemeId aScheme, char* aPaletteName)
       
   120 	{
       
   121 	char buf[1024];
       
   122 	sprintf(buf, "\\s60\\palette\\inc\\%s.h", aPaletteName);
       
   123 	cout << "Creating " << buf << endl;
       
   124 
       
   125 	TColorScheme* scheme = CreateColorSchemeL(aScheme);
       
   126 
       
   127 	ofstream out(buf);
       
   128 	if (out.is_open())
       
   129 		{
       
   130 		sprintf(buf, gIntroText, aPaletteName, aPaletteName, aPaletteName, aPaletteName, aPaletteName);
       
   131 		out << buf;
       
   132 
       
   133 		int i;
       
   134 		for (i=0; i<256; i++)
       
   135 			{
       
   136 			char str[32];
       
   137 			sprintf(str, "\t0x%08X", scheme->Color(i));
       
   138 			out << str;
       
   139 			if (i == 255)
       
   140 				out << endl;
       
   141 			else
       
   142 				out << "," << endl;
       
   143 			}
       
   144 
       
   145 		out << gMiddleText;
       
   146 
       
   147 		for (i=0; i<4096; i++)
       
   148 			{
       
   149 			TRgb color = TRgb((i & 0x00f) * 17,((i & 0x0f0) * 17)>>4,((i & 0xf00) * 17)>>8);
       
   150 
       
   151 			int nearest = 0;
       
   152 			int distance = scheme->Color(0).Difference(color);
       
   153 
       
   154 			for (int paletteIndex = 1; paletteIndex < 256; paletteIndex++)
       
   155 				{
       
   156 				TRgb paletteColor = scheme->Color(paletteIndex);
       
   157 
       
   158 				if (paletteColor == color)
       
   159 					{
       
   160 					nearest = paletteIndex;
       
   161 					break;
       
   162 					}
       
   163 
       
   164 				int paletteDistance = paletteColor.Difference(color);
       
   165 				if (paletteDistance < distance)
       
   166 					{
       
   167 					nearest = paletteIndex;
       
   168 					distance = paletteDistance;
       
   169 					}
       
   170 				}
       
   171 
       
   172 			if (i%16 == 0)
       
   173 				out << "\t";
       
   174 			else
       
   175 				out << " ";
       
   176 
       
   177 			char str[32];
       
   178 			sprintf(str, "0x%02X", nearest);
       
   179 			out << str;
       
   180 
       
   181 			if (i<4095)
       
   182 				out << ",";
       
   183 			if (i%16 == 15)
       
   184 				out << endl;
       
   185 			}
       
   186 
       
   187 		out << gEndText;
       
   188 		}
       
   189 	out.close();
       
   190 
       
   191 	delete scheme;
       
   192 	}
       
   193 
       
   194 void Color::EquivalenceTest()
       
   195 	{
       
   196 	const int KNumSchemes = 6;
       
   197 
       
   198 	TEquivalenceList list[KNumSchemes];
       
   199 	for (int s=0; s<KNumSchemes; s++)
       
   200 		MakeEqivalenceList((Color::TSchemeId)s, list[s]);
       
   201 
       
   202 	for (int i=0; i<256; i++)
       
   203 		{
       
   204 		for (int s=0; s<KNumSchemes; s++)
       
   205 			{
       
   206 			if (list[0][i] != list[s][i])
       
   207 				{
       
   208 				cout << "Palette equivalence failure at " << s << "[" << i << "]";
       
   209 				cout << " " << list[0][i] << "!=" << list[s][i];
       
   210 				cout << endl;
       
   211 				}
       
   212 			}
       
   213 		}
       
   214 	}
       
   215 
       
   216 void Color::MakeEqivalenceList(TSchemeId aScheme, TEquivalenceList& aList)
       
   217 	{
       
   218 	TColorScheme* scheme = CreateColorSchemeL(aScheme);
       
   219 
       
   220 	for (int i=0; i<256; i++)
       
   221 		{
       
   222 		for (int j=0; j<256; j++)
       
   223 			{
       
   224 			if (scheme->Color(i) == scheme->Color(j))
       
   225 				{
       
   226 				aList[i] = j;
       
   227 				break;
       
   228 				}
       
   229 			}
       
   230 		}
       
   231 
       
   232 	delete scheme;
       
   233 	}
       
   234 
       
   235 // End of File