Last active
February 14, 2019 13:29
-
-
Save alwarren/376c9c6d32adbfbde924dbf38da43ce5 to your computer and use it in GitHub Desktop.
Calculates a color value using a range pointer that lies somewhere between two colors. Originaly used an Android ProgressBar as the pointer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2016. Al Warren. | |
* | |
* 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. | |
*/ | |
import android.graphics.Color; | |
/** | |
* Range based color interpolator | |
* | |
* Calculates a color value using a range pointer that lies somewhere between | |
* two colors. In the original application,a ProgressBar value was used as | |
* the pointer. | |
*/ | |
class ColorInterpolator { | |
private static int RANGE_UNIT_MAX = 100; | |
/** | |
* Interpolate using integer color values. | |
* | |
* @param startColor int | |
* @param endColor int | |
* @param rangePointer int | |
* @return int | |
*/ | |
public int colorChange(int startColor, int endColor, int rangePointer) { | |
int red, green, blue; // rgb components | |
int deltaR, deltaG, deltaB; // change in rgb components | |
deltaR = Color.red(endColor) - Color.red(startColor); | |
deltaG = Color.green(endColor) - Color.green(startColor); | |
deltaB = Color.blue(endColor) - Color.blue(startColor); | |
red = Color.red(startColor) + (deltaR * rangePointer / RANGE_UNIT_MAX); | |
green = Color.green(startColor) + (deltaG * rangePointer / RANGE_UNIT_MAX); | |
blue = Color.blue(startColor) + (deltaB * rangePointer / RANGE_UNIT_MAX); | |
return Color.rgb(red, green, blue); | |
} | |
// interpolate using string color values | |
/** | |
* Interpolate using string color values | |
* | |
* @param startColor string | |
* @param endColor string | |
* @param rangePointer int | |
* @return int | |
*/ | |
public int colorChange(String startColor, String endColor, int rangePointer) { | |
int startR, startG, startB, endR, endG, endB; // rgb components | |
int deltaR, deltaG, deltaB, red, green, blue; // change in rgb components | |
int color; | |
color = Color.parseColor(startColor); | |
startR = Color.red(color); | |
startB = Color.blue(color); | |
startG = Color.green(color); | |
color = Color.parseColor(endColor); | |
endR = Color.red(color); | |
endB = Color.blue(color); | |
endG = Color.green(color); | |
deltaR = endR - startR; | |
deltaG = endG - startG; | |
deltaB = endB - startB; | |
red = startR + (int) Math.round(deltaR * rangePointer / RANGE_UNIT_MAX); | |
green = startG + (int) Math.round(deltaG * rangePointer / RANGE_UNIT_MAX); | |
blue = startB + (int) Math.round(deltaB * rangePointer / RANGE_UNIT_MAX); | |
return Color.rgb(red, green, blue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment