Skip to content

Instantly share code, notes, and snippets.

@f2prateek
Created September 2, 2013 19:43
Show Gist options
  • Save f2prateek/6416614 to your computer and use it in GitHub Desktop.
Save f2prateek/6416614 to your computer and use it in GitHub Desktop.
GradientColorHelper
/*
* Copyright 2013 Prateek Srivastava (@f2prateek)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.f2prateek.couchpotato.ui.util;
import android.graphics.Color;
public class GradientColorHelper {
final int startColor;
final int endColor;
final int scale;
public GradientColorHelper(int startColor, int endColor, int scale) {
this.startColor = startColor;
this.endColor = endColor;
this.scale = scale;
}
public int getColor(double point) {
double percentage = point / scale * 100;
int sr = (startColor >> 16) & 0xFF;
int sg = (startColor >> 8) & 0xFF;
int sb = (startColor >> 0) & 0xFF;
int er = (endColor >> 16) & 0xFF;
int eg = (endColor >> 8) & 0xFF;
int eb = (endColor >> 0) & 0xFF;
int red = sr + (int) (percentage * (er - sr));
int green = sg + (int) (percentage * (eg - sg));
int blue = sb + (int) (percentage * (eb - sb));
return Color.rgb(red, green, blue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment