public static Bitmap getChromakey(Context c) {
    Bitmap chroma = BitmapFactory.decodeResource(c.getResources(), R.drawable.test_chromakey);
    Bitmap bg = BitmapFactory.decodeResource(c.getResources(), R.drawable.landscape1);

    Bitmap composite = Bitmap.createBitmap(bg.getWidth(), bg.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(composite);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    // canvas.drawBitmap(bg, 0, 0, paint);
    // Slow method: scan all input (layer) image pixels and corresponding background pixels.
    // Calculate its "greenness" and translucency and recreate the pixels' values, plotting
    // them over the background.
    int bgPixel, lPixel;
    float targetHue = 97f / 360f;
    float tolerance = 0.1f;
    int bgRed, bgGreen, bgBlue, lRed, lGreen, lBlue, oRed, oGreen, oBlue;
    for (int w = 0; w < bg.getWidth(); w++)
      for (int h = 0; h < bg.getHeight(); h++) {
        // Background pixels.
        bgPixel = bg.getPixel(w, h);
        bgRed = Color.red(bgPixel); // Red level
        bgGreen = Color.green(bgPixel); // Green level
        bgBlue = Color.blue(bgPixel); // Blue level
        // Layer pixels.
        lPixel = chroma.getPixel(w, h);
        lRed = Color.red(lPixel); // Red level
        lGreen = Color.green(lPixel); // Green level
        lBlue = Color.blue(lPixel); // Blue level
        float[] lHSB = new float[3];

        Color.RGBToHSV(lRed, lGreen, lBlue, lHSB);
        // Calculate the translucency, based on the green value of the layer, using HSB coordinates.
        // To make calculations easier, let's assume that the translucency is a value between 0
        // (invisible) and 1 (opaque).
        float deltaHue = Math.abs((lHSB[0] / 360) - targetHue);
        float translucency = (deltaHue / tolerance);
        translucency = Math.min(translucency, 1f);
        // Recalculate the RGB coordinates of the layer and background pixels, using the
        // translucency
        // as a weight.
        oRed = (int) (translucency * lRed + (1 - translucency) * bgRed);
        oGreen = (int) (translucency * lGreen + (1 - translucency) * bgGreen);
        oBlue = (int) (translucency * lBlue + (1 - translucency) * bgBlue);
        // Set the pixel on the output image's raster.
        // raster.setPixel(w+shiftX,h+shiftY,new int[]{oRed,oGreen,oBlue,255});
        paint = new Paint();
        paint.setColor(Color.rgb(oRed, oGreen, oBlue));
        canvas.drawPoint(w, h, paint);
      } // end for
    return composite;
  }
Exemple #2
0
  public static int[] convertRgbToPowerLevel(int[] originalRgb, GCPowerLevel powerLevel) {
    int[] newRgbValues = new int[3];

    float[] hsv = new float[3];
    Color.RGBToHSV(originalRgb[0], originalRgb[1], originalRgb[2], hsv);

    hsv[1] = hsv[1] * powerLevel.getValue();

    int outputColor = Color.HSVToColor(hsv);
    newRgbValues[0] = Color.red(outputColor);
    newRgbValues[1] = Color.green(outputColor);
    newRgbValues[2] = Color.blue(outputColor);

    return newRgbValues;
  }
  public void runOpMode() throws InterruptedException {
    this.hardwareMap.logDevices();
    this.sensorRGB = (ColorSensor) this.hardwareMap.colorSensor.get("nxt");
    this.sensorRGB.enableLed(true);
    this.waitOneFullHardwareCycle();
    this.waitForStart();
    final float[] var1 = new float[] {0.0F, 0.0F, 0.0F};
    final View var2 = ((Activity) this.hardwareMap.appContext).findViewById(2131427353);
    boolean var3 = false;

    while (this.opModeIsActive()) {
      boolean var4;
      if (!this.gamepad1.x && !this.gamepad2.x) {
        var4 = false;
      } else {
        var4 = true;
      }

      if (var4 && var4 != var3) {
        DbgLog.msg("MY_DEBUG - x button was pressed!");
        var3 = var4;
        this.sensorRGB.enableLed(true);
      } else if (!var4 && var4 != var3) {
        DbgLog.msg("MY_DEBUG - x button was released!");
        var3 = var4;
        this.sensorRGB.enableLed(false);
      }

      Color.RGBToHSV(this.sensorRGB.red(), this.sensorRGB.green(), this.sensorRGB.blue(), var1);
      this.telemetry.addData("Clear", (float) this.sensorRGB.alpha());
      this.telemetry.addData("Red  ", (float) this.sensorRGB.red());
      this.telemetry.addData("Green", (float) this.sensorRGB.green());
      this.telemetry.addData("Blue ", (float) this.sensorRGB.blue());
      this.telemetry.addData("Hue", var1[0]);
      var2.post(
          new Runnable() {
            public void run() {
              var2.setBackgroundColor(Color.HSVToColor(255, var1));
            }
          });
      this.waitOneFullHardwareCycle();
    }
  }
  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();
  }
  @Override
  public void runOpMode() throws InterruptedException {

    // 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("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();
      idle(); // Always call idle() at the bottom of your while(opModeIsActive()) loop
    }
  }
