Can someone explain this color wheel code to me?

Posted by user1869438 on Stack Overflow See other posts from Stack Overflow or by user1869438
Published on 2012-12-01T22:14:30Z Indexed on 2012/12/01 23:03 UTC
Read the original article Hit count: 175

Filed under:
|

I just started doing java and i need some help with understanding this code. I got it from a this website. This is supposed to be code for a color wheel but i don't really understand how it works, especially the final ints STEPS and SLICES.

import java.awt.Color;

import objectdraw.*;

public class ColorWheel extends WindowController {
private double brightness;
private Text text;
private FilledRect swatch;
private Location center;
private int size;
private FilledRect brightnessOverlay;

private static final int SLICES = 96;
private static final int STEPS = 16;

public void begin() {
    canvas.setBackground(Color.BLACK);
    brightness = 1.;

    size = Math.min(canvas.getWidth(), canvas.getHeight() - 20);
    center = new Location(canvas.getWidth() / 2, size / 2);

    for(int j = STEPS; j >= 1; j--) {
        int arcSize = size * j / STEPS;
        int x = center.getX() - arcSize / 2;
        int y = center.getY() - arcSize / 2;

        for(int i = 0; i < SLICES; i++) {
            Color c = Color.getHSBColor((float)i / SLICES, (float)j / STEPS, (float)brightness);
            new FilledArc(x, y, arcSize, arcSize, i * 360. / SLICES, 360. / SLICES + .5, c, canvas);
        }
    }
    swatch = new FilledRect(0, canvas.getHeight() - 20, canvas.getWidth(), 20, Color.BLACK, canvas);
    brightnessOverlay = new FilledRect(0, 0, canvas.getWidth(), canvas.getHeight() - 20, new Color(0, 0, 0, 0), canvas);
    text = new Text("", canvas.getWidth() / 2, canvas.getHeight() - 18, canvas);
    text.setAlignment(Text.CENTER, Text.TOP);
    text.setBold(true);
}

public void onMouseDrag(Location point) {
    brightness = (canvas.getHeight() - point.getY()) / (double)(canvas.getHeight());
    if(brightness < 0) {
        brightness = 0;
    } else if(brightness > 1) {
        brightness = 1;
    }

    if(brightness < .5) {
        text.setColor(Color.WHITE);
    } else {
        text.setColor(Color.BLACK);
    }
    brightnessOverlay.setColor(new Color(0f, 0f, 0f, (float)(1 - brightness)));
}

public void onMouseMove(Location point) {
    double saturation = 2 * center.distanceTo(point) / size;
    if(saturation > 1) {
        text.setText("");
        swatch.setColor(Color.BLACK);
        return;
    }
    double hue = -Math.atan2(point.getY() - center.getY(), point.getX() - center.getX()) / (2 * Math.PI);
    if(hue < 0) {
        hue += 1;
    }
    swatch.setColor(Color.getHSBColor((float)hue, (float)saturation, (float)brightness));
    text.setText("Color.getHSBColor(" + Text.formatDecimal(hue, 2) + "f, " + 
            Text.formatDecimal(saturation, 2) + "f, " + Text.formatDecimal(brightness, 2) + "f)");
}
}

© Stack Overflow or respective owner

Related posts about java

Related posts about colors