Skip to content

Turning uigradients.com into Java

Timothy Langer edited this page May 31, 2018 · 1 revision

If you haven't heard of uigradients.com, it's a really cool website that allows anyone to get a random community-designed gradient. Plus, it's all open sourced on Github!


Introduction

Since Ocquarium has a gradient background for the "sea", I've always been thinking about adding random gradient selection to the app. However, uiGradients stores the actual colour values in a file called gradients.json, and adding a JSON parsing library to Ocquarium was not preferable, since I'm trying to keep the Ocquarium APK under 1MB. Instead, I decided to try and use Notepad++ to turn the JSON file into a Java ArrayList.

Removing three-colour gradients

Firstly, I didn't want to use any of the gradients that have more than two colours, since that'd be more difficult to support, so I searched on Google for ways to mark a line in Notepad++ if it has more than n commas. and came across a superuser thread with the answer. Since I only need to remove lines with two or more commas, this is what I used:

.*,.*,

Deleting lines before and after search term

The problem is that I don't want to just delete the line containing more than one comma, I needed to remove two lines before and one line after that specific line (e.g. see here) so the following stackoverflow thread came in handy. So I took the search term from the section before and ended up with the following search term.

(?m)(^[^\r\n]*\R+){2}.*,.*,[^\r\n]*\R+(^[^\r\n]*\R+){1}

To delete the lines, I simply did

Search > Replace > Enter search term (nothing in the second box) > Search Mode = Regular Expression > Replace All 

The rest of the process

I deleted all { and }, and indents in the same way as above. I manually deleted the square brackets at the beginning and end of the JSON file. I deleted all empty lines through Edit > Line Operations > Remove Empty Lines (Containing Blank characters). I scrolled through the file just to check that everything was normal, and noticed that "By Design" was still indented, since it has a indent of spaces instead of tabs, so I removed that indent as well.

From then on, it was really simple. I made the following replacements:

Search term Replaced with
,\r\n"colors": [ ,
"name": add(new String[]{
] });

And I was done, so I pasted the result into my Java file. Thanks for reading!