Java Code Examples for android.graphics.Color.RGBToHSV

The following are Jave code examples for showing how to use RGBToHSV of the android.graphics.Color class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: MyCalendar   File: ColorPickerView.java   View Source Code Vote up 6 votes
/**
 * Set the color this view should show.
 *
 * @param color    The color that should be selected. #argb
 * @param callback If you want to get a callback to your OnColorChangedListener.
 */
public void setColor(int color, boolean callback) {

    int alpha = Color.alpha(color);
    int red = Color.red(color);
    int blue = Color.blue(color);
    int green = Color.green(color);

    float[] hsv = new float[3];

    Color.RGBToHSV(red, green, blue, hsv);

    this.alpha = alpha;
    hue = hsv[0];
    sat = hsv[1];
    val = hsv[2];

    if (callback && onColorChangedListener != null) {
        onColorChangedListener
                .onColorChanged(Color.HSVToColor(this.alpha, new float[]{hue, sat, val}));
    }

    invalidate();
}
 
Example 2
Project: MDWechat   File: ColorPickerView.java   View Source Code Vote up 6 votes
/**
 * Set the color this view should show.
 *
 * @param color
 *     The color that should be selected. #argb
 * @param callback
 *     If you want to get a callback to your OnColorChangedListener.
 */
public void setColor(int color, boolean callback) {

  int alpha = Color.alpha(color);
  int red = Color.red(color);
  int blue = Color.blue(color);
  int green = Color.green(color);

  float[] hsv = new float[3];

  Color.RGBToHSV(red, green, blue, hsv);

  this.alpha = alpha;
  hue = hsv[0];
  sat = hsv[1];
  val = hsv[2];

  if (callback && onColorChangedListener != null) {
    onColorChangedListener
        .onColorChanged(Color.HSVToColor(this.alpha, new float[]{hue, sat, val}));
  }

  invalidate();
}
 
Example 3
Project: Swift-Braille-Soft-keyboard   File: ColorPickerView.java   View Source Code Vote up 6 votes
/**
 * Set the color this view should show.
 * @param color The color that should be selected. #argb
 * @param callback If you want to get a callback to
 * your OnColorChangedListener.
 */
public void setColor(int color, boolean callback){

	int alpha = Color.alpha(color);
	int red = Color.red(color);
	int blue = Color.blue(color);
	int green = Color.green(color);
	
	float[] hsv = new float[3];
	
	Color.RGBToHSV(red, green, blue, hsv);
	
	mAlpha = alpha;
	mHue = hsv[0];
	mSat = hsv[1];
	mVal = hsv[2];
	
	if(callback && mListener != null){			
		mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}));				
	}
	
	invalidate();
}
 
Example 4
Project: SubwayTooter   File: ColorPickerView.java   View Source Code Vote up 6 votes
/**
 * Set the color this view should show.
 *
 * @param color
 *     The color that should be selected. #argb
 * @param callback
 *     If you want to get a callback to your OnColorChangedListener.
 */
public void setColor(int color, boolean callback) {

  int alpha = Color.alpha(color);
  int red = Color.red(color);
  int blue = Color.blue(color);
  int green = Color.green(color);

  float[] hsv = new float[3];

  Color.RGBToHSV(red, green, blue, hsv);

  this.alpha = alpha;
  hue = hsv[0];
  sat = hsv[1];
  val = hsv[2];

  if (callback && onColorChangedListener != null) {
    onColorChangedListener
        .onColorChanged(Color.HSVToColor(this.alpha, new float[]{hue, sat, val}));
  }

  invalidate();
}
 
Example 5
Project: easyfilemanager   File: Utils.java   View Source Code Vote up 5 votes
public static int getComplementaryColor(int colorToInvert) {
    float[] hsv = new float[3];
    Color.RGBToHSV(Color.red(colorToInvert), Color.green(colorToInvert),
            Color.blue(colorToInvert), hsv);
    hsv[0] = (hsv[0] + 180) % 360;
    return Color.HSVToColor(hsv);
}
 
