lejos.robotics.ColorAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lejos-ev3-api Show documentation
Show all versions of lejos-ev3-api Show documentation
leJOS (pronounced like the Spanish word "lejos" for "far") is a tiny Java Virtual Machine. In 2013 it was ported to the LEGO EV3 brick.
The newest version!
package lejos.robotics;
import lejos.hardware.sensor.*;
public class ColorAdapter implements ColorDetector, ColorIdentifier {
private final float[] bufDetect;
private final float[] bufID;
SensorMode colorDetector;
SensorMode colorIdentifier;
public ColorAdapter(BaseSensor colorSensor) {
this.colorDetector = colorSensor.getMode("RGB");
this.colorIdentifier = colorSensor.getMode("ColorID");
bufDetect=new float[colorDetector.sampleSize()];
bufID=new float[colorIdentifier.sampleSize()];
}
public int getColorID() {
colorIdentifier.fetchSample(bufID, 0);
return (int)bufID[0];
}
@Override
public Color getColor() {
colorDetector.fetchSample(bufDetect, 0);
Color color = new Color((int)(256*bufDetect[0]), (int)(256*bufDetect[1]), (int)(256*bufDetect[2]));
return color;
}
}