Color (@NotNull List<? extends Pair<Color, Double>> colorCircles, @NotNull Color sampleColor, int nestLevel) { if (nestLevel > 4) { return sampleColor; } for (Pair<Color, Double> circle: colorCircles) { Color paletteColor = circle.first; double distance = colorDistance01(sampleColor, paletteColor); if (distance < circle.second) { float[] rgb = rgbDiffColor(sampleColor, paletteColor); double factor = 256 * circle.second / getLength(rgb); int mod = nestLevel % 4; int r = normalize(sampleColor.getRed() + rgb[0] * factor * (mod == 3 ? 2 : 1)); int g = normalize(sampleColor.getGreen() + rgb[1] * factor * (mod == 1 ? 2 : 1)); int b = normalize(sampleColor.getBlue() + rgb[2] * factor * (mod == 2 ? 2 : 1)); float[] hsbNew = Color.RGBtoHSB(r, g, b, null); float[] hsbOrig = Color.RGBtoHSB(sampleColor.getRed(), sampleColor.getGreen(), sampleColor.getBlue(), null); //System.out.println("#" + nestLevel + ":" + sampleColor + " diff:" + circle.second + " conflict:" + circle.first + " now:" +Color.getHSBColor(hsbNew[0], hsbNew[1], hsbOrig[2])); return resolveConflict(colorCircles, Color.getHSBColor(hsbNew[0], hsbNew[1], (hsbOrig[2] + hsbNew[2])/2), ++nestLevel); } } return sampleColor; }