Example 6
Project: FTC2016   File: SensorMRColor.java   View Source Code Vote up 5 votes
@Override
public void runOpMode() {

  // hsvValues is an array that will hold the hue, saturation, and value information.
  float hsvValues[] = {0F,0F,0F};

  // values is a reference to the hsvValues array.
  final float values[] = hsvValues;

  // get a reference to the RelativeLayout so we can change the background
  // color of the Robot Controller app to match the hue detected by the RGB sensor.
  final View relativeLayout = ((Activity) hardwareMap.appContext).findViewById(R.id.RelativeLayout);

  // bPrevState and bCurrState represent the previous and current state of the button.
  boolean bPrevState = false;
  boolean bCurrState = false;

  // bLedOn represents the state of the LED.
  boolean bLedOn = true;

  // get a reference to our ColorSensor object.
  colorSensor = hardwareMap.colorSensor.get("sensor_color");

  // Set the LED in the beginning
  colorSensor.enableLed(bLedOn);

  // wait for the start button to be pressed.
  waitForStart();

  // while the op mode is active, loop and read the RGB data.
  // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
  while (opModeIsActive()) {

    // check the status of the x button on either gamepad.
    bCurrState = gamepad1.x;

    // check for button state transitions.
    if ((bCurrState == true) && (bCurrState != bPrevState))  {

      // button is transitioning to a pressed state. So Toggle LED
      bLedOn = !bLedOn;
      colorSensor.enableLed(bLedOn);
    }

    // update previous state variable.
    bPrevState = bCurrState;

    // convert the RGB values to HSV values.
    Color.RGBToHSV(colorSensor.red() * 8, colorSensor.green() * 8, colorSensor.blue() * 8, hsvValues);

    // send the info back to driver station using telemetry function.
    telemetry.addData("LED", bLedOn ? "On" : "Off");
    telemetry.addData("Clear", colorSensor.alpha());
    telemetry.addData("Red  ", colorSensor.red());
    telemetry.addData("Green", colorSensor.green());
    telemetry.addData("Blue ", colorSensor.blue());
    telemetry.addData("Hue", hsvValues[0]);

    // change the background color to match the color detected by the RGB sensor.
    // pass a reference to the hue, saturation, and value array as an argument
    // to the HSVToColor method.
    relativeLayout.post(new Runnable() {
      public void run() {
        relativeLayout.setBackgroundColor(Color.HSVToColor(0xff, values));
      }
    });

    telemetry.update();
  }
}
 
Example 7
Project: Ftc2018RelicRecovery   File: SensorREVColorDistance.java   View Source Code Vote up 5 votes
@Override
public void runOpMode() {

    // get a reference to the color sensor.
    sensorColor = hardwareMap.get(ColorSensor.class, "sensor_color_distance");

    // get a reference to the distance sensor that shares the same name.
    sensorDistance = hardwareMap.get(DistanceSensor.class, "sensor_color_distance");

    // hsvValues is an array that will hold the hue, saturation, and value information.
    float hsvValues[] = {0F, 0F, 0F};

    // values is a reference to the hsvValues array.
    final float values[] = hsvValues;

    // sometimes it helps to multiply the raw RGB values with a scale factor
    // to amplify/attentuate the measured values.
    final double SCALE_FACTOR = 255;

    // get a reference to the RelativeLayout so we can change the background
    // color of the Robot Controller app to match the hue detected by the RGB sensor.
    int relativeLayoutId = hardwareMap.appContext.getResources().getIdentifier("RelativeLayout", "id", hardwareMap.appContext.getPackageName());
    final View relativeLayout = ((Activity) hardwareMap.appContext).findViewById(relativeLayoutId);

    // wait for the start button to be pressed.
    waitForStart();

    // loop and read the RGB and distance data.
    // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
    while (opModeIsActive()) {
        // convert the RGB values to HSV values.
        // multiply by the SCALE_FACTOR.
        // then cast it back to int (SCALE_FACTOR is a double)
        Color.RGBToHSV((int) (sensorColor.red() * SCALE_FACTOR),
                (int) (sensorColor.green() * SCALE_FACTOR),
                (int) (sensorColor.blue() * SCALE_FACTOR),
                hsvValues);

        // send the info back to driver station using telemetry function.
        telemetry.addData("Distance (cm)",
                String.format(Locale.US, "%.02f", sensorDistance.getDistance(DistanceUnit.CM)));
        telemetry.addData("Alpha", sensorColor.alpha());
        telemetry.addData("Red  ", sensorColor.red());
        telemetry.addData("Green", sensorColor.green());
        telemetry.addData("Blue ", sensorColor.blue());
        telemetry.addData("Hue", hsvValues[0]);

        // change the background color to match the color detected by the RGB sensor.
        // pass a reference to the hue, saturation, and value array as an argument
        // to the HSVToColor method.
        relativeLayout.post(new Runnable() {
            public void run() {
                relativeLayout.setBackgroundColor(Color.HSVToColor(0xff, values));
            }
        });

        telemetry.update();
    }

    // Set the panel back to the default color
    relativeLayout.post(new Runnable() {
        public void run() {
            relativeLayout.setBackgroundColor(Color.WHITE);
        }
    });
}
 
Example 8
Project: simple-share-android   File: Utils.java   View Source Code Vote up 5 votes
public static int getComplementaryColor(int colorToInvert) {
    float[] hsv = new float[3];
    Color.RGBToHSV(Color.red(colorToInvert), Color.green(colorToInvert),
            Color.blue(colorToInvert), hsv);
    hsv[0] = (hsv[0] + 180) % 360;
    return Color.HSVToColor(hsv);
}
 
