Exemplo n.º 1
0
  public boolean equals(SgnColor o, int tolerance) {

    if (this == o) return true;

    if (o == null) return false;

    if (mColor == o.mColor) {
      return true;
    }

    if (tolerance == 0) {
      return false;
    }

    int r1 = Color.red(mColor);
    int g1 = Color.green(mColor);
    int b1 = Color.blue(mColor);
    int a1 = Color.alpha(mColor);

    int r2 = Color.red(o.mColor);
    int g2 = Color.green(o.mColor);
    int b2 = Color.blue(o.mColor);
    int a2 = Color.alpha(o.mColor);

    return Math.abs(r1 - r2) <= tolerance
        && Math.abs(g1 - g2) <= tolerance
        && Math.abs(b1 - b2) <= tolerance
        && Math.abs(a1 - a2) <= tolerance;
  }
Exemplo n.º 2
0
  /**
   * Calculate the color using the supplied angle.
   *
   * @param angle The selected color's position expressed as angle (in rad).
   * @return The ARGB value of the color on the color wheel at the specified angle.
   */
  private int calculateColor(float angle) {
    float unit = (float) (angle / (2 * Math.PI));
    if (unit < 0) {
      unit += 1;
    }

    if (unit <= 0) {
      mColor = COLORS[0];
      return COLORS[0];
    }
    if (unit >= 1) {
      mColor = COLORS[COLORS.length - 1];
      return COLORS[COLORS.length - 1];
    }

    float p = unit * (COLORS.length - 1);
    int i = (int) p;
    p -= i;

    int c0 = COLORS[i];
    int c1 = COLORS[i + 1];
    int a = ave(Color.alpha(c0), Color.alpha(c1), p);
    int r = ave(Color.red(c0), Color.red(c1), p);
    int g = ave(Color.green(c0), Color.green(c1), p);
    int b = ave(Color.blue(c0), Color.blue(c1), p);

    mColor = Color.argb(a, r, g, b);
    return Color.argb(a, r, g, b);
  }
Exemplo n.º 3
0
  @Override
  public void draw(Canvas canvas) {
    if (!isRunning()) fillCanvas(canvas, mCurColor, mCurColorTransparent);
    else {
      ColorChangeTask task = mTasks[mCurTask];

      if (mAnimProgress == 0f) fillCanvas(canvas, mCurColor, mCurColorTransparent);
      else if (mAnimProgress == 1f) fillCanvas(canvas, task.color, mNextColorTransparent);
      else if (task.isOut) {
        float radius = mMaxRadius * task.interpolator.getInterpolation(mAnimProgress);

        if (Color.alpha(task.color) == 255) fillCanvas(canvas, mCurColor, mCurColorTransparent);
        else fillCanvasWithHole(canvas, task, radius, mCurColorTransparent);

        fillCircle(canvas, task.x, task.y, radius, task.color, mNextColorTransparent);
      } else {
        float radius = mMaxRadius * task.interpolator.getInterpolation(mAnimProgress);

        if (Color.alpha(mCurColor) == 255) fillCanvas(canvas, task.color, mNextColorTransparent);
        else fillCanvasWithHole(canvas, task, radius, mNextColorTransparent);

        fillCircle(canvas, task.x, task.y, radius, mCurColor, mCurColorTransparent);
      }
    }
  }
Exemplo n.º 4
0
 protected int getGradientPartialColor(int minColor, int maxColor, float fraction) {
   int alpha =
       Math.round(fraction * Color.alpha(minColor) + (1 - fraction) * Color.alpha(maxColor));
   int r = Math.round(fraction * Color.red(minColor) + (1 - fraction) * Color.red(maxColor));
   int g = Math.round(fraction * Color.green(minColor) + (1 - fraction) * Color.green(maxColor));
   int b = Math.round(fraction * Color.blue(minColor) + (1 - fraction) * Color.blue((maxColor)));
   return Color.argb(alpha, r, g, b);
 }
  /** Return a {@link Color} that is (1-p) like {@code from} and p like {@code to}. */
  public static int blend(int from, int to, float weigh) {
    int a = (int) ((1 - weigh) * Color.alpha(from) + weigh * Color.alpha(to));
    int r = (int) ((1 - weigh) * Color.red(from) + weigh * Color.red(to));
    int g = (int) ((1 - weigh) * Color.green(from) + weigh * Color.green(to));
    int b = (int) ((1 - weigh) * Color.blue(from) + weigh * Color.blue(to));

    return Color.argb(a, r, g, b);
  }
Exemplo n.º 6
0
 private void resetAnimation() {
   mStartTime = SystemClock.uptimeMillis();
   mAnimProgress = 0f;
   mCurColorTransparent = Color.alpha(mCurColor) == 0;
   mNextColorTransparent = Color.alpha(mTasks[mCurTask].color) == 0;
   mMaxRadius = getMaxRadius(mTasks[mCurTask].x, mTasks[mCurTask].y, getBounds());
   mShader = null;
 }
Exemplo n.º 7
0
 private static int blendColors(int color1, int color2, float factor) {
   final float inverseFactor = 1f - factor;
   float a = (Color.alpha(color1) * factor) + (Color.alpha(color2) * inverseFactor);
   float r = (Color.red(color1) * factor) + (Color.red(color2) * inverseFactor);
   float g = (Color.green(color1) * factor) + (Color.green(color2) * inverseFactor);
   float b = (Color.blue(color1) * factor) + (Color.blue(color2) * inverseFactor);
   return Color.argb((int) a, (int) r, (int) g, (int) b);
 }
Exemplo n.º 8
0
  /**
   * @param from color value in the form 0xAARRGGBB.
   * @param to color value in the form 0xAARRGGBB.
   */
  public static int getGradientColor(int from, int to, float factor) {
    int r =
        calculateGradient(
            Color.red(from), Color.red(to), factor); // It's so annoying without lambda.
    int g = calculateGradient(Color.green(from), Color.green(to), factor);
    int b = calculateGradient(Color.blue(from), Color.blue(to), factor);
    int a = calculateGradient(Color.alpha(from), Color.alpha(to), factor);

    return Color.argb(a, r, g, b);
  }
 public static int getMiddleColor(int prevColor, int curColor, float factor) {
   if (prevColor == curColor) return curColor;
   if (factor == 0f) return prevColor;
   else if (factor == 1f) return curColor;
   int a = getMiddleValue(Color.alpha(prevColor), Color.alpha(curColor), factor);
   int r = getMiddleValue(Color.red(prevColor), Color.red(curColor), factor);
   int g = getMiddleValue(Color.green(prevColor), Color.green(curColor), factor);
   int b = getMiddleValue(Color.blue(prevColor), Color.blue(curColor), factor);
   return Color.argb(a, r, g, b);
 }
Exemplo n.º 10
0
 @ColorInt
 private static int blendColors(
     @ColorInt int color1, @ColorInt int color2, @FloatRange(from = 0f, to = 1f) float ratio) {
   final float inverseRatio = 1f - ratio;
   float a = (Color.alpha(color1) * inverseRatio) + (Color.alpha(color2) * ratio);
   float r = (Color.red(color1) * inverseRatio) + (Color.red(color2) * ratio);
   float g = (Color.green(color1) * inverseRatio) + (Color.green(color2) * ratio);
   float b = (Color.blue(color1) * inverseRatio) + (Color.blue(color2) * ratio);
   return Color.argb((int) a, (int) r, (int) g, (int) b);
 }
Exemplo n.º 11
0
  /** Composite two potentially translucent colors over each other and returns the result. */
  public static int compositeColors(int foreground, int background) {
    final float alpha1 = Color.alpha(foreground) / 255f;
    final float alpha2 = Color.alpha(background) / 255f;

    float a = (alpha1 + alpha2) * (1f - alpha1);
    float r = (Color.red(foreground) * alpha1) + (Color.red(background) * alpha2 * (1f - alpha1));
    float g =
        (Color.green(foreground) * alpha1) + (Color.green(background) * alpha2 * (1f - alpha1));
    float b = (Color.blue(foreground) * alpha1) + (Color.blue(background) * alpha2 * (1f - alpha1));

    return Color.argb((int) a, (int) r, (int) g, (int) b);
  }
Exemplo n.º 12
0
  /**
   * Returns the contrast ratio between {@code foreground} and {@code background}. {@code
   * background} must be opaque.
   *
   * <p>Formula defined <a
   * href="http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef">here</a>.
   */
  public static double calculateContrast(int foreground, int background) {
    if (Color.alpha(background) != 255) {
      throw new IllegalArgumentException("background can not be translucent");
    }
    if (Color.alpha(foreground) < 255) {
      // If the foreground is translucent, composite the foreground over the background
      foreground = compositeColors(foreground, background);
    }

    final double luminance1 = calculateLuminance(foreground) + 0.05;
    final double luminance2 = calculateLuminance(background) + 0.05;

    // Now return the lighter luminance divided by the darker luminance
    return Math.max(luminance1, luminance2) / Math.min(luminance1, luminance2);
  }
Exemplo n.º 13
0
  /**
   * Calculate the color selected by the pointer on the bar.
   *
   * @param coord Coordinate of the pointer.
   */
  private void calculateColor(int coord) {
    coord = coord - mBarPointerHaloRadius;
    if (coord < 0) {
      coord = 0;
    } else if (coord > mBarLength) {
      coord = mBarLength;
    }

    mColor = Color.HSVToColor(Math.round(mPosToOpacFactor * coord), mHSVColor);
    if (Color.alpha(mColor) > 250) {
      mColor = Color.HSVToColor(mHSVColor);
    } else if (Color.alpha(mColor) < 5) {
      mColor = Color.TRANSPARENT;
    }
  }
Exemplo n.º 14
0
  @Override
  public synchronized void onDrawFrame(GL10 gl) {

    mObserver.onDrawFrame();

    if (mBackgroundColorChanged) {
      gl.glClearColor(
          Color.red(mBackgroundColor) / 255f,
          Color.green(mBackgroundColor) / 255f,
          Color.blue(mBackgroundColor) / 255f,
          Color.alpha(mBackgroundColor) / 255f);
      mBackgroundColorChanged = false;
    }

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();

    if (USE_PERSPECTIVE_PROJECTION) {
      gl.glTranslatef(0, 0, -6f);
    }

    for (int i = 0; i < mCurlMeshes.size(); ++i) {
      mCurlMeshes.get(i).draw(gl);
    }
  }
Exemplo n.º 15
0
 public static int getDarkerColor(int color) {
   int alpha = Color.alpha(color);
   int red = Color.red(color);
   int green = Color.green(color);
   int blue = Color.blue(color);
   return Color.argb(alpha, (int) (red * 0.9), (int) (green * 0.9), (int) (blue * 0.9));
 }
Exemplo n.º 16
0
 /**
  * Sets the background color of the scene.
  *
  * @param color Android color integer.
  */
 public void setBackgroundColor(int color) {
   setBackgroundColor(
       Color.red(color) / 255f,
       Color.green(color) / 255f,
       Color.blue(color) / 255f,
       Color.alpha(color) / 255f);
 }
Exemplo n.º 17
0
  /**
   * Get the white icon corresponding to a poiType.
   *
   * @param poiType the PoiType or null for notes.
   * @return The white icon.
   */
  public Drawable getIconWhite(PoiType poiType) {
    Bitmap myBitmap =
        BitmapFactory.decodeResource(
            context.getResources(),
            poiType == null ? R.drawable.open_book : getIconDrawableId(poiType));
    myBitmap = myBitmap.copy(myBitmap.getConfig(), true);

    int[] allpixels = new int[myBitmap.getHeight() * myBitmap.getWidth()];

    myBitmap.getPixels(
        allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

    for (int i = 0; i < myBitmap.getHeight() * myBitmap.getWidth(); i++) {
      if (allpixels[i] != 0) {
        int A = Color.alpha(allpixels[i]);
        // inverting byte for each R/G/B channel
        int R = 255 - Color.red(allpixels[i]);
        int G = 255 - Color.green(allpixels[i]);
        int B = 255 - Color.blue(allpixels[i]);
        // set newly-inverted pixel to output image
        allpixels[i] = Color.argb(A, R, G, B);
      }
    }

    myBitmap.setPixels(
        allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

    return new BitmapDrawable(context.getResources(), myBitmap);
  }
Exemplo n.º 18
0
    private int rotateColor(int color, float rad) {
      float deg = rad * 180 / 3.1415927f;
      int r = Color.red(color);
      int g = Color.green(color);
      int b = Color.blue(color);

      ColorMatrix cm = new ColorMatrix();
      ColorMatrix tmp = new ColorMatrix();

      cm.setRGB2YUV();
      tmp.setRotate(0, deg);
      cm.postConcat(tmp);
      tmp.setYUV2RGB();
      cm.postConcat(tmp);

      final float[] a = cm.getArray();

      int ir = floatToByte(a[0] * r + a[1] * g + a[2] * b);
      int ig = floatToByte(a[5] * r + a[6] * g + a[7] * b);
      int ib = floatToByte(a[10] * r + a[11] * g + a[12] * b);

      return Color.argb(
          Color.alpha(color), pinToByte(ir),
          pinToByte(ig), pinToByte(ib));
    }
Exemplo n.º 19
0
 private static int argbColor(int colorResource) {
   return Color.argb(
       Color.alpha(colorResource),
       Color.red(colorResource),
       Color.green(colorResource),
       Color.blue(colorResource));
 }
Exemplo n.º 20
0
 /**
  * The material's diffuse color. This can be overwritten by {@link Object3D#setColor(int)}. This
  * color will be applied to the whole object. For vertex colors use {@link
  * Material#useVertexColors(boolean)} and {@link Material#setVertexColors(int)}.
  *
  * @param int color The color to be used. Color.RED for instance. Or 0xffff0000.
  */
 public void setColor(int color) {
   mColor[0] = (float) Color.red(color) / 255.f;
   mColor[1] = (float) Color.green(color) / 255.f;
   mColor[2] = (float) Color.blue(color) / 255.f;
   mColor[3] = (float) Color.alpha(color) / 255.f;
   if (mVertexShader != null) mVertexShader.setColor(mColor);
 }
 private int getInverseColor(int color) {
   int red = Color.red(color);
   int green = Color.green(color);
   int blue = Color.blue(color);
   int alpha = Color.alpha(color);
   return Color.argb(alpha, 255 - red, 255 - green, 255 - blue);
 }
 private int changeColorAlpha(int color, float value) {
   int alpha = Math.round(Color.alpha(color) * value);
   int red = Color.red(color);
   int green = Color.green(color);
   int blue = Color.blue(color);
   return Color.argb(alpha, red, green, blue);
 }
  /**
   * Adjust the line width used when the hide animation is taking place. This will reduce the line
   * width from the original width to nothing over time. At the same this the alpha of the line will
   * be reduced to nothing
   */
  protected void processRevealEffect() {
    if ((mDrawMode != DecoEvent.EventType.EVENT_HIDE)
        && (mDrawMode != DecoEvent.EventType.EVENT_SHOW)) {
      return;
    }

    float lineWidth = mSeriesItem.getLineWidth();
    if (mPercentComplete > 0) {
      lineWidth *= (1.0f - mPercentComplete);
      mPaint.setAlpha((int) (Color.alpha(mSeriesItem.getColor()) * (1.0f - mPercentComplete)));
    } else {
      mPaint.setAlpha(Color.alpha(mSeriesItem.getColor()));
    }

    mPaint.setStrokeWidth(lineWidth);
  }
Exemplo n.º 24
0
  public void refresh() {
    int alpha = Color.alpha(mButtonColor);
    float[] hsv = new float[3];
    Color.colorToHSV(mButtonColor, hsv);
    hsv[2] *= 0.8f; // value component
    // if shadow color was not defined, generate shadow color = 80% brightness
    if (!isShadowColorDefined) {
      mShadowColor = Color.HSVToColor(alpha, hsv);
    }
    // Create pressed background and unpressed background drawables

    if (this.isEnabled()) {
      if (isShadowEnabled) {
        pressedDrawable = createDrawable(mCornerRadius, Color.TRANSPARENT, mButtonColor);
        unpressedDrawable = createDrawable(mCornerRadius, mButtonColor, mShadowColor);
      } else {
        mShadowHeight = 0;
        pressedDrawable = createDrawable(mCornerRadius, mShadowColor, Color.TRANSPARENT);
        unpressedDrawable = createDrawable(mCornerRadius, mButtonColor, Color.TRANSPARENT);
      }
    } else {
      Color.colorToHSV(mButtonColor, hsv);
      hsv[1] *= 0.25f; // saturation component
      int disabledColor = mShadowColor = Color.HSVToColor(alpha, hsv);
      // Disabled button does not have shadow
      pressedDrawable = createDrawable(mCornerRadius, disabledColor, Color.TRANSPARENT);
      unpressedDrawable = createDrawable(mCornerRadius, disabledColor, Color.TRANSPARENT);
    }
    updateBackground(unpressedDrawable);
    // Set padding
    this.setPadding(
        mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom + mShadowHeight);
  }
Exemplo n.º 25
0
  /**
   * Set the color to be highlighted by the pointer. </br> </br> If the instances {@code SVBar} and
   * the {@code OpacityBar} aren't null the color will also be set to them
   *
   * @param color The RGB value of the color to highlight. If this is not a color displayed on the
   *     color wheel a very simple algorithm is used to map it to the color wheel. The resulting
   *     color often won't look close to the original color. This is especially true for shades of
   *     grey. You have been warned!
   */
  public void setColor(int color) {
    mAngle = colorToAngle(color);
    mPointerColor.setColor(calculateColor(mAngle));

    // check of the instance isn't null
    if (mOpacityBar != null) {
      // set the value of the opacity
      mOpacityBar.setColor(mColor);
      mOpacityBar.setOpacity(Color.alpha(color));
    }

    // check if the instance isn't null
    if (mSVbar != null) {
      // the array mHSV will be filled with the HSV values of the color.
      Color.colorToHSV(color, mHSV);
      mSVbar.setColor(mColor);

      // because of the design of the Saturation/Value bar,
      // we can only use Saturation or Value every time.
      // Here will be checked which we shall use.
      if (mHSV[1] < mHSV[2]) {
        mSVbar.setSaturation(mHSV[1]);
      } else { // if (mHSV[1] > mHSV[2]) {
        mSVbar.setValue(mHSV[2]);
      }
    }

    invalidate();
  }
  private void initBackgroundDimAnimation() {
    final int maxAlpha = Color.alpha(mBackgroundColor);
    final int red = Color.red(mBackgroundColor);
    final int green = Color.green(mBackgroundColor);
    final int blue = Color.blue(mBackgroundColor);

    mShowBackgroundAnimator = ValueAnimator.ofInt(0, maxAlpha);
    mShowBackgroundAnimator.setDuration(ANIMATION_DURATION);
    mShowBackgroundAnimator.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator animation) {
            Integer alpha = (Integer) animation.getAnimatedValue();
            setBackgroundColor(Color.argb(alpha, red, green, blue));
          }
        });

    mHideBackgroundAnimator = ValueAnimator.ofInt(maxAlpha, 0);
    mHideBackgroundAnimator.setDuration(ANIMATION_DURATION);
    mHideBackgroundAnimator.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator animation) {
            Integer alpha = (Integer) animation.getAnimatedValue();
            setBackgroundColor(Color.argb(alpha, red, green, blue));
          }
        });
  }
Exemplo n.º 27
0
  public static Bitmap doColorFilter(Bitmap src, double red, double green, double blue) {
    // image size
    int width = src.getWidth();
    int height = src.getHeight();
    // create output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    // color information
    int A, R, G, B;
    int pixel;

    // scan through all pixels
    for (int x = 0; x < width; ++x) {
      for (int y = 0; y < height; ++y) {
        // get pixel color
        pixel = src.getPixel(x, y);
        // apply filtering on each channel R, G, B
        A = Color.alpha(pixel);
        R = (int) (Color.red(pixel) * red);
        G = (int) (Color.green(pixel) * green);
        B = (int) (Color.blue(pixel) * blue);
        // set new color pixel to output bitmap
        bmOut.setPixel(x, y, Color.argb(A, R, G, B));
      }
    }

    // return final image
    return bmOut;
  }
Exemplo n.º 28
0
 public void setCurColor(int color) {
   if (mCurColor != color) {
     mCurColor = color;
     mCurColorTransparent = Color.alpha(mCurColor) == 0;
     invalidateSelf();
   }
 }
Exemplo n.º 29
0
  @SuppressWarnings("NullableProblems")
  @Override
  protected void onDraw(Canvas canvas) {

    // Draw Title Text
    if (mAttributes.isShowTitle()
        && mTitlePaint != null
        && mCurTitleProperty != null
        && mCurTitleProperty.mAlpha != 0) {
      CharSequence buf = getHint();
      if (buf != null) {
        mTitlePaint.setTextSize(mCurTitleProperty.mTextSize);

        int color = getCurrentTitleTextColor();
        int alpha = GeniusUi.modulateAlpha(Color.alpha(color), mCurTitleProperty.mAlpha);

        mTitlePaint.setColor(color);
        mTitlePaint.setAlpha(alpha);

        canvas.drawText(
            buf,
            0,
            buf.length(),
            mCurTitleProperty.mPaddingLeft,
            mCurTitleProperty.mPaddingTop + mCurTitleProperty.mTextSize,
            mTitlePaint);
      }
    }

    super.onDraw(canvas);
  }
Exemplo n.º 30
0
 private int getHighlightColor(int color, int amount) {
   return Color.argb(
       Math.min(255, Color.alpha(color)),
       Math.min(255, Color.red(color) + amount),
       Math.min(255, Color.green(color) + amount),
       Math.min(255, Color.blue(color) + amount));
 }