lejos.nxt.addon
Class ColorSensor

java.lang.Object
  extended by lejos.nxt.I2CSensor
      extended by lejos.nxt.addon.ColorSensor
All Implemented Interfaces:
SensorConstants, ColorDetector

public class ColorSensor
extends I2CSensor
implements ColorDetector

HiTechnic color sensor.
www.hitechnic.com

Author:
BB extended by A.T.Brask

Field Summary
 
Fields inherited from class lejos.nxt.I2CSensor
PRODUCT_ID, SENSOR_TYPE, VERSION
 
Fields inherited from interface lejos.nxt.SensorConstants
MODE_ANGLESTEP, MODE_BOOLEAN, MODE_CELSIUS, MODE_FARENHEIT, MODE_PCTFULLSCALE, MODE_PERIODCOUNTER, MODE_RAW, MODE_TRANSITIONCNT, TYPE_ANGLE, TYPE_COLORBLUE, TYPE_COLORFULL, TYPE_COLORGREEN, TYPE_COLORNONE, TYPE_COLORRED, TYPE_CUSTOM, TYPE_HISPEED, TYPE_LIGHT_ACTIVE, TYPE_LIGHT_INACTIVE, TYPE_LOWSPEED, TYPE_LOWSPEED_9V, TYPE_NO_SENSOR, TYPE_REFLECTION, TYPE_SOUND_DB, TYPE_SOUND_DBA, TYPE_SWITCH, TYPE_TEMPERATURE
 
Constructor Summary
ColorSensor(I2CPort port)
           
 
Method Summary
 int getBlueComponent()
          Returns the blue saturation of the color.
 int[] getColor()
          Return the Red, Green and Blue values together in one array.
 int getColorIndexNumber()
          Essentially the same as getColorNumber() but with a resolution of 6 bits.
 int getColorNumber()
          Returns the color index detected by the sensor.
 int getGreenComponent()
          Returns the green saturation of the color.
 int getMode()
          Returns the value of the mode control register (0x41)
 int getNormalizedBlue()
          Returns the normalized blue saturation of the color.
 int getNormalizedGreen()
          Returns the normalized green saturation of the color.
 int getNormalizedRed()
          Returns the normalized red saturation of the color.
 int getRawBlue()
          Returns the raw blue saturation of the color.
 int getRawGreen()
          Returns the raw green saturation of the color.
 int getRawRed()
          Returns the raw red saturation of the color.
 int getRedComponent()
          Returns the red saturation of the color.
 int initBlackLevel()
          Puts the sensor into black/ambient level calibration mode.
 int initWhiteBalance()
          Puts the sensor into white balance calibration mode.
 
Methods inherited from class lejos.nxt.I2CSensor
fetchString, getData, getPort, getProductID, getSensorType, getVersion, sendData, sendData, setAddress
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ColorSensor

public ColorSensor(I2CPort port)
Method Detail

getColorNumber

public int getColorNumber()
Returns the color index detected by the sensor.

Returns:
Color index.
  • 0 = black
  • 1 = violet
  • 2 = purple
  • 3 = blue
  • 4 = green
  • 5 = lime
  • 6 = yellow
  • 7 = orange
  • 8 = red
  • 9 = crimson
  • 10 = magenta
  • 11 to 16 = pastels
  • 17 = white

  • getColorIndexNumber

    public int getColorIndexNumber()
    Essentially the same as getColorNumber() but with a resolution of 6 bits. Red is bit 5-4, green is bit 3-2 and blue is bit 1-0.

    Returns:
    Color index number

    getRedComponent

    public int getRedComponent()
    Returns the red saturation of the color.

    Specified by:
    getRedComponent in interface ColorDetector
    Returns:
    red value (0 to 255).

    getGreenComponent

    public int getGreenComponent()
    Returns the green saturation of the color.

    Specified by:
    getGreenComponent in interface ColorDetector
    Returns:
    green value (0 to 255).

    getBlueComponent

    public int getBlueComponent()
    Returns the blue saturation of the color.

    Specified by:
    getBlueComponent in interface ColorDetector
    Returns:
    blue value (0 to 255).

    getNormalizedRed

    public int getNormalizedRed()
    Returns the normalized red saturation of the color.

    Returns:
    red value (0 to 255).

    getNormalizedGreen

    public int getNormalizedGreen()
    Returns the normalized green saturation of the color.

    Returns:
    green value (0 to 255).

    getNormalizedBlue

    public int getNormalizedBlue()
    Returns the normalized blue saturation of the color.

    Returns:
    blue value (0 to 255).

    getRawRed

    public int getRawRed()
    Returns the raw red saturation of the color.

    Returns:
    red value (0 to 1023).

    getRawGreen

    public int getRawGreen()
    Returns the raw green saturation of the color.

    Returns:
    green value (0 to 1023).

    getRawBlue

    public int getRawBlue()
    Returns the raw blue saturation of the color.

    Returns:
    blue value (0 to 1023).

    getMode

    public int getMode()
    Returns the value of the mode control register (0x41)

    Returns:
    The value of the register or -1 if the operation fails.

    initWhiteBalance

    public int initWhiteBalance()
    Puts the sensor into white balance calibration mode. For best results the sensor should be pointed at a diffuse white surface at a distance of approximately 15mm before calling this method. After a fraction of a second the sensor lights will flash and the calibration is done. When calibrated, the sensor keeps this information in non-volatile memory.

    Returns:
    0 if it went well and -1 otherwise

    initBlackLevel

    public int initBlackLevel()
    Puts the sensor into black/ambient level calibration mode. For best results the sensor should be pointed in a direction with no obstacles for 50cm or so. This reading the sensor will use as a base level for other readings. After a fraction of a second the sensor lights will flash and the calibration is done. When calibrated, the sensor keeps this information in non-volatile memory.

    Returns:
    0 if it went well and -1 otherwise.

    getColor

    public int[] getColor()
    Description copied from interface: ColorDetector
    Return the Red, Green and Blue values together in one array. All values are 0-255.

    Specified by:
    getColor in interface ColorDetector
    Returns:
    All three color values. The array index for Red is 0, Green is 1, Blue is 2.