Example 9
Project: Team9261-2017-2018   File: SensorHTColor.java   View Source Code Vote up 4 votes
@Override
public void runOpMode() {

  // hsvValues is an array that will hold the hue, saturation, and value information.
  float hsvValues[] = {0F,0F,0F};

  // values is a reference to the hsvValues array.
  final float values[] = hsvValues;

  // get a reference to the RelativeLayout so we can change the background
  // color of the Robot Controller app to match the hue detected by the RGB sensor.
  int relativeLayoutId = hardwareMap.appContext.getResources().getIdentifier("RelativeLayout", "id", hardwareMap.appContext.getPackageName());
  final View relativeLayout = ((Activity) hardwareMap.appContext).findViewById(relativeLayoutId);

  // bPrevState and bCurrState represent the previous and current state of the button.
  boolean bPrevState = false;
  boolean bCurrState = false;

  // bLedOn represents the state of the LED.
  boolean bLedOn = true;

  // get a reference to our ColorSensor object.
  colorSensor = hardwareMap.colorSensor.get("sensor_color");

  // turn the LED on in the beginning, just so user will know that the sensor is active.
  colorSensor.enableLed(bLedOn);

  // wait for the start button to be pressed.
  waitForStart();

  // loop and read the RGB data.
  // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
  while (opModeIsActive())  {

    // check the status of the x button on gamepad1.
    bCurrState = gamepad1.x;

    // check for button state transitions.
    if (bCurrState && (bCurrState != bPrevState))  {

      // button is transitioning to a pressed state.  Toggle LED.
      // on button press, enable the LED.
      bLedOn = !bLedOn;
      colorSensor.enableLed(bLedOn);
    }

    // update previous state variable.
    bPrevState = bCurrState;

    // convert the RGB values to HSV values.
    Color.RGBToHSV(colorSensor.red(), colorSensor.green(), colorSensor.blue(), hsvValues);

    // send the info back to driver station using telemetry function.
    telemetry.addData("LED", bLedOn ? "On" : "Off");
    telemetry.addData("Clear", colorSensor.alpha());
    telemetry.addData("Red  ", colorSensor.red());
    telemetry.addData("Green", colorSensor.green());
    telemetry.addData("Blue ", colorSensor.blue());
    telemetry.addData("Hue", hsvValues[0]);

    // change the background color to match the color detected by the RGB sensor.
    // pass a reference to the hue, saturation, and value array as an argument
    // to the HSVToColor method.
    relativeLayout.post(new Runnable() {
      public void run() {
        relativeLayout.setBackgroundColor(Color.HSVToColor(0xff, values));
      }
    });

    telemetry.update();
  }

  // Set the panel back to the default color
  relativeLayout.post(new Runnable() {
    public void run() {
      relativeLayout.setBackgroundColor(Color.WHITE);
    }
  });

}
 
Example 10
Project: GitHub   File: BubbleChartRenderer.java   View Source Code Vote up 4 votes
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {

    BubbleData bubbleData = mChart.getBubbleData();

    float phaseY = mAnimator.getPhaseY();

    for (Highlight high : indices) {

        IBubbleDataSet set = bubbleData.getDataSetByIndex(high.getDataSetIndex());

        if (set == null || !set.isHighlightEnabled())
            continue;

        final BubbleEntry entry = set.getEntryForXValue(high.getX(), high.getY());

        if (entry.getY() != high.getY())
            continue;

        if (!isInBoundsX(entry, set))
            continue;

        Transformer trans = mChart.getTransformer(set.getAxisDependency());

        sizeBuffer[0] = 0f;
        sizeBuffer[2] = 1f;

        trans.pointValuesToPixel(sizeBuffer);

        boolean normalizeSize = set.isNormalizeSizeEnabled();

        // calcualte the full width of 1 step on the x-axis
        final float maxBubbleWidth = Math.abs(sizeBuffer[2] - sizeBuffer[0]);
        final float maxBubbleHeight = Math.abs(
                mViewPortHandler.contentBottom() - mViewPortHandler.contentTop());
        final float referenceSize = Math.min(maxBubbleHeight, maxBubbleWidth);

        pointBuffer[0] = entry.getX();
        pointBuffer[1] = (entry.getY()) * phaseY;
        trans.pointValuesToPixel(pointBuffer);

        high.setDraw(pointBuffer[0], pointBuffer[1]);

        float shapeHalf = getShapeSize(entry.getSize(),
                set.getMaxSize(),
                referenceSize,
                normalizeSize) / 2f;

        if (!mViewPortHandler.isInBoundsTop(pointBuffer[1] + shapeHalf)
                || !mViewPortHandler.isInBoundsBottom(pointBuffer[1] - shapeHalf))
            continue;

        if (!mViewPortHandler.isInBoundsLeft(pointBuffer[0] + shapeHalf))
            continue;

        if (!mViewPortHandler.isInBoundsRight(pointBuffer[0] - shapeHalf))
            break;

        final int originalColor = set.getColor((int) entry.getX());

        Color.RGBToHSV(Color.red(originalColor), Color.green(originalColor),
                Color.blue(originalColor), _hsvBuffer);
        _hsvBuffer[2] *= 0.5f;
        final int color = Color.HSVToColor(Color.alpha(originalColor), _hsvBuffer);

        mHighlightPaint.setColor(color);
        mHighlightPaint.setStrokeWidth(set.getHighlightCircleWidth());
        c.drawCircle(pointBuffer[0], pointBuffer[1], shapeHalf, mHighlightPaint);
    }
}
 
Example 11
Project: FTC2016   File: SensorHTColor.java   View Source Code Vote up 4 votes
@Override
public void runOpMode() {

  // hsvValues is an array that will hold the hue, saturation, and value information.
  float hsvValues[] = {0F,0F,0F};

  // values is a reference to the hsvValues array.
  final float values[] = hsvValues;

  // get a reference to the RelativeLayout so we can change the background
  // color of the Robot Controller app to match the hue detected by the RGB sensor.
  final View relativeLayout = ((Activity) hardwareMap.appContext).findViewById(R.id.RelativeLayout);

  // bPrevState and bCurrState represent the previous and current state of the button.
  boolean bPrevState = false;
  boolean bCurrState = false;

  // bLedOn represents the state of the LED.
  boolean bLedOn = true;

  // get a reference to our ColorSensor object.
  colorSensor = hardwareMap.colorSensor.get("sensor_color");

  // turn the LED on in the beginning, just so user will know that the sensor is active.
  colorSensor.enableLed(bLedOn);

  // wait for the start button to be pressed.
  waitForStart();

  // loop and read the RGB data.
  // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
  while (opModeIsActive())  {

    // check the status of the x button on either gamepad.
    bCurrState = gamepad1.x;

    // check for button state transitions.
    if ((bCurrState == true) && (bCurrState != bPrevState))  {

      // button is transitioning to a pressed state.  Toggle LED.
      // on button press, enable the LED.
      bLedOn = !bLedOn;
      colorSensor.enableLed(bLedOn);
    }

    // update previous state variable.
    bPrevState = bCurrState;

    // convert the RGB values to HSV values.
    Color.RGBToHSV(colorSensor.red(), colorSensor.green(), colorSensor.blue(), hsvValues);

    // send the info back to driver station using telemetry function.
    telemetry.addData("LED", bLedOn ? "On" : "Off");
    telemetry.addData("Clear", colorSensor.alpha());
    telemetry.addData("Red  ", colorSensor.red());
    telemetry.addData("Green", colorSensor.green());
    telemetry.addData("Blue ", colorSensor.blue());
    telemetry.addData("Hue", hsvValues[0]);

    // change the background color to match the color detected by the RGB sensor.
    // pass a reference to the hue, saturation, and value array as an argument
    // to the HSVToColor method.
    relativeLayout.post(new Runnable() {
      public void run() {
        relativeLayout.setBackgroundColor(Color.HSVToColor(0xff, values));
      }
    });

    telemetry.update();
  }
}
 
Example 12
Project: FTC2016   File: SensorAdafruitRGB.java   View Source Code Vote up 4 votes
@Override
public void runOpMode() {

  // hsvValues is an array that will hold the hue, saturation, and value information.
  float hsvValues[] = {0F,0F,0F};

  // values is a reference to the hsvValues array.
  final float values[] = hsvValues;

  // get a reference to the RelativeLayout so we can change the background
  // color of the Robot Controller app to match the hue detected by the RGB sensor.
  final View relativeLayout = ((Activity) hardwareMap.appContext).findViewById(R.id.RelativeLayout);

  // bPrevState and bCurrState represent the previous and current state of the button.
  boolean bPrevState = false;
  boolean bCurrState = false;

  // bLedOn represents the state of the LED.
  boolean bLedOn = true;

  // get a reference to our DeviceInterfaceModule object.
  cdim = hardwareMap.deviceInterfaceModule.get("dim");

  // set the digital channel to output mode.
  // remember, the Adafruit sensor is actually two devices.
  // It's an I2C sensor and it's also an LED that can be turned on or off.
  cdim.setDigitalChannelMode(LED_CHANNEL, DigitalChannelController.Mode.OUTPUT);

  // get a reference to our ColorSensor object.
  sensorRGB = hardwareMap.colorSensor.get("sensor_color");

  // turn the LED on in the beginning, just so user will know that the sensor is active.
  cdim.setDigitalChannelState(LED_CHANNEL, bLedOn);

  // wait for the start button to be pressed.
  waitForStart();

  // loop and read the RGB data.
  // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
  while (opModeIsActive())  {

    // check the status of the x button on gamepad.
    bCurrState = gamepad1.x;

    // check for button-press state transitions.
    if ((bCurrState == true) && (bCurrState != bPrevState))  {

      // button is transitioning to a pressed state. Toggle the LED.
      bLedOn = !bLedOn;
      cdim.setDigitalChannelState(LED_CHANNEL, bLedOn);
    }

    // update previous state variable.
    bPrevState = bCurrState;

    // convert the RGB values to HSV values.
    Color.RGBToHSV((sensorRGB.red() * 255) / 800, (sensorRGB.green() * 255) / 800, (sensorRGB.blue() * 255) / 800, hsvValues);

    // send the info back to driver station using telemetry function.
    telemetry.addData("LED", bLedOn ? "On" : "Off");
    telemetry.addData("Clear", sensorRGB.alpha());
    telemetry.addData("Red  ", sensorRGB.red());
    telemetry.addData("Green", sensorRGB.green());
    telemetry.addData("Blue ", sensorRGB.blue());
    telemetry.addData("Hue", hsvValues[0]);

    // change the background color to match the color detected by the RGB sensor.
    // pass a reference to the hue, saturation, and value array as an argument
    // to the HSVToColor method.
    relativeLayout.post(new Runnable() {
      public void run() {
        relativeLayout.setBackgroundColor(Color.HSVToColor(0xff, values));
      }
    });

    telemetry.update();
  }
}
 
