using System;
using UnityEngine;


namespace Blaze{

	public class RenderedGradient{
		
		public int Length;
		public Color[] Pixels;
		
		
		public RenderedGradient(Color[] pixels){
			Pixels=pixels;
			Length=Pixels.Length;
		}
		
		public Color this[int index]{
			get{
				return Pixels[index];
			}
			set{
				Pixels[index]=value;
			}
		}
		
		public Color SampleAt(float point){
			
			float position=point*Length;
			
			// Figure out the bottom index to interpolate from:
			int index=(int)position;
			
			if(index>=Length-1){
				return Pixels[Length-1];
			}else if(index<0){
				return Pixels[0];
			}
			
			// Make position relative to that bottom index:
			// This puts it into a 0->1 range.
			position-=index;
			
			// Interpolate, using position as the ratio:
			return Pixels[index]+(position*(Pixels[index+1]-Pixels[index]));
			
		}
		
	}

}