00001 #include "flGlobal.h"
00002 #if FL_COLOR != 0
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005
00006 #if FL_INCLUDE_ALL_C == 0
00007 #include "flColor.h"
00008 #include "flMath.h"
00009 #endif
00010
00011 u32 colorHSL(u8 inHue, u8 inSat, u8 inLum) {
00012 if(!inSat)
00013 return color8888_RGB(inLum, inLum, inLum);
00014 int tempQ;
00015 if(inLum < 128)
00016 tempQ = inLum * (256 + inSat);
00017 else
00018 tempQ = (inLum + inSat) - (inLum * inSat);
00019 int tempP = (512 * inLum) - tempQ;
00020 int tempOut[3];
00021 tempOut[0] = inHue + 85;
00022 tempOut[1] = inHue;
00023 tempOut[2] = inHue - 85;
00024 int i;
00025 for(i = 0; i < 3; i++) {
00026 while(tempOut[i] < 0)
00027 tempOut[i] += 256;
00028 while(tempOut[i] >= 256)
00029 tempOut[i] -= 256;
00030 if(tempOut[i] < 43) {
00031 tempOut[i] = tempP + ((tempQ - tempP) * 6 * tempOut[i]);
00032 } else if(tempOut[i] < 128) {
00033 tempOut[i] = tempQ;
00034 } else if(tempOut[i] < 170) {
00035 tempOut[i] = tempP + ((tempQ - tempP) * (170 - tempOut[i]) * 6);
00036 } else {
00037 tempOut[i] = tempP;
00038 }
00039 }
00040 return color8888_RGB(tempOut[0], tempOut[1], tempOut[2]);
00041 }
00042
00043 u8 colorHue8888(u32 inColor) {
00044 if(max(max(color8888_Red(inColor), color8888_Green(inColor)), color8888_Blue(inColor)) == min(min(color8888_Red(inColor), color8888_Green(inColor)), color8888_Blue(inColor)))
00045 return 0;
00046 int tempHue = 0;
00047 if(color8888_Red(inColor) > max(color8888_Green(inColor), color8888_Blue(inColor))) {
00048 if(color8888_Green(inColor) >= color8888_Blue(inColor)) {
00049 tempHue = (color8888_Green(inColor) - color8888_Blue(inColor));
00050 tempHue /= (max(max(color8888_Red(inColor), color8888_Green(inColor)), color8888_Blue(inColor)) - min(min(color8888_Red(inColor), color8888_Green(inColor)), color8888_Blue(inColor)));
00051 tempHue /= 6;
00052 } else {
00053 tempHue = ((int)color8888_Green(inColor) - (int)color8888_Blue(inColor));
00054 tempHue /= (max(max(color8888_Red(inColor), color8888_Green(inColor)), color8888_Blue(inColor)) - min(min(color8888_Red(inColor), color8888_Green(inColor)), color8888_Blue(inColor)));
00055 tempHue /= 6;
00056 tempHue += 256;
00057 }
00058 } else if(color8888_Green(inColor) > max(color8888_Red(inColor), color8888_Blue(inColor))) {
00059 tempHue = ((int)color8888_Blue(inColor) - color8888_Red((int)inColor));
00060 tempHue /= (max(max(color8888_Red(inColor), color8888_Green(inColor)), color8888_Blue(inColor)) - min(min(color8888_Red(inColor), color8888_Green(inColor)), color8888_Blue(inColor)));
00061 tempHue /= 6;
00062 tempHue += 85;
00063 } else {
00064 tempHue = ((int)color8888_Red(inColor) - color8888_Green((int)inColor));
00065 tempHue /= (max(max(color8888_Red(inColor), color8888_Green(inColor)), color8888_Blue(inColor)) - min(min(color8888_Red(inColor), color8888_Green(inColor)), color8888_Blue(inColor)));
00066 tempHue /= 6;
00067 tempHue += 170;
00068 }
00069 return (tempHue & 0xFF);
00070 }
00071
00072 u8 colorSat8888(u32 inColor) {
00073 u32 tempSat = 0;
00074 if(color8888_Red(inColor) > color8888_Green(inColor))
00075 tempSat = color8888_Red(inColor) - color8888_Green(inColor);
00076 else
00077 tempSat = color8888_Red(inColor) - color8888_Green(inColor);
00078 if(color8888_Green(inColor) > color8888_Blue(inColor))
00079 tempSat = color8888_Green(inColor) - color8888_Blue(inColor);
00080 else
00081 tempSat = color8888_Blue(inColor) - color8888_Green(inColor);
00082 if(color8888_Blue(inColor) > color8888_Red(inColor))
00083 tempSat = color8888_Blue(inColor) - color8888_Red(inColor);
00084 else
00085 tempSat = color8888_Red(inColor) - color8888_Blue(inColor);
00086 return ((tempSat / 3) & 0x000000FF);
00087 }
00088
00089 u8 colorLum8888(u32 inColor) {
00090 return ((max(max(color8888_Red(inColor), color8888_Green(inColor)), color8888_Blue(inColor)) >> 1) + (min(min(color8888_Red(inColor), color8888_Green(inColor)), color8888_Blue(inColor)) >> 1));
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 u32 colorMergeN8888(u32* inColor, u32 inColorCount) {
00105 u32 tempRed = 0, tempGreen = 0, tempBlue = 0, tempAlpha = 0;
00106 u8* tempPtr = (u8*)inColor;
00107 u32 i;
00108 for(i = 0; i < inColorCount; i++, tempPtr += 4) {
00109 tempRed += tempPtr[0];
00110 tempGreen += tempPtr[1];
00111 tempBlue += tempPtr[2];
00112 tempAlpha += tempPtr[3];
00113 }
00114 tempRed /= inColorCount;
00115 tempGreen /= inColorCount;
00116 tempBlue /= inColorCount;
00117 tempAlpha /= inColorCount;
00118
00119 return color8888_RGBA(tempRed, tempGreen, tempBlue, tempAlpha);
00120 }
00121
00122 u16 colorMergeN4444(u16* inColor, u16 inColorCount) {
00123 u32 tempRed = 0, tempGreen = 0, tempBlue = 0, tempAlpha = 0;
00124 u32 i;
00125 for(i = 0; i < inColorCount; i++) {
00126 tempRed += color4444_Red(inColor[i]);
00127 tempGreen += color4444_Green(inColor[i]);
00128 tempBlue += color4444_Blue(inColor[i]);
00129 tempAlpha += color4444_Alpha(inColor[i]);
00130 }
00131 tempRed /= inColorCount;
00132 tempGreen /= inColorCount;
00133 tempBlue /= inColorCount;
00134 tempAlpha /= inColorCount;
00135
00136 return color4444_RGBA(tempRed, tempGreen, tempBlue, tempAlpha);
00137 }
00138
00139 u16 colorMergeN5551(u16* inColor, u16 inColorCount) {
00140 u32 tempRed = 0, tempGreen = 0, tempBlue = 0, tempAlpha = 0;
00141 u32 i;
00142 for(i = 0; i < inColorCount; i++) {
00143 tempRed += color5551_Red(inColor[i]);
00144 tempGreen += color5551_Green(inColor[i]);
00145 tempBlue += color5551_Blue(inColor[i]);
00146 tempAlpha += color5551_Alpha(inColor[i]);
00147 }
00148 tempRed /= inColorCount;
00149 tempGreen /= inColorCount;
00150 tempBlue /= inColorCount;
00151 tempAlpha /= inColorCount;
00152
00153 return color5551_RGBA(tempRed, tempGreen, tempBlue, tempAlpha);
00154 }
00155
00156 u16 colorMergeN565(u16* inColor, u16 inColorCount) {
00157 u32 tempRed = 0, tempGreen = 0, tempBlue = 0;
00158 u32 i;
00159 for(i = 0; i < inColorCount; i++) {
00160 tempRed += color565_Red(inColor[i]);
00161 tempGreen += color565_Green(inColor[i]);
00162 tempBlue += color565_Blue(inColor[i]);
00163 }
00164 tempRed /= inColorCount;
00165 tempGreen /= inColorCount;
00166 tempBlue /= inColorCount;
00167
00168 return color565_RGB(tempRed, tempGreen, tempBlue);
00169 }
00170
00171 u32 colorRandom8888(u8 inBaseLum, u8 inRandLum, u8 inAlpha) {
00172 #if FL_MATH != 0
00173 u32 tempOut = ((inBaseLum + mathRandi(inRandLum)) + ((inBaseLum + mathRandi(inRandLum)) << 8) + ((inBaseLum + mathRandi(inRandLum)) << 16)) | (inAlpha << 24);
00174 return tempOut;
00175 #else
00176 u32 tempOut = (inBaseLum + ((rand() * inRandLum) / RAND_MAX));
00177 tempOut += ((inBaseLum + ((rand() * inRandLum) / RAND_MAX)) << 8);
00178 tempOut += ((inBaseLum + ((rand() * inRandLum) / RAND_MAX)) << 16);
00179 tempOut |= (inAlpha << 24);
00180 return tempOut;
00181 #endif
00182 }
00183
00184
00185 u32 colorLumMult8888(u32 inColor, float inLum) {
00186 inLum = pos(inLum);
00187 u32 tempOut = (min((int)((float)color8888_Red(inColor) * inLum), 255));
00188 tempOut += ((min((int)((float)color8888_Green(inColor) * inLum), 255)) << 8);
00189 tempOut += ((min((int)((float)color8888_Blue(inColor) * inLum), 255)) << 16);
00190 tempOut |= (inColor & 0xFF000000);
00191 return tempOut;
00192 }
00193
00194 u32 colorAlphaMult8888(u32 inColor, float inMult) {
00195 u32 tempOut = (min((int)((float)color8888_Alpha(inColor) * inMult), 255) << 24);
00196 tempOut = ((inColor & 0x00FFFFFF) | tempOut);
00197 return tempOut;
00198 }
00199
00200 #endif