001package armyc2.c2sd.renderer.utilities;
002
003import android.graphics.Canvas;
004import android.graphics.Paint;
005import android.graphics.Paint.Style;
006import android.util.SparseArray;
007
008import java.util.logging.Level;
009
010public class RendererUtilities {
011        
012        private static SparseArray<Color> pastIdealOutlineColors = new SparseArray<Color>();
013        /**
014     * 
015     * @param {String} color like "#FFFFFF"
016     * @returns {String}
017     */
018    public static Color getIdealOutlineColor(Color color){
019        Color idealColor = Color.white;
020        
021        if(color != null && pastIdealOutlineColors.indexOfKey(color.toInt())>=0)
022        {
023            return pastIdealOutlineColors.get(color.toInt());
024        }//*/
025        
026        if(color != null)
027        {
028                
029                int threshold = RendererSettings.getInstance().getTextBackgroundAutoColorThreshold();
030                        
031            int r = color.getRed();
032            int g = color.getGreen();
033            int b = color.getBlue();
034        
035            float delta = ((r * 0.299f) + (g * 0.587f) + (b * 0.114f));
036            
037            if((255 - delta < threshold))
038            {
039                idealColor = Color.black;
040            }
041            else
042            {
043                idealColor = Color.white;
044            }
045        }
046        
047        if(color != null)
048                pastIdealOutlineColors.put(color.toInt(),idealColor);
049        
050        return idealColor;
051    }
052    
053    public static void renderSymbolCharacter(Canvas ctx, String symbol, int x, int y, Paint paint, Color color, int outlineWidth)
054    {
055        int tbm = RendererSettings.getInstance().getTextBackgroundMethod();
056
057        Color outlineColor = RendererUtilities.getIdealOutlineColor(color);
058
059        //if(tbm == RendererSettings.TextBackgroundMethod_OUTLINE_QUICK)
060        //{    
061            //draw symbol outline
062                paint.setStyle(Style.FILL);
063
064                paint.setColor(outlineColor.toInt());
065            if(outlineWidth > 0)
066            {
067                for(int i = 1; i <= outlineWidth; i++)
068                {
069                        if(i % 2 == 1)
070                        {
071                                ctx.drawText(symbol, x - i, y, paint);
072                        ctx.drawText(symbol, x + i, y, paint);
073                        ctx.drawText(symbol, x, y + i, paint);
074                        ctx.drawText(symbol, x, y - i, paint);
075                        }
076                        else
077                        {
078                                ctx.drawText(symbol, x - i, y - i, paint);
079                        ctx.drawText(symbol, x + i, y - i, paint);
080                        ctx.drawText(symbol, x - i, y + i, paint);
081                        ctx.drawText(symbol, x + i, y + i, paint);
082                        }
083                        
084                }
085                
086            }
087            //draw symbol
088            paint.setColor(color.toInt());
089            
090                ctx.drawText(symbol, x, y, paint);
091            
092        /*}
093        else
094        {
095            //draw text outline
096                paint.setStyle(Style.STROKE);
097                paint.setStrokeWidth(RendererSettings.getInstance().getTextOutlineWidth());
098                paint.setColor(outlineColor.toInt());
099            if(outlineWidth > 0)
100            {
101                
102                ctx.drawText(symbol, x, y, paint);
103                
104            }
105            //draw text
106            paint.setColor(color.toInt());
107            paint.setStyle(Style.FILL);
108            
109                ctx.drawText(symbol, x, y, paint);
110        }//*/     
111    }
112
113    public static String colorToHexString(Color color, Boolean withAlpha)
114    {
115        if(color != null)
116        {
117            String hex = color.toHexString();
118            hex = hex.toUpperCase();
119            if(withAlpha)
120                return "#" + hex;
121            else
122                return "#" + hex.substring(2);
123        }
124        return null;
125    }
126
127    /**
128     *
129     * @param hexValue - String representing hex value (formatted "0xRRGGBB"
130     * i.e. "0xFFFFFF") OR formatted "0xAARRGGBB" i.e. "0x00FFFFFF" for a color
131     * with an alpha value I will also put up with "RRGGBB" and "AARRGGBB"
132     * without the starting "0x"
133     * @return
134     */
135    public static Color getColorFromHexString(String hexValue)
136    {
137        try
138        {
139            if(hexValue==null || hexValue.isEmpty())
140                return null;
141            String hexOriginal = hexValue;
142
143            String hexAlphabet = "0123456789ABCDEF";
144
145            if (hexValue.charAt(0) == '#')
146            {
147                hexValue = hexValue.substring(1);
148            }
149            if (hexValue.substring(0, 2).equals("0x") || hexValue.substring(0, 2).equals("0X"))
150            {
151                hexValue = hexValue.substring(2);
152            }
153
154            hexValue = hexValue.toUpperCase();
155
156            int count = hexValue.length();
157            int[] value = null;
158            int k = 0;
159            int int1 = 0;
160            int int2 = 0;
161
162            if (count == 8 || count == 6)
163            {
164                value = new int[(count / 2)];
165                for (int i = 0; i < count; i += 2)
166                {
167                    int1 = hexAlphabet.indexOf(hexValue.charAt(i));
168                    int2 = hexAlphabet.indexOf(hexValue.charAt(i + 1));
169
170                    if(int1 == -1 || int2 == -1)
171                    {
172                        ErrorLogger.LogMessage("RendererUtilities", "getColorFromHexString", "Bad hex value: " + hexOriginal, Level.WARNING);
173                        return null;
174                    }
175
176                    value[k] = (int1 * 16) + int2;
177                    k++;
178                }
179
180                if (count == 8)
181                {
182                    return new Color(value[1], value[2], value[3], value[0]);
183                }
184                else if (count == 6)
185                {
186                    return new Color(value[0], value[1], value[2]);
187                }
188            }
189            else
190            {
191                ErrorLogger.LogMessage("RendererUtilities", "getColorFromHexString", "Bad hex value: " + hexOriginal, Level.WARNING);
192            }
193            return null;
194        }
195        catch (Exception exc)
196        {
197            ErrorLogger.LogException("RendererUtilities", "getColorFromHexString", exc);
198            return null;
199        }
200    }
201
202}