Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

Commit

Permalink
add /gradient and grad()
Browse files Browse the repository at this point in the history
  • Loading branch information
funnyboy-roks committed Nov 5, 2020
1 parent 85b2180 commit 6bfd432
Show file tree
Hide file tree
Showing 19 changed files with 577 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.idea/
target/

*.zip

# Compiled class file
*.class

Expand Down
3 changes: 0 additions & 3 deletions .idea/.gitignore

This file was deleted.

4 changes: 2 additions & 2 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Gradient-Generator-Plugin.iml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.bukkit:bukkit:1.15.2-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.25" level="project" />
<orderEntry type="library" name="Maven: net.md-5:bungeecord-chat:1.16-R0.3" level="project" />
</component>
</module>
31 changes: 31 additions & 0 deletions Gradient-Generator.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="minecraft" name="Minecraft">
<configuration>
<autoDetectTypes>
<platformType>BUKKIT</platformType>
</autoDetectTypes>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.bukkit:bukkit:1.15.2-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.25" level="project" />
<orderEntry type="library" name="Maven: net.md-5:bungeecord-chat:1.16-R0.3" level="project" />
</component>
</module>
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,10 @@
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-chat</artifactId>
<version>1.16-R0.3</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.anomal.RainbowVis;

public class HomogeneousRainbowException extends RainbowException {

private static final long serialVersionUID = -3883632693158928681L;

public String getMessage() {
return "Rainbow must have two or more colours.";
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/anomal/RainbowVis/InvalidColourException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.anomal.RainbowVis;

public class InvalidColourException extends RainbowException {

private static final long serialVersionUID = 5801441252925805756L;

private String nonColor;

public InvalidColourException(String nonColour){
super();
nonColor = nonColour;
}

public String getMessage() {
return nonColor + " is not a valid colour.";
}

}
20 changes: 20 additions & 0 deletions src/main/java/com/anomal/RainbowVis/NumberRangeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.anomal.RainbowVis;

public class NumberRangeException extends RainbowException {

private static final long serialVersionUID = 4165381497766700805L;

private double minNum;
private double maxNum;

public NumberRangeException (double minNumber, double maxNumber) {
super();
minNum = minNumber;
maxNum = maxNumber;
}

public String getMessage() {
return "maxNumber (" + maxNum + ") is not greater than minNumber (" + minNum + ")";
}

}
227 changes: 227 additions & 0 deletions src/main/java/com/anomal/RainbowVis/Rainbow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
// (MIT License)
// Copyright (c) 2012 Sophiah (Zing-Ming)
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.anomal.RainbowVis;

import java.util.ArrayList;
import java.util.Hashtable;

/**
* The Rainbow class by default maps the range 0 to 100 (inclusive) to the colours of the rainbow
* (i.e., a gradient transitioning from red to yellow to lime to blue)
* @author Sophiah (Zing-Ming)
*
*/
public class Rainbow {

private double minNum;
private double maxNum;
private String[] colours;
private ArrayList<ColourGradient> colourGradients;

/**
* Constructor. By default, the number range is from 0 to 100, and the spectrum is a rainbow.
*/
public Rainbow() {
try {
minNum = 0;
maxNum = 100;
colours = new String[]{"red", "yellow", "lime", "blue"};
setSpectrum(colours);
}
// These exceptions are theoretically impossible, so rethrow as unchecked exceptions
catch (HomogeneousRainbowException e) {
throw new AssertionError(e);
} catch (InvalidColourException e) {
throw new AssertionError(e);
}

}

/**
* Returns the hex colour corresponding to the number. If number is out of range,
* it returns the appropriate hex colour corresponding to either the minNumber or maxNumber.
* @param number The number for which you want to find the corresponding colour
* @return The corresponding colour represented as a HTML RGB hexidecimal String
*/
public String colourAt(double number) {
if (colourGradients.size() == 1) {
return colourGradients.get(0).colourAt(number);
} else {
double segment = (maxNum - minNum)/(colourGradients.size());
int index = (int) Math.min(Math.floor((Math.max(number, minNum) - minNum)/segment), colourGradients.size() - 1);
return colourGradients.get(index).colourAt(number);
}
}

/**
* Sets the spectrum of the Rainbow object. By default, the spectrum is a rainbow.
* You must have a minimum of two colours, but you can specify more than two colours.
* Colours can be in the form "red", "ff0000", or "#ff0000".
* For example, <code>rainbow.setSpectrum("red", "yellow", "white");</code>
* makes the "Rainbow" a colour gradient from red to yellow to white.
* @param spectrum Two or more Strings representing HTML colours,
* or pass in a whole String array of length 2 or greater
* @throws HomogeneousRainbowException if there is less than two arguments
* @throws InvalidColourException if one of the arguments is an invalid colour
*/
public void setSpectrum (String ... spectrum) throws HomogeneousRainbowException, InvalidColourException {
try {
if (spectrum.length < 2) {
throw new HomogeneousRainbowException();
} else {
double increment = (maxNum - minNum)/(spectrum.length - 1);
ColourGradient firstGradient = new ColourGradient();
firstGradient.setGradient(spectrum[0], spectrum[1]);
firstGradient.setNumberRange(minNum, minNum + increment);

colourGradients = new ArrayList<ColourGradient>();
colourGradients.add(firstGradient);

for (int i = 1; i < spectrum.length - 1; i++) {
ColourGradient colourGradient = new ColourGradient();
colourGradient.setGradient(spectrum[i], spectrum[i + 1]);
colourGradient.setNumberRange(minNum + increment * i, minNum + increment * (i + 1));
colourGradients.add(colourGradient);
}

colours = spectrum;
}
}
// This exception is theoretically impossible, so rethrow as unchecked exception
catch (NumberRangeException e) {
throw new RuntimeException(e);
}
}

/**
* Sets the number range of the Rainbow object. By default, it is 0 to 100.
* @param minNumber The minimum number of the number range
* @param maxNumber The maximum number of the number range
* @throws NumberRangeException if minNumber is greater than maxNumber
*/
public void setNumberRange(double minNumber, double maxNumber) throws NumberRangeException
{
try {
if (maxNumber > minNumber) {
minNum = minNumber;
maxNum = maxNumber;
setSpectrum(colours);
} else {
throw new NumberRangeException(minNumber, maxNumber);
}
}
// These exceptions are theoretically impossible, so rethrow as unchecked exceptions
catch (HomogeneousRainbowException e) {
throw new RuntimeException(e);
} catch (InvalidColourException e) {
throw new RuntimeException(e);
}
}

/**
* Same as colourAt(double number)
*/
public String colorAt(double number) {
return colourAt(number);
}

}

class ColourGradient {

private int[] startColour = {0xff, 0x00, 0x00};
private int[] endColour = {0x00, 0x00, 0xff};
private double minNum = 0;
private double maxNum = 100;

private static Hashtable<String, int[]> htmlColors;
static {
htmlColors = new Hashtable<String, int[]>();
htmlColors.put("black", new int[]{0x00, 0x00, 0x00});
htmlColors.put("navy", new int[]{0x00, 0x00, 0x80});
htmlColors.put("blue", new int[]{0x00, 0x00, 0xff});
htmlColors.put("green", new int[]{0x00, 0x80, 0x00});
htmlColors.put("teal", new int[]{0x00, 0x80, 0x80});
htmlColors.put("lime", new int[]{0x00, 0xff, 0x00});
htmlColors.put("aqua", new int[]{0x00, 0xff, 0xff});
htmlColors.put("maroon", new int[]{0x80, 0x00, 0x00});
htmlColors.put("purple", new int[]{0x80, 0x00, 0x80});
htmlColors.put("olive", new int[]{0x80, 0x80, 0x00});
htmlColors.put("grey", new int[]{0x80, 0x80, 0x80});
htmlColors.put("gray", new int[]{0x80, 0x80, 0x80});
htmlColors.put("silver", new int[]{0xc0, 0xc0, 0xc0});
htmlColors.put("red", new int[]{0xff, 0x00, 0x00});
htmlColors.put("fuchsia", new int[]{0xff, 0x00, 0xff});
htmlColors.put("orange", new int[]{0xff, 0x80, 0x00});
htmlColors.put("yellow", new int[]{0xff, 0xff, 0x00});
htmlColors.put("white", new int[]{0xff, 0xff, 0xff});
}

public String colourAt(double number) {
return formatHex(calcHex(number, startColour[0], endColour[0]))
+ formatHex(calcHex(number, startColour[1], endColour[1]))
+ formatHex(calcHex(number, startColour[2], endColour[2]));
}

private int calcHex(double number, int channelStart, int channelEnd) {
double num = number;
if (num < minNum) {
num = minNum;
}
if (num > maxNum) {
num = maxNum;
}
double numRange = maxNum - minNum;
double cPerUnit = (channelEnd - channelStart)/numRange;
return (int) Math.round(cPerUnit * (num - minNum) + channelStart);
}

private String formatHex (int val)
{
String hex = Integer.toHexString(val);
if (hex.length() == 1) {
return '0' + hex;
} else {
return hex;
}
}

public void setNumberRange(double minNumber, double maxNumber) throws NumberRangeException{
if (maxNumber > minNumber) {
minNum = minNumber;
maxNum = maxNumber;
} else {
throw new NumberRangeException(minNumber, maxNumber);
}
}

public void setGradient (String colourStart, String colourEnd) throws InvalidColourException {
startColour = getHexColour(colourStart);
endColour = getHexColour(colourEnd);
}

private int[] getHexColour(String s) throws InvalidColourException {
if (s.matches("^#?[0-9a-fA-F]{6}$")){
return rgbStringToArray(s.replace("#", ""));
} else {
int[] rgbArray = htmlColors.get(s.toLowerCase());
if (rgbArray == null) {
throw new InvalidColourException(s);
} else {
return rgbArray;
}
}
}

private int[] rgbStringToArray(String s){
int red = Integer.parseInt(s.substring(0,2), 16);
int green = Integer.parseInt(s.substring(2,4), 16);
int blue = Integer.parseInt(s.substring(4,6), 16);
return new int[]{red, green, blue};
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/anomal/RainbowVis/RainbowException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.anomal.RainbowVis;

public class RainbowException extends Exception {

private static final long serialVersionUID = -6374325269566937721L;

}
Loading

0 comments on commit 6bfd432

Please sign in to comment.