package acr.browser.lightning.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Typeface;

public class DrawableUtils {

    public static Bitmap getRoundedNumberImage(int number, int width, int height, int color, int thickness) {
        String text;

        if (number > 99) {
            text = "\u221E";
        } else {
            text = String.valueOf(number);
        }

        Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(image);
        Paint paint = new Paint();
        paint.setColor(color);
        Typeface boldText = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
        paint.setTypeface(boldText);
        paint.setTextSize(Utils.dpToPx(14));
        paint.setAntiAlias(true);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

        int radius = Utils.dpToPx(2);

        RectF outer = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
        canvas.drawRoundRect(outer, radius, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

        radius--;
        RectF inner = new RectF(thickness, thickness, canvas.getWidth() - thickness, canvas.getHeight() - thickness);
        canvas.drawRoundRect(inner, radius, radius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

        int xPos = (canvas.getWidth() / 2);
        int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));

        canvas.drawText(String.valueOf(text), xPos, yPos, paint);

        return image;
    }


    public static int mixColor(float fraction, int startValue, int endValue) {
        int startInt = startValue;
        int startA = (startInt >> 24) & 0xff;
        int startR = (startInt >> 16) & 0xff;
        int startG = (startInt >> 8) & 0xff;
        int startB = startInt & 0xff;

        int endInt = endValue;
        int endA = (endInt >> 24) & 0xff;
        int endR = (endInt >> 16) & 0xff;
        int endG = (endInt >> 8) & 0xff;
        int endB = endInt & 0xff;

        return (startA + (int)(fraction * (endA - startA))) << 24 |
                (startR + (int)(fraction * (endR - startR))) << 16 |
                (startG + (int)(fraction * (endG - startG))) << 8 |
                (startB + (int)(fraction * (endB - startB)));
    }

}

@@ -31,16 +31,87 @@
 utils;%0A%0A
+import android.content.Context;%0Aimport android.content.res.TypedArray;%0A
 import a
@@ -341,16 +341,272 @@
 ypeface;
+%0Aimport android.graphics.drawable.Drawable;%0Aimport android.os.Build;%0Aimport android.support.annotation.AttrRes;%0Aimport android.support.annotation.NonNull;%0Aimport android.support.annotation.Nullable;%0Aimport android.util.TypedValue;%0Aimport android.view.View;
 %0A%0Apublic
@@ -2673,32 +2673,33 @@
  (startA + (int)
+ 
 (fraction * (end
@@ -2728,28 +2728,24 @@
             
-    
 (startR + (i
@@ -2739,32 +2739,33 @@
  (startR + (int)
+ 
 (fraction * (end
@@ -2794,28 +2794,24 @@
             
-    
 (startG + (i
@@ -2805,32 +2805,33 @@
  (startG + (int)
+ 
 (fraction * (end
@@ -2859,28 +2859,24 @@
             
- 
-   
 (startB + (i
@@ -2878,16 +2878,17 @@
  + (int)
+ 
 (fractio
@@ -2917,10 +2917,761 @@
 %0A    %7D%0A%0A
+    public static Drawable resolveDrawableAttribute(@NonNull Context context, @AttrRes int res) %7B%0A        int%5B%5D attribute = new int%5B%5D%7Bres%7D;%0A        int indexOfAttrTextSize = 0;%0A        TypedValue typedValue = new TypedValue();%0A        TypedArray a = context.obtainStyledAttributes(typedValue.data, attribute);%0A        Drawable drawable = a.getDrawable(indexOfAttrTextSize);%0A        a.recycle();%0A        return drawable;%0A    %7D%0A%0A    public static void setBackground(@NonNull View view, @Nullable Drawable drawable) %7B%0A        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.JELLY_BEAN) %7B%0A            view.setBackground(drawable);%0A        %7D else %7B%0A            //noinspection deprecation%0A            view.setBackgroundDrawable(drawable);%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Add support background setting