Exemple #6
0
  @Override
  public void runOpMode() throws InterruptedException {

    // write some device information (connection info, name and type)
    // to the log file.
    hardwareMap.logDevices();

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

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

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

    // wait one cycle.
    waitOneFullHardwareCycle();

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

    // 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;

    // 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 || gamepad2.x;

      // check for button state transitions.
      if (bCurrState == true && bCurrState != bPrevState) {
        // button is transitioning to a pressed state.

        // print a debug statement.
        DbgLog.msg("MY_DEBUG - x button was pressed!");

        // update previous state variable.
        bPrevState = bCurrState;

        // on button press, enable the LED.
        bEnabled = true;

        // turn on the LED.
        sensorRGB.enableLed(bEnabled);
      } else if (bCurrState == false && bCurrState != bPrevState) {
        // button is transitioning to a released state.

        // print a debug statement.
        DbgLog.msg("MY_DEBUG - x button was released!");

        // update previous state variable.
        bPrevState = bCurrState;

        // on button press, enable the LED.
        bEnabled = false;

        // turn off the LED.
        sensorRGB.enableLed(false);
      }

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

      // send the info back to driver station using telemetry function.
      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));
            }
          });

      // wait a hardware cycle before iterating.
      waitOneFullHardwareCycle();
    }
  }
  @Override
  public void drawHighlighted(Canvas c, Highlight[] indices) {

    BubbleData bubbleData = mChart.getBubbleData();

    float phaseX = mAnimator.getPhaseX();
    float phaseY = mAnimator.getPhaseY();

    for (Highlight indice : indices) {

      IBubbleDataSet dataSet = bubbleData.getDataSetByIndex(indice.getDataSetIndex());

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

      BubbleEntry entryFrom = dataSet.getEntryForXIndex(mMinX);
      BubbleEntry entryTo = dataSet.getEntryForXIndex(mMaxX);

      int minx = dataSet.getEntryIndex(entryFrom);
      int maxx = Math.min(dataSet.getEntryIndex(entryTo) + 1, dataSet.getEntryCount());

      final BubbleEntry entry = (BubbleEntry) bubbleData.getEntryForHighlight(indice);
      if (entry == null || entry.getXIndex() != indice.getXIndex()) continue;

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

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

      trans.pointValuesToPixel(sizeBuffer);

      // 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] = (float) (entry.getXIndex() - minx) * phaseX + (float) minx;
      pointBuffer[1] = (float) (entry.getVal()) * phaseY;
      trans.pointValuesToPixel(pointBuffer);

      float shapeHalf = getShapeSize(entry.getSize(), dataSet.getMaxSize(), referenceSize) / 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;

      if (indice.getXIndex() < minx || indice.getXIndex() >= maxx) continue;

      final int originalColor = dataSet.getColor(entry.getXIndex());

      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(dataSet.getHighlightCircleWidth());
      c.drawCircle(pointBuffer[0], pointBuffer[1], shapeHalf, mHighlightPaint);
    }
  }
  @Override
  public void main() throws InterruptedException {

    // write some device information (connection info, name and type)
    // to the log file.
    hardwareMap.logDevices();

    // 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("color");

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

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

    // Set up our dashboard computations
    composeDashboard();

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

    // 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;

    // 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 || gamepad2.x;

      // check for button state transitions.
      if (bCurrState == true && bCurrState != bPrevState) {
        // button is transitioning to a pressed state.

        // print a debug statement.
        DbgLog.msg("MY_DEBUG - x button was pressed!");

        // update previous state variable.
        bPrevState = bCurrState;

        // on button press, enable the LED.
        bEnabled = true;

        // turn on the LED.
        cdim.setDigitalChannelState(LED_CHANNEL, bEnabled);

      } else if (bCurrState == false && bCurrState != bPrevState) {
        // button is transitioning to a released state.

        // print a debug statement.
        DbgLog.msg("MY_DEBUG - x button was released!");

        // update previous state variable.
        bPrevState = bCurrState;

        // on button press, enable the LED.
        bEnabled = false;

        // turn off the LED.
        cdim.setDigitalChannelState(LED_CHANNEL, bEnabled);
      }

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

      this.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));
            }
          });

      this.telemetry.update();
      this.idle();
    }
  }