Example 13
Project: Auto.js   File: Colors.java   View Source Code Vote up 4 votes
public void RGBToHSV(int red, int green, int blue, float[] hsv) {
    Color.RGBToHSV(red, green, blue, hsv);
}
 
Example 14
Project: Ftc2018RelicRecovery   File: SensorMRColor.java   View Source Code Vote up 4 votes
@Override
public void runOpMode() {

  // hsvValues is an array that will hold the hue, saturation, and value information.
  float hsvValues[] = {0F,0F,0F};

  // values is a reference to the hsvValues array.
  final float values[] = hsvValues;

  // get a reference to the RelativeLayout so we can change the background
  // color of the Robot Controller app to match the hue detected by the RGB sensor.
  int relativeLayoutId = hardwareMap.appContext.getResources().getIdentifier("RelativeLayout", "id", hardwareMap.appContext.getPackageName());
  final View relativeLayout = ((Activity) hardwareMap.appContext).findViewById(relativeLayoutId);

  // bPrevState and bCurrState represent the previous and current state of the button.
  boolean bPrevState = false;
  boolean bCurrState = false;

  // bLedOn represents the state of the LED.
  boolean bLedOn = true;

  // get a reference to our ColorSensor object.
  colorSensor = hardwareMap.get(ColorSensor.class, "sensor_color");

  // Set the LED in the beginning
  colorSensor.enableLed(bLedOn);

  // wait for the start button to be pressed.
  waitForStart();

  // while the op mode is active, loop and read the RGB data.
  // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
  while (opModeIsActive()) {

    // check the status of the x button on either gamepad.
    bCurrState = gamepad1.x;

    // check for button state transitions.
    if (bCurrState && (bCurrState != bPrevState))  {

      // button is transitioning to a pressed state. So Toggle LED
      bLedOn = !bLedOn;
      colorSensor.enableLed(bLedOn);
    }

    // update previous state variable.
    bPrevState = bCurrState;

    // convert the RGB values to HSV values.
    Color.RGBToHSV(colorSensor.red() * 8, colorSensor.green() * 8, colorSensor.blue() * 8, hsvValues);

    // send the info back to driver station using telemetry function.
    telemetry.addData("LED", bLedOn ? "On" : "Off");
    telemetry.addData("Clear", colorSensor.alpha());
    telemetry.addData("Red  ", colorSensor.red());
    telemetry.addData("Green", colorSensor.green());
    telemetry.addData("Blue ", colorSensor.blue());
    telemetry.addData("Hue", hsvValues[0]);

    // change the background color to match the color detected by the RGB sensor.
    // pass a reference to the hue, saturation, and value array as an argument
    // to the HSVToColor method.
    relativeLayout.post(new Runnable() {
      public void run() {
        relativeLayout.setBackgroundColor(Color.HSVToColor(0xff, values));
      }
    });

    telemetry.update();
  }

  // Set the panel back to the default color
  relativeLayout.post(new Runnable() {
    public void run() {
      relativeLayout.setBackgroundColor(Color.WHITE);
    }
  });
}
 
Example 15
Project: Ftc2018RelicRecovery   File: SensorHTColor.java   View Source Code Vote up 4 votes
@Override
public void runOpMode() {

  // hsvValues is an array that will hold the hue, saturation, and value information.
  float hsvValues[] = {0F,0F,0F};

  // values is a reference to the hsvValues array.
  final float values[] = hsvValues;

  // get a reference to the RelativeLayout so we can change the background
  // color of the Robot Controller app to match the hue detected by the RGB sensor.
  int relativeLayoutId = hardwareMap.appContext.getResources().getIdentifier("RelativeLayout", "id", hardwareMap.appContext.getPackageName());
  final View relativeLayout = ((Activity) hardwareMap.appContext).findViewById(relativeLayoutId);

  // bPrevState and bCurrState represent the previous and current state of the button.
  boolean bPrevState = false;
  boolean bCurrState = false;

  // bLedOn represents the state of the LED.
  boolean bLedOn = true;

  // get a reference to our ColorSensor object.
  colorSensor = hardwareMap.colorSensor.get("sensor_color");

  // turn the LED on in the beginning, just so user will know that the sensor is active.
  colorSensor.enableLed(bLedOn);

  // wait for the start button to be pressed.
  waitForStart();

  // loop and read the RGB data.
  // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
  while (opModeIsActive())  {

    // check the status of the x button on gamepad1.
    bCurrState = gamepad1.x;

    // check for button state transitions.
    if (bCurrState && (bCurrState != bPrevState))  {

      // button is transitioning to a pressed state.  Toggle LED.
      // on button press, enable the LED.
      bLedOn = !bLedOn;
      colorSensor.enableLed(bLedOn);
    }

    // update previous state variable.
    bPrevState = bCurrState;

    // convert the RGB values to HSV values.
    Color.RGBToHSV(colorSensor.red(), colorSensor.green(), colorSensor.blue(), hsvValues);

    // send the info back to driver station using telemetry function.
    telemetry.addData("LED", bLedOn ? "On" : "Off");
    telemetry.addData("Clear", colorSensor.alpha());
    telemetry.addData("Red  ", colorSensor.red());
    telemetry.addData("Green", colorSensor.green());
    telemetry.addData("Blue ", colorSensor.blue());
    telemetry.addData("Hue", hsvValues[0]);

    // change the background color to match the color detected by the RGB sensor.
    // pass a reference to the hue, saturation, and value array as an argument
    // to the HSVToColor method.
    relativeLayout.post(new Runnable() {
      public void run() {
        relativeLayout.setBackgroundColor(Color.HSVToColor(0xff, values));
      }
    });

    telemetry.update();
  }

  // Set the panel back to the default color
  relativeLayout.post(new Runnable() {
    public void run() {
      relativeLayout.setBackgroundColor(Color.WHITE);
    }
  });

}
 
Example 16
Project: RobotIGS   File: SensorMRColor.java   View Source Code Vote up 4 votes
@Override
public void runOpMode() {

  // hsvValues is an array that will hold the hue, saturation, and value information.
  float hsvValues[] = {0F,0F,0F};

  // values is a reference to the hsvValues array.
  final float values[] = hsvValues;

  // get a reference to the RelativeLayout so we can change the background
  // color of the Robot Controller app to match the hue detected by the RGB sensor.
  final View relativeLayout = ((Activity) hardwareMap.appContext).findViewById(R.id.RelativeLayout);

  // bPrevState and bCurrState represent the previous and current state of the button.
  boolean bPrevState = false;
  boolean bCurrState = false;

  // bLedOn represents the state of the LED.
  boolean bLedOn = true;

  // get a reference to our ColorSensor object.
  colorSensor = hardwareMap.colorSensor.get("sensor_color");

  // Set the LED in the beginning
  colorSensor.enableLed(bLedOn);

  // wait for the start button to be pressed.
  waitForStart();

  // while the op mode is active, loop and read the RGB data.
  // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
  while (opModeIsActive()) {

    // check the status of the x button on either gamepad.
    bCurrState = gamepad1.x;

    // check for button state transitions.
    if ((bCurrState == true) && (bCurrState != bPrevState))  {

      // button is transitioning to a pressed state. So Toggle LED
      bLedOn = !bLedOn;
      colorSensor.enableLed(bLedOn);
    }

    // update previous state variable.
    bPrevState = bCurrState;

    // convert the RGB values to HSV values.
    Color.RGBToHSV(colorSensor.red() * 8, colorSensor.green() * 8, colorSensor.blue() * 8, hsvValues);

    // send the info back to driver station using telemetry function.
    telemetry.addData("LED", bLedOn ? "On" : "Off");
    telemetry.addData("Clear", colorSensor.alpha());
    telemetry.addData("Red  ", colorSensor.red());
    telemetry.addData("Green", colorSensor.green());
    telemetry.addData("Blue ", colorSensor.blue());
    telemetry.addData("Hue", hsvValues[0]);

    // change the background color to match the color detected by the RGB sensor.
    // pass a reference to the hue, saturation, and value array as an argument
    // to the HSVToColor method.
    relativeLayout.post(new Runnable() {
      public void run() {
        relativeLayout.setBackgroundColor(Color.HSVToColor(0xff, values));
      }
    });

    telemetry.update();
  }
}
 
Example 17
Project: RobotIGS   File: SensorHTColor.java   View Source Code Vote up 4 votes
@Override
public void runOpMode() {

  // hsvValues is an array that will hold the hue, saturation, and value information.
  float hsvValues[] = {0F,0F,0F};

  // values is a reference to the hsvValues array.
  final float values[] = hsvValues;

  // get a reference to the RelativeLayout so we can change the background
  // color of the Robot Controller app to match the hue detected by the RGB sensor.
  final View relativeLayout = ((Activity) hardwareMap.appContext).findViewById(R.id.RelativeLayout);

  // bPrevState and bCurrState represent the previous and current state of the button.
  boolean bPrevState = false;
  boolean bCurrState = false;

  // bLedOn represents the state of the LED.
  boolean bLedOn = true;

  // get a reference to our ColorSensor object.
  colorSensor = hardwareMap.colorSensor.get("sensor_color");

  // turn the LED on in the beginning, just so user will know that the sensor is active.
  colorSensor.enableLed(bLedOn);

  // wait for the start button to be pressed.
  waitForStart();

  // loop and read the RGB data.
  // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
  while (opModeIsActive())  {

    // check the status of the x button on either gamepad.
    bCurrState = gamepad1.x;

    // check for button state transitions.
    if ((bCurrState == true) && (bCurrState != bPrevState))  {

      // button is transitioning to a pressed state.  Toggle LED.
      // on button press, enable the LED.
      bLedOn = !bLedOn;
      colorSensor.enableLed(bLedOn);
    }

    // update previous state variable.
    bPrevState = bCurrState;

    // convert the RGB values to HSV values.
    Color.RGBToHSV(colorSensor.red(), colorSensor.green(), colorSensor.blue(), hsvValues);

    // send the info back to driver station using telemetry function.
    telemetry.addData("LED", bLedOn ? "On" : "Off");
    telemetry.addData("Clear", colorSensor.alpha());
    telemetry.addData("Red  ", colorSensor.red());
    telemetry.addData("Green", colorSensor.green());
    telemetry.addData("Blue ", colorSensor.blue());
    telemetry.addData("Hue", hsvValues[0]);

    // change the background color to match the color detected by the RGB sensor.
    // pass a reference to the hue, saturation, and value array as an argument
    // to the HSVToColor method.
    relativeLayout.post(new Runnable() {
      public void run() {
        relativeLayout.setBackgroundColor(Color.HSVToColor(0xff, values));
      }
    });

    telemetry.update();
  }
}
 
Example 18
Project: RobotIGS   File: SensorAdafruitRGB.java   View Source Code Vote up 4 votes
@Override
public void runOpMode() {

  // hsvValues is an array that will hold the hue, saturation, and value information.
  float hsvValues[] = {0F,0F,0F};

  // values is a reference to the hsvValues array.
  final float values[] = hsvValues;

  // get a reference to the RelativeLayout so we can change the background
  // color of the Robot Controller app to match the hue detected by the RGB sensor.
  final View relativeLayout = ((Activity) hardwareMap.appContext).findViewById(R.id.RelativeLayout);

  // bPrevState and bCurrState represent the previous and current state of the button.
  boolean bPrevState = false;
  boolean bCurrState = false;

  // bLedOn represents the state of the LED.
  boolean bLedOn = true;

  // get a reference to our DeviceInterfaceModule object.
  cdim = hardwareMap.deviceInterfaceModule.get("dim");

  // set the digital channel to output mode.
  // remember, the Adafruit sensor is actually two devices.
  // It's an I2C sensor and it's also an LED that can be turned on or off.
  cdim.setDigitalChannelMode(LED_CHANNEL, DigitalChannelController.Mode.OUTPUT);

  // get a reference to our ColorSensor object.
  sensorRGB = hardwareMap.colorSensor.get("sensor_color");

  // turn the LED on in the beginning, just so user will know that the sensor is active.
  cdim.setDigitalChannelState(LED_CHANNEL, bLedOn);

  // wait for the start button to be pressed.
  waitForStart();

  // loop and read the RGB data.
  // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
  while (opModeIsActive())  {

    // check the status of the x button on gamepad.
    bCurrState = gamepad1.x;

    // check for button-press state transitions.
    if ((bCurrState == true) && (bCurrState != bPrevState))  {

      // button is transitioning to a pressed state. Toggle the LED.
      bLedOn = !bLedOn;
      cdim.setDigitalChannelState(LED_CHANNEL, bLedOn);
    }

    // update previous state variable.
    bPrevState = bCurrState;

    // convert the RGB values to HSV values.
    Color.RGBToHSV((sensorRGB.red() * 255) / 800, (sensorRGB.green() * 255) / 800, (sensorRGB.blue() * 255) / 800, hsvValues);

    // send the info back to driver station using telemetry function.
    telemetry.addData("LED", bLedOn ? "On" : "Off");
    telemetry.addData("Clear", sensorRGB.alpha());
    telemetry.addData("Red  ", sensorRGB.red());
    telemetry.addData("Green", sensorRGB.green());
    telemetry.addData("Blue ", sensorRGB.blue());
    telemetry.addData("Hue", hsvValues[0]);

    // change the background color to match the color detected by the RGB sensor.
    // pass a reference to the hue, saturation, and value array as an argument
    // to the HSVToColor method.
    relativeLayout.post(new Runnable() {
      public void run() {
        relativeLayout.setBackgroundColor(Color.HSVToColor(0xff, values));
      }
    });

    telemetry.update();
  }
}
 
Example 19
Project: Team9261-2017-2018   File: SensorREVColorDistance.java   View Source Code Vote up 4 votes
@Override
public void runOpMode() {

    // get a reference to the color sensor.
    sensorColor = hardwareMap.get(ColorSensor.class, "sensor_color_distance");

    // get a reference to the distance sensor that shares the same name.
    sensorDistance = hardwareMap.get(DistanceSensor.class, "sensor_color_distance");

    // hsvValues is an array that will hold the hue, saturation, and value information.
    float hsvValues[] = {0F, 0F, 0F};

    // values is a reference to the hsvValues array.
    final float values[] = hsvValues;

    // sometimes it helps to multiply the raw RGB values with a scale factor
    // to amplify/attentuate the measured values.
    final double SCALE_FACTOR = 255;

    // get a reference to the RelativeLayout so we can change the background
    // color of the Robot Controller app to match the hue detected by the RGB sensor.
    int relativeLayoutId = hardwareMap.appContext.getResources().getIdentifier("RelativeLayout", "id", hardwareMap.appContext.getPackageName());
    final View relativeLayout = ((Activity) hardwareMap.appContext).findViewById(relativeLayoutId);

    // wait for the start button to be pressed.
    waitForStart();

    // loop and read the RGB and distance data.
    // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
    while (opModeIsActive()) {
        // convert the RGB values to HSV values.
        // multiply by the SCALE_FACTOR.
        // then cast it back to int (SCALE_FACTOR is a double)
        Color.RGBToHSV((int) (sensorColor.red() * SCALE_FACTOR),
                (int) (sensorColor.green() * SCALE_FACTOR),
                (int) (sensorColor.blue() * SCALE_FACTOR),
                hsvValues);

        // send the info back to driver station using telemetry function.
        telemetry.addData("Distance (cm)",
                String.format(Locale.US, "%.02f", sensorDistance.getDistance(DistanceUnit.CM)));
        telemetry.addData("Alpha", sensorColor.alpha());
        telemetry.addData("Red  ", sensorColor.red());
        telemetry.addData("Green", sensorColor.green());
        telemetry.addData("Blue ", sensorColor.blue());
        telemetry.addData("Hue", hsvValues[0]);

        // change the background color to match the color detected by the RGB sensor.
        // pass a reference to the hue, saturation, and value array as an argument
        // to the HSVToColor method.
        relativeLayout.post(new Runnable() {
            public void run() {
                relativeLayout.setBackgroundColor(Color.HSVToColor(0xff, values));
            }
        });

        telemetry.update();
    }

    // Set the panel back to the default color
    relativeLayout.post(new Runnable() {
        public void run() {
            relativeLayout.setBackgroundColor(Color.WHITE);
        }
    });
}
 
Example 20
Project: Team9261-2017-2018   File: SensorMRColor.java   View Source Code Vote up 4 votes
@Override
public void runOpMode() {

  // hsvValues is an array that will hold the hue, saturation, and value information.
  float hsvValues[] = {0F,0F,0F};

  // values is a reference to the hsvValues array.
  final float values[] = hsvValues;

  // get a reference to the RelativeLayout so we can change the background
  // color of the Robot Controller app to match the hue detected by the RGB sensor.
  int relativeLayoutId = hardwareMap.appContext.getResources().getIdentifier("RelativeLayout", "id", hardwareMap.appContext.getPackageName());
  final View relativeLayout = ((Activity) hardwareMap.appContext).findViewById(relativeLayoutId);

  // bPrevState and bCurrState represent the previous and current state of the button.
  boolean bPrevState = false;
  boolean bCurrState = false;

  // bLedOn represents the state of the LED.
  boolean bLedOn = true;

  // get a reference to our ColorSensor object.
  colorSensor = hardwareMap.get(ColorSensor.class, "sensor_color");

  // Set the LED in the beginning
  colorSensor.enableLed(bLedOn);

  // wait for the start button to be pressed.
  waitForStart();

  // while the op mode is active, loop and read the RGB data.
  // Note we use opModeIsActive() as our loop condition because it is an interruptible method.
  while (opModeIsActive()) {

    // check the status of the x button on either gamepad.
    bCurrState = gamepad1.x;

    // check for button state transitions.
    if (bCurrState && (bCurrState != bPrevState))  {

      // button is transitioning to a pressed state. So Toggle LED
      bLedOn = !bLedOn;
      colorSensor.enableLed(bLedOn);
    }

    // update previous state variable.
    bPrevState = bCurrState;

    // convert the RGB values to HSV values.
    Color.RGBToHSV(colorSensor.red() * 8, colorSensor.green() * 8, colorSensor.blue() * 8, hsvValues);

    // send the info back to driver station using telemetry function.
    telemetry.addData("LED", bLedOn ? "On" : "Off");
    telemetry.addData("Clear", colorSensor.alpha());
    telemetry.addData("Red  ", colorSensor.red());
    telemetry.addData("Green", colorSensor.green());
    telemetry.addData("Blue ", colorSensor.blue());
    telemetry.addData("Hue", hsvValues[0]);

    // change the background color to match the color detected by the RGB sensor.
    // pass a reference to the hue, saturation, and value array as an argument
    // to the HSVToColor method.
    relativeLayout.post(new Runnable() {
      public void run() {
        relativeLayout.setBackgroundColor(Color.HSVToColor(0xff, values));
      }
    });

    telemetry.update();
  }

  // Set the panel back to the default color
  relativeLayout.post(new Runnable() {
    public void run() {
      relativeLayout.setBackgroundColor(Color.WHITE);
    }
  });
}