Color Sensors
NextColorDistanceSensor combines a color sensor and an optional distance sensor into one class.
Creating a NextColorDistanceSensor
Section titled “Creating a NextColorDistanceSensor”// By Lynx Module and I2C bus, recommendedval sensor = NextColorDistanceSensor(RobotController.controlHub, 0)
// Using your configuration nameval sensor = NextColorDistanceSensor("colorSensor")// By Lynx Module and I2C bus, recommendedNextColorDistanceSensor sensor = new NextColorDistanceSensor(RobotController.controlHub(), 0);
// Using your configuration nameNextColorDistanceSensor sensor = new NextColorDistanceSensor("colorSensor");Both constructors accept an optional hasDistance parameter:
| Parameter | Type | Default | Description |
|---|---|---|---|
hasDistance |
Boolean |
false |
Whether to also create a distance sensor from the same sensor. |
update()
Section titled “update()”Reads the color sensor (and distance sensor, if present) and refreshes the cache.
Call this once per loop, before reading anything.
It’s recommended to put this in your periodic for your Mechanism..
sensor.update()sensor.update();isWithinDistance(threshold, unit)
Section titled “isWithinDistance(threshold, unit)”True if the distance sensor senses an object within threshold, in the given unit.
if (sensor.isWithinDistance(4.0)) { ... } //defaults to CMif (sensor.isWithinDistance(4.0)) { ... } //defaults to CMisColor(profile)
Section titled “isColor(profile)”True if the cached color reading matches the given ColorProfile.
if (sensor.isColor(green)) { ... }if (sensor.isColor(green)) { ... }isColorWithinDistance(profile, threshold, unit)
Section titled “isColorWithinDistance(profile, threshold, unit)”True if the cached color reading matches profile and an object is within threshold, in the given unit.
if (sensor.isColorWithinDistance(green, 4.0)) { ... } //defaults to CMif (sensor.isColorWithinDistance(green, 4.0)) { ... } //defaults to CMdebug()
Section titled “debug()”Single-line telemetry string showing the current RGB, HSV, and distance readings.
Useful for calibrating ColorProfiles.
telemetry.addLine(sensor.debug())telemetry.addLine(sensor.debug());NextColor
Section titled “NextColor”NextColor stores a color, either as RGB or HSV.
HSV is recommended for most cases, since it’s more stable under different lighting conditions.
val lime = NextColor.hsv(100f, 0.9f, 0.85f)
val sameColor = NextColor.rgb(lime.red, lime.green, lime.blue)NextColor lime = NextColor.Companion.hsv(100f, 0.9f, 0.85f);
NextColor sameColor = NextColor.Companion.rgb(lime.getRed(), lime.getGreen(), lime.getBlue());ColorProfile
Section titled “ColorProfile”ColorProfile describes a target color and the tolerances, to match your readings.
These can be used for isColor() or isColorWithinDistance().
val green = ColorProfile( space = ColorSpace.HSV, color = NextColor.hsv(130f, 0.7f, 0.6f), tolerance = NextColor.hsv(20f, 0.3f, 1f),)ColorProfile green = new ColorProfile( ColorSpace.HSV, NextColor.hsv(130f, 0.7f, 0.6f), NextColor.hsv(20f, 0.3f, 1f));| Parameter | Type | Description |
|---|---|---|
space |
ColorSpace |
The color space to compare in: RGB or HSV. |
color |
NextColor |
The target color to match against. |
tolerance |
NextColor |
How far each channel can deviate from color and still count as a match. |

