Skip to content

Instantly share code, notes, and snippets.

@av
Created September 29, 2019 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save av/aa5ea6542373395e24e81eeb1f8f9c17 to your computer and use it in GitHub Desktop.
Save av/aa5ea6542373395e24e81eeb1f8f9c17 to your computer and use it in GitHub Desktop.
Flutter: gradient stripes over x-axis
/// Main area of interest, this function will
/// return color for each particular color on our [ui.Image]
int generatePixel(int x, int y, Size size) {
/// Compute unified vector, values of its components
/// will be between 0 and 1
var uv = Vector2(x / size.width, y / size.height);
var xTiles = 10.0;
/// First of all, we scale uv.xxx ten times:
/// 0..1 * 10 -> 0..10
///
/// Then, for a particular position, it would look like:
/// 0.24 * 10 -> 2.4
///
/// Taking a fraction, would clamp resulting value back to
/// unit range:
/// frac(2.4) -> 0.4
///
/// So, below we will have 10 gradient stripes over x-axis.
return toColorInt(frac(uv.xxx * xTiles));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment