|
| 1 | +/** |
| 2 | + * This utility allows JRubyArt users to use the processing.org color method |
| 3 | + * in their sketches. Includes a method to efficiently convert an cols of web |
| 4 | + * strings to an cols of color int, and another to convert an cols of p5 color |
| 5 | + * (int) to a string that can be used in ruby code (to generate web color cols). |
| 6 | + * Copyright (c) 2015-18 Martin Prout. |
| 7 | + * This utility is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Lesser General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2.1 of the License, or (at |
| 10 | + * your option) any later version. |
| 11 | + * |
| 12 | + * Obtain a copy of the license at http://www.gnu.org/licenses/lgpl-2.1.html |
| 13 | + */ |
| 14 | +package monkstone; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Random; |
| 19 | + |
| 20 | +/** |
| 21 | + * |
| 22 | + * @author Martin Prout |
| 23 | + */ |
| 24 | +public class ColorUtil { |
| 25 | + |
| 26 | + static final String TOO_BIG = "produces a line too long a line for code"; |
| 27 | + |
| 28 | + /** |
| 29 | + * Returns hex long as a positive int unless greater than Integer.MAX_VALUE |
| 30 | + * else return the complement as a negative integer or something like that |
| 31 | + * |
| 32 | + * @param hexlong long |
| 33 | + * @return rgb int |
| 34 | + */ |
| 35 | + static final int hexLong(long hexlong) { |
| 36 | + long SPLIT = Integer.MAX_VALUE + 1; |
| 37 | + if (hexlong < SPLIT) { |
| 38 | + return (int) hexlong; |
| 39 | + } else { |
| 40 | + return (int) (hexlong - SPLIT * 2L); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param hexstring String |
| 46 | + * @return rgb int |
| 47 | + */ |
| 48 | + static public int colorString(String hexstring) { |
| 49 | + return java.awt.Color.decode(hexstring).getRGB(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * |
| 54 | + * @param web Array of web (hex) String |
| 55 | + * @return cols of p5 color (int) |
| 56 | + */ |
| 57 | + static public int[] webArray(String... web) { |
| 58 | + int[] result = new int[web.length]; |
| 59 | + for (int i = 0; i < web.length; i++) { |
| 60 | + result[i] = java.awt.Color.decode(web[i]).getRGB(); |
| 61 | + } |
| 62 | + return result; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Return a ruby string of the form "%w[a b c]" where a, b, c are raw web |
| 67 | + * strings. This string can be used in ruby code. |
| 68 | + * |
| 69 | + * @param p5colors cols of p5 colors (int) |
| 70 | + * @return String for use in ruby |
| 71 | + */ |
| 72 | + static public String rubyString(int[] p5colors) { |
| 73 | + if (p5colors.length > 8){ return TOO_BIG;} |
| 74 | + StringBuilder sb = new StringBuilder("%w["); |
| 75 | + for (int p5color : p5colors) { |
| 76 | + sb.append(String.format("#%06X", (0xFFFFFF & p5color))); |
| 77 | + sb.append(' '); |
| 78 | + } |
| 79 | + sb.deleteCharAt(sb.length() - 1); |
| 80 | + sb.append("]\n"); |
| 81 | + return sb.toString(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * |
| 86 | + * @param p5colors |
| 87 | + * @return array of web color as String |
| 88 | + */ |
| 89 | + static public String[] p5ToWeb(int[] p5colors) { |
| 90 | + List<String> list = new ArrayList<>(); |
| 91 | + for (int p5color : p5colors) { |
| 92 | + list.add(String.format("#%06X", (0xFFFFFF & p5color))); |
| 93 | + } |
| 94 | + return list.toArray(new String[0]); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * |
| 99 | + * @param cols |
| 100 | + * @return array of color int |
| 101 | + */ |
| 102 | + static public int[] shuffle(int[] cols) { |
| 103 | + Random rgen = new Random(); // Random number generator |
| 104 | + for (int i = 0; i < cols.length; i++) { |
| 105 | + int randomPosition = rgen.nextInt(cols.length); |
| 106 | + int temp = cols[i]; |
| 107 | + cols[i] = cols[randomPosition]; |
| 108 | + cols[randomPosition] = temp; |
| 109 | + } |
| 110 | + return cols; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * |
| 115 | + * @param hex double |
| 116 | + * @return hex float |
| 117 | + */ |
| 118 | + static public float colorLong(double hex) { |
| 119 | + return (float) hex; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * |
| 124 | + * @param hexlong long |
| 125 | + * @return hexlong int |
| 126 | + */ |
| 127 | + static public int colorLong(long hexlong) { |
| 128 | + return hexLong(hexlong); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * |
| 133 | + * @param hex double |
| 134 | + * @return hex float |
| 135 | + */ |
| 136 | + static public float colorDouble(double hex) { |
| 137 | + return (float) hex; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * |
| 142 | + * @param hue |
| 143 | + * @param sat |
| 144 | + * @param brightness |
| 145 | + * @return color int |
| 146 | + */ |
| 147 | + static public int hsbToRgB(double hue, double sat, double brightness) { |
| 148 | + return java.awt.Color.HSBtoRGB((float) hue, (float) sat, (float) brightness); |
| 149 | + } |
| 150 | +} |
0 commit comments