Java Code Examples for android.graphics.Color#RGBToHSV

The following examples show how to use android.graphics.Color#RGBToHSV . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ColorPickerView.java    From Atomic with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Set the color this view should show.
 * @param color The color that should be selected.
 * @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 2
Source File: MainActivity.java    From AndroidHeatMap with Apache License 2.0 6 votes vote down vote up
private static int doGradient(double value, double min, double max, int min_color, int max_color) {
    if (value >= max) {
        return max_color;
    }
    if (value <= min) {
        return min_color;
    }
    float[] hsvmin = new float[3];
    float[] hsvmax = new float[3];
    float frac = (float)((value - min) / (max - min));
    Color.RGBToHSV(Color.red(min_color), Color.green(min_color), Color.blue(min_color), hsvmin);
    Color.RGBToHSV(Color.red(max_color), Color.green(max_color), Color.blue(max_color), hsvmax);
    float[] retval = new float[3];
    for (int i = 0; i < 3; i++) {
        retval[i] = interpolate(hsvmin[i], hsvmax[i], frac);
    }
    return Color.HSVToColor(retval);
}
 
Example 3
Source File: ColorPickerView.java    From document-viewer with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Set the color this view should show.
 * @param color The color that should be selected.
 * @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
Source File: ColorPickerView.java    From ColorPicker with Apache License 2.0 6 votes vote down vote up
/**
 * 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
Source File: ColorPickerView.java    From GravityBox with Apache License 2.0 6 votes vote down vote up
/**
 * Set the color this view should show.
 * @param color The color that should be selected.
 * @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 6
Source File: ColorPickerView.java    From HomeGenie-Android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Set the color this view should show.
 * @param color The color that should be selected.
 * @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 7
Source File: Gradient.java    From android-maps-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Helper function for creation of color map
 * Interpolates between two given colors using their HSV values.
 *
 * @param color1 First color
 * @param color2 Second color
 * @param ratio  Between 0 to 1. Fraction of the distance between color1 and color2
 * @return Color associated with x2
 */
static int interpolateColor(int color1, int color2, float ratio) {

    int alpha = (int) ((Color.alpha(color2) - Color.alpha(color1)) * ratio + Color.alpha(color1));

    float[] hsv1 = new float[3];
    Color.RGBToHSV(Color.red(color1), Color.green(color1), Color.blue(color1), hsv1);
    float[] hsv2 = new float[3];
    Color.RGBToHSV(Color.red(color2), Color.green(color2), Color.blue(color2), hsv2);

    // adjust so that the shortest path on the color wheel will be taken
    if (hsv1[0] - hsv2[0] > 180) {
        hsv2[0] += 360;
    } else if (hsv2[0] - hsv1[0] > 180) {
        hsv1[0] += 360;
    }

    // Interpolate using calculated ratio
    float[] result = new float[3];
    for (int i = 0; i < 3; i++) {
        result[i] = (hsv2[i] - hsv1[i]) * (ratio) + hsv1[i];
    }

    return Color.HSVToColor(alpha, result);
}
 
Example 8
Source File: Field.java    From effective_android_sample with Apache License 2.0 5 votes vote down vote up
/**
 * @return the colorHue
 */
public float getColorHue() {

    float[] hsv = new float[3];

    if (colorRgb == null) {
        Color.RGBToHSV(255, 0, 0, hsv); // default:red
    } else {
        Color.RGBToHSV(colorRgb[0], colorRgb[1], colorRgb[2], hsv);
    }

    return hsv[0];
}
 
Example 9
Source File: Utils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
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 10
Source File: ColorUtil.java    From rubik-robot with MIT License 5 votes vote down vote up
/**
 * @param R Red in range 0..255
 * @param G Green in range 0..255
 * @param B Blue in range 0..255
 * @return HSB values: H is 0..360 degrees / 360 (0..1), S is 0..1, B is 0..1
 */
public static double[] RGBtoHSB(int R, int G, int B) {
    double[] result = new double[3];
    float[] hsb = new float[3];
    Color.RGBToHSV(R, G, B, hsb);
    result[0] = hsb[0];
    result[1] = hsb[1];
    result[2] = hsb[2];
    return result;
}
 
Example 11
Source File: RainbowGraphics.java    From rainbow with GNU Lesser General Public License v3.0 5 votes vote down vote up
public final float brightness(int what) {
    if (what != cacheHsbKey) {
        Color.RGBToHSV((what >> 16) & 0xff, (what >> 8) & 0xff, what & 0xff, cacheHsbValue);
        cacheHsbKey = what;
    }
    return cacheHsbValue[2] * colorModeZ;
}
 
Example 12
Source File: Tool.java    From MusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
public static int getContrastVersionForColor(int color) {
    float[] hsv = new float[3];
    Color.RGBToHSV(Color.red(color), Color.green(color), Color.blue(color),
            hsv);
    if (hsv[2] < 0.5) {
        hsv[2] = 0.7f;
    } else {
        hsv[2] = 0.3f;
    }
    hsv[1] = hsv[1] * 0.2f;
    return Color.HSVToColor(hsv);
}
 
Example 13
Source File: ColorSelector.java    From APDE with GNU General Public License v2.0 4 votes vote down vote up
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public void setRGB(float red, float green, float blue) {
	red = Math.min(Math.max(red, 0), 255);
	green = Math.min(Math.max(green, 0), 255);
	blue = Math.min(Math.max(blue, 0), 255);
	
	float[] hsb = new float[3];
	Color.RGBToHSV((int) red, (int) green, (int) blue, hsb);
	
	int color = Color.rgb((int) red, (int) green, (int) blue);
	
	hueStrip.setHue(hsb[0] / 360.0f);
	colorSquare.setHue(hsb[0] / 360.0f);
	colorSquare.setPoint(hsb[1] * colorSquare.getWidth(), (1.0f - hsb[2]) * colorSquare.getHeight());
	
	hsv[0].setValue(hsb[0]);
	hsv[1].setValue(hsb[1]);
	hsv[2].setValue(hsb[2]);
	
	rgb[0].setValue(red);
	rgb[1].setValue(green);
	rgb[2].setValue(blue);
	
	//Hex conversion
	hex.setText(String.format("#%06X", (0xFFFFFF & color)));
	//Make the text readable
	hex.setTextColor(hsb[2] < 0.5 ? Color.WHITE : Color.BLACK);
	
	//Show the selected color
	
	if(selectionBackground == null) {
		selectionBackground = ((GradientDrawable) context.getResources().getDrawable(R.drawable.color_selector_selection_background));
	}
	
	selectionBackground.setColor(color);
	
	if(android.os.Build.VERSION.SDK_INT >= 16) {
		hex.setBackground(selectionBackground);
	} else {
		hex.setBackgroundDrawable(selectionBackground);
	}
}
 
Example 14
Source File: BubbleChartRenderer.java    From iMoney with Apache License 2.0 4 votes vote down vote up
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {

    BubbleData bubbleData = mChart.getBubbleData();

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

    for (Highlight indice : indices) {

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

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

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

        int minx = dataSet.getEntryPosition(entryFrom);
        int maxx = Math.min(dataSet.getEntryPosition(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);
    }
}
 
Example 15
Source File: BubbleChartRenderer.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
@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 16
Source File: SensorMRColor.java    From FtcSamples with MIT License 4 votes vote down vote up
@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 17
Source File: SensorAdafruitRGB.java    From FtcSamples with MIT License 4 votes vote down vote up
@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 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, DigitalChannel.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();
  }

  // Set the panel back to the default color
  relativeLayout.post(new Runnable() {
    public void run() {
      relativeLayout.setBackgroundColor(Color.WHITE);
    }
  });
}
 
Example 18
Source File: SensorREVColorDistance.java    From FtcSamples with MIT License 4 votes vote down vote up
@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 19
Source File: BubbleChartRenderer.java    From Stayfit with Apache License 2.0 4 votes vote down vote up
@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);
    }
}
 
Example 20
Source File: BubbleChartRenderer.java    From NetKnight with Apache License 2.0 4 votes vote down vote up
@Override
public void drawHighlighted(Canvas c, Highlight[] indices) {

    BubbleData bubbleData = mChart.getBubbleData();

    float phaseX = Math.max(0.f, Math.min(1.f, mAnimator.getPhaseX()));
    float phaseY = mAnimator.getPhaseY();

    for (Highlight high : indices) {

        final int minDataSetIndex = high.getDataSetIndex() == -1
                ? 0
                : high.getDataSetIndex();
        final int maxDataSetIndex = high.getDataSetIndex() == -1
                ? bubbleData.getDataSetCount()
                : (high.getDataSetIndex() + 1);
        if (maxDataSetIndex - minDataSetIndex < 1) continue;

        for (int dataSetIndex = minDataSetIndex;
             dataSetIndex < maxDataSetIndex;
             dataSetIndex++) {

            IBubbleDataSet dataSet = bubbleData.getDataSetByIndex(dataSetIndex);

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

            final BubbleEntry entry = (BubbleEntry) bubbleData.getEntryForHighlight(high);
            if (entry == null || entry.getXIndex() != high.getXIndex())
                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());

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

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

            trans.pointValuesToPixel(sizeBuffer);

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

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

            if (high.getXIndex() < minx || high.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);
        }
    }
}