using System;
using UnityEngine;

namespace Blaze{

	public class RenderedGradient32{
		
		public int Length;
		public Color32[] Pixels;
		
		
		public RenderedGradient32(Color32[] pixels){
			Pixels=pixels;
			Length=Pixels.Length;
		}
		
		public Color32 this[int index]{
			get{
				return Pixels[index];
			}
			set{
				Pixels[index]=value;
			}
		}
		
		public Color32 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:
			Color32 next=Pixels[index+1];
			Color32 cur=Pixels[index];
			
			Color32 result=cur;
			
			result.r=(byte)( result.r + (position * (next.r-cur.r)) );
			result.g=(byte)( result.g + (position * (next.g-cur.g)) );
			result.b=(byte)( result.b + (position * (next.b-cur.b)) );
			result.a=(byte)( result.a + (position * (next.a-cur.a)) );
			
			return result;
		}
		
	}

}