Skip to content

Instantly share code, notes, and snippets.

@abhishekgargx
Created December 13, 2018 07:43
Show Gist options
  • Select an option

  • Save abhishekgargx/f692988a7c1d6c0b4ef4f39b9cd0c1c0 to your computer and use it in GitHub Desktop.

Select an option

Save abhishekgargx/f692988a7c1d6c0b4ef4f39b9cd0c1c0 to your computer and use it in GitHub Desktop.
Color Blender for android , blend colors like instagram background
/*
* Copyright 2018 Abhishek Garg
*
* 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.abhishekgarg.background;
import android.view.View;
/**
* Updates the background of an activity as scrolled.
*/
public interface BackgroundManager {
/**
* Performs the update operation. This method may be called frequently, therefore it should not
* perform long running operations or block the UI thread.
* <p/>
* The leftPageIndex and offset parameters can be used to create backgrounds which depend on the
* exact scroll position. When scrolling between pages, there will always be a left page and a
* right page. The index parameter always refers to the left page. The offset parameter always
* indicates what fraction of the right page is currently visible, and varies between 0 and 1.
* When the offset is 0, the left page is entirely selected and the right page is not visible.
* When the offset is 0.5, the left page has been half scrolled out and the right page has been
* half scrolling in. The offset will approach 1 but never reach it (if 1 were reached then
* index would increment and offset would reset to 0).
*
* @param background
* the View to draw the background on, not null
* @param index
* the index of the current left page
* @param offset
* the fraction of the right page which is currently visible, as a value between 0 and 1
*/
void updateBackground(View background, int index, float offset);
}
/*
* Copyright 2018 Abhishek Garg
*
* 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.abhishekgarg.background;
import android.view.View;
import com.abhishekgarg.ColorHelper;
/**
* A ColorBlender is a BackgroundManager which presents a variable color as the background. Each
* page has a color associated with it, which will be displayed when that page is fully selected.
* When the scrolling is between pages, the colors are blended together to create a continuous color
* effect.
*/
public class ColorBlender implements BackgroundManager {
/**
* The colors to use for the backgrounds.
*/
private final int[] colors;
/**
* Constructs a new ColorBlender. The length of the array must match the number of pages in the
* IntroActivity this BackgroundManager is used with. The colors are mapped to the pages using
* the ordering of the array (e.g. page 1 maps to the color at index 0).
*
* @param colors
* the background colors to use, not null
* @throws IllegalArgumentException
* if {@code colors} is null or if the length of colors is less than 1
*/
public ColorBlender(final int[] colors) {
if (colors == null) {
throw new IllegalArgumentException("colors cannot be null");
} else if (colors.length == 0) {
throw new IllegalArgumentException("colors must have at least one element");
}
this.colors = colors;
}
@Override
public void updateBackground(final View background, final int index, final float offset) {
// Check that index doesn't exceed array bounds before progressing
if (index > colors.length - 1) {
throw new IllegalArgumentException("index is too large");
}
// The left color is always directly referenced by index
final int colorLeft = colors[index];
// Must be careful to avoid index out of bounds exceptions
final boolean isLast = index == colors.length - 1;
final int colorRight = isLast ? colors[index] : colors[index + 1];
// Blend the colors to make the final background color
background.setBackgroundColor(ColorHelper.blendColors(colorLeft, colorRight, offset));
}
}
/*
* Copyright 2016 Abhishek Garg
*
* 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.abhishekgarg.helpers;
import android.graphics.Color;
/**
* Helper class for working with colors.
*/
public abstract class ColorHelper {
/**
* Blends two colours to produce a single output colour. No check is done to ensure the provided
* colors are valid ARGB hex codes, and providing invalid codes will result in an undefined
* result. The blend works individually combining the ARGB components of the supplied colors and
* then synthesising the components back into one color.
*
* @param color1
* the first colour to blend, as an ARGB hex code
* @param color2
* the second colour to blend, as an ARGB hex code
* @param ratio
* the proportion of {@code color2} to use in the blended result, between 0 and 1
* (inclusive)
* @return the ARGB code for the blended colour
* @throws IllegalArgumentException
* if {@code ratio} is not between 0 and 1 (inclusive)
*/
public static int blendColors(final int color1, final int color2, final float ratio) {
if (ratio < 0 || ratio > 1) {
throw new IllegalArgumentException("ratio must be between 0 and 1 (inclusive)");
}
// Calculate the inverse ratio once and cache the result to improve time performance
final float inverseRatio = 1f - ratio;
// Combine the colors using the ARGB components
final float a = (Color.alpha(color1) * inverseRatio) + (Color.alpha(color2) * ratio);
final float r = (Color.red(color1) * inverseRatio) + (Color.red(color2) * ratio);
final float g = (Color.green(color1) * inverseRatio) + (Color.green(color2) * ratio);
final float b = (Color.blue(color1) * inverseRatio) + (Color.blue(color2) * ratio);
// Compose the result from by combining the ARGB components
return Color.argb((int) a, (int) r, (int) g, (int) b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment