Skip to content

Commit 3e57f2d

Browse files
Merge pull request #33 from sdsmdg/customPaletteFeature
Custom Palette Feature and Bug Fixes
2 parents abdcc52 + 7524f57 commit 3e57f2d

21 files changed

+653
-197
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ If you're looking for something to work on, have a look at the open issues in th
4444
Trianglify is built keeping [MVP (model-view-presenter)](https://en.wikipedia.org/wiki/Model–view–presenter) architecture in mind, so any changes that are proposed to Trianglfiy should follow MVP architecture. If you are confused regarding where a method should be, join us at [MDG public chat room](https://mdg.sdslabs.co/chat), we'll be happy to help.
4545

4646
### Style Check
47-
Trianglify uses Sputnik for performing style checks on the codebase, which helps us in maintaining the quality of the code. Sputnik checks for violation upon submission of Pull Requests. If Sputnik reports a violation and you believe that it is not applicable, just comment `N/A` on sputnik review with the reason why it is not applicable. **Pull Requests will only be merged once all the violations are resolved**.
47+
Trianglify uses Sputnik for performing style checks on the codebase, which helps us in maintaining the quality of the code. Sputnik checks for violation upon submission of Pull Requests. If Sputnik reports a violation and you believe that it is not applicable, just comment `N/A` on the sputnik review with the reason of why it is not applicable. **Pull Requests will only be merged once all the violations are resolved**.
4848

4949
### Git Commit Messages
5050
* Use the present tense ("Add feature" not "Added feature")

DOCUMENTATION.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Welcome to the Trianglify documentation! Here's the ultimate guide to use Trianglify in your app.
1+
Welcome to the Trianglify documentation! Here's the ultimate guide to using Trianglify in your app.
22

33
# Documentation
44
## Contents
@@ -70,11 +70,12 @@ trianglifyView.setGridWidth(trianglifyView.getWidth())
7070
| Y-Axis Bleed | 0 px | .setBleedY(...) | bleed_y | TrianglifyView generates total area having height = gridWidth + 2*bleedY to avoid unfilled triangles at the edges of the view |
7171
| Variance | 10 px | .setVariance(...) | variance | Displacement of points from original grid position to create triangles of different sizes |
7272
| Cell Size | 40 px | .setCellSize(...) | cell_size | Size of cells of rectangular grid used to generated vertices of the triangles |
73-
| Grid Type* | 0 | .setGridType(...) | grid_type | Type of grid 0 for Rectangular |
73+
| Grid Type* | 0 | .setTypeGrid(...) | grid_type | Type of grid 0 for Rectangular |
7474
| Fill Triangles with color | true | .setFillTriangle(...) | fill_triangles | Fills the triangle generated with color chosen |
7575
| Draw strokes | false | .setDrawStrokes(...) | draw_strokes | Draws triangle's border with neighboring triangle's color |
7676
| Color Palette | YlGn | .setPalette(...) | palette | Set of existing colors to color triangles |
7777
| Random Coloring | false | .setRandomColoring(...) | random_coloring | If random coloring is on triangles will be colored randomly instead of linear interpolation |
78+
| Fill the View Completely | false | .setFillViewCompletely(...) | fillViewCompletely| If fillViewCompletely is true, then it will throw illegalArgumentsException whenever both `BleedX` and `BleedY` are not greater than `cellSize`. Refer to [Section 2.2](#2.2)
7879

7980
*Current release contains only one GridType accessible with id `0`
8081

@@ -90,10 +91,10 @@ trianglifyView.setGridWidth(trianglifyView.getWidth())
9091
<p>
9192

9293
### 2.2 Details of Bleed and Grid Dimensions
93-
* **Bleed:** Bleed defines the dimensions of extra size that TrianglifyView view generate so that triangles on the edge doesn't appear to be chopped off. In most of the cases `min{bleedX, bleedY} > cellSize` would ensure that the view is completely filled.
94+
* **Bleed:** Bleed defines the dimensions of extra size that TrianglifyView view generates so that triangles on the edge don't appear to be chopped off. In most of the cases `min{bleedX, bleedY} > cellSize` would ensure that the view is completely filled.
9495
* **Grid Dimensions:** `GridHeight` and `GridWidth` defines the dimensions of the visible area of the view.
9596

96-
Total area generated by TrianglifyView is (`gridHeight` + `BleedY`) * (`gridWidth` + `BleedX`) while total area visible is (`gridHeight` + `BleedY`) * (`gridWidth` + `BleedX`)
97+
Total area generated by TrianglifyView is (`gridHeight` + 2 * `BleedY`) * (`gridWidth` + 2 * `BleedX`) while total area visible is (`gridHeight`) * (`gridWidth`)
9798

9899
Following image demonstrates region covered by `gridHeight`, `gridWidth`, `bleedX` and `bleedY`
99100
<p>

app/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,7 @@ dependencies {
3434

3535
compile 'com.android.support:appcompat-v7:25.0.1'
3636
compile 'com.android.support.constraint:constraint-layout:1.0.2'
37+
compile 'com.github.QuadFlask:colorpicker:0.0.13'
3738
testCompile 'junit:junit:4.12'
38-
testCompile 'org.mockito:mockito-core:1.10.19'}
39+
testCompile 'org.mockito:mockito-core:1.10.19'
40+
}

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
<category android:name="android.intent.category.LAUNCHER" />
1616
</intent-filter>
1717
</activity>
18-
<activity android:name=".AboutActivity"></activity>
18+
<activity android:name=".AboutActivity" />
19+
<activity android:name=".CustomPalettePickerActivity"/>
1920
</application>
2021

21-
</manifest>
22+
</manifest>
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.sdsmdg.kd.trianglifyexample;
2+
3+
import android.content.Context;
4+
import android.content.DialogInterface;
5+
import android.content.Intent;
6+
import android.graphics.Color;
7+
import android.support.v7.app.ActionBar;
8+
import android.support.v7.app.AlertDialog;
9+
import android.support.v7.app.AppCompatActivity;
10+
import android.os.Bundle;
11+
import android.view.Menu;
12+
import android.view.MenuInflater;
13+
import android.view.MenuItem;
14+
import android.view.View;
15+
import android.widget.ImageView;
16+
17+
import com.flask.colorpicker.ColorPickerView;
18+
import com.flask.colorpicker.OnColorSelectedListener;
19+
import com.flask.colorpicker.builder.ColorPickerClickListener;
20+
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
21+
import com.sdsmdg.kd.trianglify.models.Palette;
22+
import com.sdsmdg.kd.trianglify.views.TrianglifyView;
23+
24+
public class CustomPalettePickerActivity extends AppCompatActivity {
25+
private Palette palette;
26+
private TrianglifyView trianglifyView;
27+
private Context context;
28+
private ImageView[] imageViews = new ImageView[9];
29+
private int[] colors = {Color.BLACK, Color.BLUE, Color.BLACK, Color.CYAN, Color.DKGRAY, Color.GREEN, Color.RED, Color.MAGENTA, Color.LTGRAY};
30+
public static final String CUSTOM_PALETTE_COLOR_ARRAY = "Custom Palette Color Array";
31+
32+
@Override
33+
protected void onCreate(Bundle savedInstanceState) {
34+
super.onCreate(savedInstanceState);
35+
setContentView(R.layout.activity_custom_palette_picker);
36+
37+
ActionBar actionBar = getSupportActionBar();
38+
actionBar.setDisplayHomeAsUpEnabled(true);
39+
actionBar.setTitle("Custom Palette Picker");
40+
41+
colors = getIntent().getIntArrayExtra(MainActivity.PALETTE_COLOR_ARRAY);
42+
43+
context = this;
44+
45+
trianglifyView = (TrianglifyView) findViewById(R.id.trianglify_custom_palette_view);
46+
trianglifyView.setPalette(new Palette(colors));
47+
trianglifyView.smartUpdate();
48+
49+
for (int i = 0; i < imageViews.length; i++) {
50+
String imageViewNumber = "custom_palette_c" + String.valueOf(i);
51+
int resID = getResources().getIdentifier(imageViewNumber, "id", getPackageName());
52+
imageViews[i] = (ImageView) findViewById(resID);
53+
imageViews[i].setBackgroundColor(colors[i] + 0xff000000);
54+
55+
final int finalI = i;
56+
57+
imageViews[i].setOnClickListener(new View.OnClickListener() {
58+
@Override
59+
public void onClick(View v) {
60+
final AlertDialog dialog = ColorPickerDialogBuilder
61+
.with(context)
62+
.initialColor(colors[finalI] + 0xff000000)
63+
.setTitle("Choose Color")
64+
.wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
65+
.density(9)
66+
.showColorEdit(true)
67+
.lightnessSliderOnly()
68+
.setColorEditTextColor(0xff000000)
69+
.showColorPreview(true)
70+
.setOnColorSelectedListener(new OnColorSelectedListener() {
71+
@Override
72+
public void onColorSelected(int color) {
73+
}
74+
})
75+
.setPositiveButton("ok", new ColorPickerClickListener() {
76+
@Override
77+
public void onClick(DialogInterface dialog, int color, Integer[] allColors) {
78+
imageViews[finalI].setBackgroundColor(color);
79+
colors[finalI] = color - 0xff000000;
80+
palette = new Palette(colors);
81+
trianglifyView.setPalette(palette);
82+
trianglifyView.smartUpdate();
83+
}
84+
})
85+
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
86+
@Override
87+
public void onClick(DialogInterface dialog, int which) {
88+
dialog.dismiss();
89+
}
90+
})
91+
.build();
92+
93+
dialog.show();
94+
}
95+
});
96+
}
97+
}
98+
99+
@Override
100+
public boolean onCreateOptionsMenu(Menu menu) {
101+
MenuInflater inflater = getMenuInflater();
102+
inflater.inflate(R.menu.menu_custom_palette_picker, menu);
103+
return true;
104+
}
105+
106+
public boolean onOptionsItemSelected(MenuItem item) {
107+
switch (item.getItemId()) {
108+
case android.R.id.home:
109+
onBackPressed();
110+
return true;
111+
case R.id.custom_palette_ok:
112+
Intent intent = new Intent();
113+
intent.putExtra(CUSTOM_PALETTE_COLOR_ARRAY, colors);
114+
setResult(RESULT_OK, intent);
115+
onBackPressed();
116+
return true;
117+
default:
118+
return super.onOptionsItemSelected(item);
119+
}
120+
}
121+
}

app/src/main/java/com/sdsmdg/kd/trianglifyexample/MainActivity.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,18 @@ public class MainActivity extends AppCompatActivity {
2525
private CheckBox strokeCheckBox;
2626
private CheckBox fillCheckBox;
2727
private CheckBox randomColoringCheckbox;
28+
private CheckBox customPaletteCheckbox;
29+
private Palette customPalette;
30+
public static final String PALETTE_COLOR_ARRAY = "Palette Color Array";
2831

2932
@Override
3033
protected void onCreate(Bundle savedInstanceState) {
3134
super.onCreate(savedInstanceState);
3235
setContentView(R.layout.activity_main);
36+
3337
trianglifyView = (TrianglifyView) findViewById(R.id.trianglify_main_view);
38+
customPalette = trianglifyView.getPalette();
39+
3440
varianceSeekBar = (SeekBar) findViewById(R.id.variance_seekbar);
3541
varianceSeekBar.setMax(100);
3642
varianceSeekBar.setProgress(trianglifyView.getVariance());
@@ -51,11 +57,12 @@ public void onStopTrackingTouch(SeekBar seekBar) {
5157

5258
}
5359
});
60+
5461
cellSizeSeekBar = (SeekBar) findViewById(R.id.cell_size_seekbar);
5562
int maxCellSize = 150;
5663

5764
cellSizeSeekBar.setMax(maxCellSize);
58-
cellSizeSeekBar.setProgress(trianglifyView.getCellSize());
65+
cellSizeSeekBar.setProgress(trianglifyView.getCellSize() - 100);
5966
cellSizeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
6067
@Override
6168
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
@@ -81,6 +88,7 @@ public void onStopTrackingTouch(SeekBar seekBar) {
8188
@Override
8289
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
8390
trianglifyView.setPalette(Palette.getPalette(progress));
91+
customPaletteCheckbox.setChecked(false);
8492
trianglifyView.smartUpdate();
8593
}
8694

@@ -136,6 +144,21 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
136144
trianglifyView.smartUpdate();
137145
}
138146
});
147+
148+
customPaletteCheckbox = (CheckBox) findViewById(R.id.custom_palette_checkbox);
149+
customPaletteCheckbox.setChecked(false);
150+
customPaletteCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
151+
@Override
152+
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
153+
if (isChecked) {
154+
trianglifyView.setPalette(customPalette);
155+
trianglifyView.smartUpdate();
156+
} else {
157+
trianglifyView.setPalette(Palette.getPalette(paletteSeekBar.getProgress()));
158+
trianglifyView.smartUpdate();
159+
}
160+
}
161+
});
139162
}
140163

141164
@Override
@@ -163,6 +186,12 @@ public void randomizeTrianglifyParameters(TrianglifyView trianglifyView) {
163186
.setFillTriangle(rnd.nextInt(2) == 0)
164187
.setDrawStrokeEnabled(rnd.nextInt(2) == 0)
165188
.setVariance(rnd.nextInt(60));
189+
190+
if ( !trianglifyView.isFillTriangle() && !trianglifyView.isDrawStrokeEnabled()) {
191+
trianglifyView.setDrawStrokeEnabled(true);
192+
trianglifyView.setFillTriangle(true);
193+
}
194+
166195
updateUIElements(trianglifyView);
167196
}
168197

@@ -189,6 +218,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
189218
randomizeTrianglifyParameters(trianglifyView);
190219
trianglifyView.generateAndInvalidate();
191220
break;
221+
// action with ID custom_palette_picker was selected
222+
case R.id.custom_palette_picker:
223+
Intent customPalettePickerIntent = new Intent(this, CustomPalettePickerActivity.class);
224+
customPalettePickerIntent.putExtra(PALETTE_COLOR_ARRAY, trianglifyView.getPalette().getColors());
225+
startActivityForResult(customPalettePickerIntent, 1);
226+
break;
192227
default:
193228
break;
194229
}
@@ -197,6 +232,17 @@ public boolean onOptionsItemSelected(MenuItem item) {
197232
}
198233

199234
public void showColoringError() {
200-
Toast.makeText(this, "view should at least be set to draw strokes or fill triangles or both.", Toast.LENGTH_LONG).show();
235+
Toast.makeText(this, "view should at least be set to draw strokes or fill triangles or both.", Toast.LENGTH_LONG).show();
236+
}
237+
238+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
239+
super.onActivityResult(requestCode, resultCode, data);
240+
if (requestCode == 1 && resultCode == RESULT_OK) {
241+
customPalette = new Palette(data.getIntArrayExtra(CustomPalettePickerActivity.CUSTOM_PALETTE_COLOR_ARRAY));
242+
if (customPaletteCheckbox.isChecked()) {
243+
trianglifyView.setPalette(customPalette);
244+
trianglifyView.smartUpdate();
245+
}
246+
}
201247
}
202248
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
<stroke android:width="2dp" android:color="#bdbdbd"/>
4+
<padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp"/>
5+
</shape>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:height="48dp"
4+
android:width="48dp"
5+
android:viewportWidth="24"
6+
android:viewportHeight="24">
7+
<path android:fillColor="#fff" android:pathData="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z" />
8+
</vector>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:height="48dp"
4+
android:width="48dp"
5+
android:viewportWidth="24"
6+
android:viewportHeight="24">
7+
<path android:fillColor="#efeaeaea" android:pathData="M17.5,12A1.5,1.5 0 0,1 16,10.5A1.5,1.5 0 0,1 17.5,9A1.5,1.5 0 0,1 19,10.5A1.5,1.5 0 0,1 17.5,12M14.5,8A1.5,1.5 0 0,1 13,6.5A1.5,1.5 0 0,1 14.5,5A1.5,1.5 0 0,1 16,6.5A1.5,1.5 0 0,1 14.5,8M9.5,8A1.5,1.5 0 0,1 8,6.5A1.5,1.5 0 0,1 9.5,5A1.5,1.5 0 0,1 11,6.5A1.5,1.5 0 0,1 9.5,8M6.5,12A1.5,1.5 0 0,1 5,10.5A1.5,1.5 0 0,1 6.5,9A1.5,1.5 0 0,1 8,10.5A1.5,1.5 0 0,1 6.5,12M12,3A9,9 0 0,0 3,12A9,9 0 0,0 12,21A1.5,1.5 0 0,0 13.5,19.5C13.5,19.11 13.35,18.76 13.11,18.5C12.88,18.23 12.73,17.88 12.73,17.5A1.5,1.5 0 0,1 14.23,16H16A5,5 0 0,0 21,11C21,6.58 16.97,3 12,3Z" />
8+
</vector>

0 commit comments

Comments
 (0)