summaryrefslogtreecommitdiff
path: root/android/source/src/java/org/libreoffice/ColorPickerAdapter.java
blob: 897084c9e607aa0024fb779c22972577c11897ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package org.libreoffice;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;


public class ColorPickerAdapter extends RecyclerView.Adapter<ColorPickerAdapter.ColorPickerViewHolder> {

    Context mContext;
    ColorPaletteAdapter colorPaletteAdapter;
    ColorPaletteListener colorPaletteListener;
    int[] colorList;
    int[][] colorPalette = new int[11][8];
    int selectedBox = 0;

    public ColorPickerAdapter(Context mContext, final ColorPaletteAdapter colorPaletteAdapter, ColorPaletteListener colorPaletteListener) {
        this.mContext = mContext;
        this.colorPaletteAdapter = colorPaletteAdapter;
        this.colorPaletteListener = colorPaletteListener;
        Resources r = mContext.getResources();
        this.colorList = r.getIntArray(R.array.fontcolors);
        initializeColorPalette();


    }

    @Override
    public ColorPickerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View item = LayoutInflater.from(mContext).inflate(R.layout.colorbox, parent, false);
        ColorPickerViewHolder holder = new ColorPickerViewHolder(item);
        return holder;

    }

    @Override
    public void onBindViewHolder(final ColorPickerViewHolder holder, final int position) {
        holder.colorBox.setBackgroundColor(colorList[position]);

        if (selectedBox != position)
            holder.colorBox.setImageDrawable(null);
        else {
            holder.colorBox.setImageResource(R.drawable.ic_done_white_12dp);
        }

        holder.colorBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setPosition(position);
                colorPaletteListener.applyColor(colorList[position]);
            }
        });


    }

    @Override
    public int getItemCount() {
        return colorList.length;
    }


    private void setPosition(int position) {
        this.selectedBox = position;
        selectSubColor(position, position==0?0:3);
        colorPaletteListener.applyColor(colorList[position]);
        updateAdapter();
    }

    private void selectSubColor(int position1, int position2) {
        colorPaletteAdapter.setPosition(position1, position2);
    }

    private void initializeColorPalette() {

        for (int i = 0; i < 11; i++) {
            int red = Color.red(colorList[i]);
            int green = Color.green(colorList[i]);
            int blue = Color.blue(colorList[i]);

            int red_tint = red;
            int green_tint = green;
            int blue_tint = blue;

            int red_shade = red;
            int green_shade = green;
            int blue_shade = blue;
            if (i != 0) {
                colorPalette[i][3] = colorList[i];
                for (int k = 2; k >= 0; k--) {
                    red_shade = (int) (red_shade * 0.75);
                    green_shade = (int) (green_shade * 0.75);
                    blue_shade = (int) (blue_shade * 0.75);
                    colorPalette[i][k] = (Color.rgb(red_shade, green_shade, blue_shade));
                }
                for (int k = 4; k < 7; k++) {
                    red_tint = (int) (red_tint + (255 - red_tint) * 0.45);
                    green_tint = (int) (green_tint + (255 - green_tint) * 0.45);
                    blue_tint = (int) (blue_tint + (255 - blue_tint) * 0.45);
                    colorPalette[i][k] = (Color.rgb(red_tint, green_tint, blue_tint));
                }
            } else {
                colorPalette[0][0] = colorList[i];
                for (int k = 1; k < 7; k++) {
                    red_tint = (int) (red_tint + (255 - red_tint) * 0.25);
                    green_tint = (int) (green_tint + (255 - green_tint) * 0.25);
                    blue_tint = (int) (blue_tint + (255 - blue_tint) * 0.25);
                    colorPalette[i][k] = (Color.rgb(red_tint, green_tint, blue_tint));
                }
            }
        }
        for (int i = 0; i < 11; i++){
            this.colorPalette[i][7] = (Color.rgb(255, 255, 255)); // last one is always white
        }
        colorPaletteAdapter.setColorPalette(colorPalette, 0, 0);
    }

    public void findSelectedTextColor(int color) {
        /*
            Libreoffice recognizes -1 as Black
         */
        if (color == -1) {
            colorPaletteAdapter.changePosition(0, 0);
            selectedBox = 0;
            updateAdapter();
            return;
        }
        /*
            Find the color if the palette points another color
         */
        if (colorPalette[selectedBox][colorPaletteAdapter.getSelectedBox()] != color) {
            for (int i = 0; i < 11; i++) {
                for (int k = 0; k < 8; k++) {
                    if (colorPalette[i][k] == color) {
                        colorPaletteAdapter.changePosition(i, k);
                        selectedBox = i;
                        updateAdapter();
                        return;
                    }
                }
            }
        }
    }
    private void updateAdapter(){
        LOKitShell.getMainHandler().post(new Runnable() {
            @Override
            public void run() {
                ColorPickerAdapter.this.notifyDataSetChanged();
            }
        });

    }

    class ColorPickerViewHolder extends RecyclerView.ViewHolder {

        ImageButton colorBox;

        public ColorPickerViewHolder(View itemView) {
            super(itemView);
            this.colorBox = (ImageButton) itemView.findViewById(R.id.fontColorBox);
        }
    }
}