Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- #if UNITY_EDITOR
- using UnityEditor;
- [CustomEditor(typeof(Palette_Tool))]
- public class Palette_ToolOverride : Editor
- {
- public override void OnInspectorGUI()
- {
- Palette_Tool _temp = (Palette_Tool)target;
- if(GUILayout.Button("Use image for palette"))
- {
- _temp.debugColourQuant();
- }
- DrawDefaultInspector();
- }
- }
- #endif
- [Serializable]
- public class Palette_Class {
- public int _palette_ID;
- public string _palette_Name;
- public Color _palette_Dominant;
- public Gradient _palette_Subs;
- }
- [Serializable]
- public struct Palette_Struct {
- public Color32 _colour_RGB;
- public float h, s, v;
- public int _count;
- public float _lum;
- public Palette_Struct( Color color , int count ){
- this._colour_RGB = color;
- Color.RGBToHSV ( this._colour_RGB , out this.h , out this.s , out this.v );
- this._count = count;
- this._lum = ((0.375f * this._colour_RGB.r)/255) + ((0.5f * this._colour_RGB.g)/255) + ((0.125f * this._colour_RGB.b)/255);
- return;
- }
- public void Set_Colour( Color32 _newColour ){
- this._colour_RGB = _newColour;
- Color.RGBToHSV ( this._colour_RGB , out this.h , out this.s , out this.v );
- this._lum = ((0.375f * this._colour_RGB.r)/255) + ((0.5f * this._colour_RGB.g)/255) + ((0.125f * this._colour_RGB.b)/255);
- }
- }
- public class PaletteSpaceCount: IComparer<Palette_Struct>
- {
- public int Compare(Palette_Struct x, Palette_Struct y)
- {
- int retval = y._count.CompareTo(x._count);
- return retval;
- }
- }
- public class PaletteSpaceHue: IComparer<Palette_Struct>
- {
- public int Compare(Palette_Struct x, Palette_Struct y)
- {
- float xHue = x.h;
- float yHue = y.h;
- int retval = 0;
- if( xHue < yHue){
- retval = 1;
- } else if ( xHue > yHue ){
- retval = -1;
- }
- return retval;
- }
- }
- public class PaletteSpaceVal: IComparer<Palette_Struct>
- {
- public int Compare(Palette_Struct x, Palette_Struct y)
- {
- int retval = (y._lum) .CompareTo( (x._lum) );
- return retval;
- }
- }
- public class Palette_Tool : MonoBehaviour {
- public int Quality = 4; // every so many pixels sample ..
- private int Quality_i; // every so many pixels sample ..
- public Texture2D Image;
- public void debugColourQuant(){
- ColourQuantize ( Image );
- }
- private Dictionary<int,int> _palettDict = new Dictionary<int, int>();
- private List<Color32> _paletteColours = new List<Color32> ();
- private List<float> _hueCutOffs = new List<float> ();
- private List<Palette_Struct> _currentHues = new List<Palette_Struct> ();
- private List<Palette_Struct> _palette_List_All = new List<Palette_Struct> ();
- private List<Palette_Struct> _palette_List = new List<Palette_Struct> ();
- private List<Palette_Struct> _palette_Clean_List = new List<Palette_Struct> ();
- PaletteSpaceCount _paletteSortCount = new PaletteSpaceCount();
- PaletteSpaceHue _paletteSortHue = new PaletteSpaceHue();
- PaletteSpaceVal _paletteSortVal = new PaletteSpaceVal();
- private int _colourHash = 0;
- private int _Size;
- private int _Count;
- private int _Count02;
- private int _CountHue;
- private int _intVal;
- private const float Hue_Sensitivity = 0.005f;
- private const float _darkestValAllowed = 0.25f;
- private const float _lightestValAllowed = 0.98f;
- private Palette_Struct _tempColour;
- private float _Avg_Hue;
- private float _Avg_Lum;
- private float _Avg_Sat;
- private int _Avg_Count;
- private int _Hi_Count;
- private float _power;
- public int _paletteSizeMax = 10;
- public List<Color> _palette_Slots = new List<Color> ();
- private void Add_NewHueCutOff( float _val ){
- if (_hueCutOffs.Contains (_val) == false) {
- _hueCutOffs.Add (_val);
- }
- }
- public void ColourQuantize( Texture2D tex ){
- // clear arrays //
- _palettDict.Clear (); _palette_List_All.Clear (); _palette_Clean_List.Clear();
- _palette_Slots.Clear (); _paletteColours.Clear ();
- _hueCutOffs.Clear ();
- Quality = Mathf.Clamp( Quality , 1, 9);
- Quality_i = 10 - Quality;
- // Hue_Sensitivity = Mathf.Clamp ( Hue_Sensitivity , 0f , 1f);
- Color32[] texColors = tex.GetPixels32();
- _Size = texColors.Length;
- for (_Count = 0; _Count < _Size; _Count += Quality_i) {
- _colourHash = ColorUtility.ToHtmlStringRGB( texColors [_Count] ).GetHashCode() ;
- _intVal = 0;
- if ( _palettDict .TryGetValue( _colourHash , out _intVal)) {
- _palettDict[ _colourHash ] +=1;
- } else {
- _palettDict.Add ( _colourHash , 1);
- _paletteColours.Add ( texColors [_Count] );
- }
- }
- for (_Count = 0; _Count < _paletteColours.Count; _Count ++) {
- _colourHash = ColorUtility.ToHtmlStringRGB( _paletteColours [_Count] ).GetHashCode() ;
- _intVal = _palettDict [_colourHash];
- if ( _intVal > 1 ) {
- _tempColour = new Palette_Struct ( _paletteColours [_Count] , _intVal );
- if( _tempColour._lum < _lightestValAllowed && _tempColour._lum > _darkestValAllowed ){
- if (_tempColour.s > .1f ) {
- _palette_List_All.Add (_tempColour);
- }
- }
- }
- }
- _palette_List_All.Sort ( _paletteSortHue );
- // find hue cutoffs .. RANGES //
- Add_NewHueCutOff ( 1f );
- for (_Count = 0; _Count < _palette_List_All.Count - 1; _Count++) {
- if( _palette_List_All[_Count].h - _palette_List_All[_Count + 1].h > Hue_Sensitivity ){
- if (_hueCutOffs.Count % 2 == 0) {
- Add_NewHueCutOff(_palette_List_All [_Count].h);
- } else {
- Add_NewHueCutOff ( _palette_List_All[_Count + 1].h );
- }
- }
- }
- Add_NewHueCutOff ( 0f);
- _Size = _hueCutOffs.Count - 1;
- for (_CountHue = 0; _CountHue < _Size; _CountHue++) {
- _currentHues.Clear ();
- _Avg_Hue = 0f; _Avg_Sat = 0f; _Avg_Lum = 0f; _Hi_Count = 0;
- for (_Count = 0; _Count < _palette_List_All.Count; _Count++) {
- if( _palette_List_All[_Count].h < _hueCutOffs[_CountHue] && _palette_List_All[_Count].h > _hueCutOffs[_CountHue + 1]){
- _currentHues.Add ( _palette_List_All[_Count] );
- _Hi_Count = Mathf.Max ( _Hi_Count , _palette_List_All [_Count]._count );
- }
- }
- //sort by strength!
- _currentHues.Sort ( _paletteSortVal );
- // find strength cutoff!
- _intVal = 0;
- if (_currentHues.Count > 0) {
- for (_Count = 0; _Count < _currentHues.Count; _Count++) {
- _Avg_Hue += _currentHues [_Count].h;
- _Avg_Sat += _currentHues [_Count].s;
- _Avg_Lum += _currentHues [_Count].v;
- // avg above gets more brighter colours instead ...
- // _power = Mathf.Max( 0.1f , (float)_currentHues [_Count]._count / _Hi_Count );
- // _Avg_Hue = Mathf.Lerp( _Avg_Hue , _currentHues [_Count].h , _power );
- // _Avg_Lum = Mathf.Lerp( _Avg_Lum , _currentHues [_Count].v , _power );
- // _Avg_Sat = Mathf.Lerp( _Avg_Sat , _currentHues [_Count].s , _power );
- _intVal += _currentHues [_Count]._count;
- }
- _Count02 = _currentHues.Count + 1;
- _Avg_Hue = _Avg_Hue / _Count02;
- _Avg_Sat = _Avg_Sat / _Count02;
- _Avg_Lum = _Avg_Lum / _Count02;
- _palette_Clean_List.Add (new Palette_Struct (Color.HSVToRGB (_Avg_Hue, _Avg_Sat, _Avg_Lum), _intVal));
- }
- }
- _palette_Clean_List.Sort ( _paletteSortVal );
- // // softening palette // is pretty meh ..
- // for (_Count = 0; _Count < _palette_Clean_List.Count; _Count++) {
- // if (_Count < _palette_Clean_List.Count / 2 - 1) {
- // _tempColour = _palette_Clean_List [_Count];
- // _tempColour._colour_RGB = Color.Lerp ( _tempColour._colour_RGB , Color.white , .05f );
- // _palette_Clean_List [_Count] = _tempColour;
- // } else {
- // if (_Count > _palette_Clean_List.Count / 2 + 1) {
- // _tempColour = _palette_Clean_List [_Count];
- // _tempColour._colour_RGB = Color.Lerp (_tempColour._colour_RGB, Color.black, .05f );
- // _palette_Clean_List [_Count] = _tempColour;
- // }
- // }
- // }
- // search final list for simialar colour!
- for (_Count = 0; _Count < _palette_Clean_List.Count; _Count++) {
- // get all closest colours to this one!
- for (_Count02 = 0; _Count02 < _palette_Clean_List.Count; _Count02++) {
- if (_Count != _Count02) {
- if ( _palette_Clean_List [_Count]._count > 1 && _palette_Clean_List [_Count02]._count > 1 ) {
- if ( Mathf.Abs( _palette_Clean_List [_Count].h - _palette_Clean_List [_Count02].h) < .04f) {
- if ( Mathf.Abs( _palette_Clean_List [_Count]._lum - _palette_Clean_List [_Count02]._lum) < .1f) {
- if (_palette_Clean_List [_Count]._lum > _palette_Clean_List [_Count02]._lum) {
- _tempColour._colour_RGB = Color.red;
- _Avg_Count = _palette_Clean_List [_Count02]._count;
- _tempColour._count = -1;
- _palette_Clean_List [_Count02] = _tempColour;
- _tempColour = _palette_Clean_List [_Count];
- _tempColour._count += _Avg_Count;
- _palette_Clean_List [_Count] = _tempColour;
- } else {
- _tempColour._colour_RGB = Color.red;
- _Avg_Count = _palette_Clean_List [_Count]._count;
- _tempColour._count = -1;
- _palette_Clean_List [_Count] = _tempColour;
- _tempColour = _palette_Clean_List [_Count02];
- _tempColour._count += _Avg_Count;
- _palette_Clean_List [_Count02] = _tempColour;
- }
- }
- }
- }
- }
- }
- }
- _palette_Clean_List.Sort ( _paletteSortCount );
- _palette_Slots.Add ( _palette_Clean_List[0]._colour_RGB );
- _palette_Clean_List.RemoveAt (0);
- for (_Count = _palette_Clean_List.Count - 1; _Count > 0; _Count--) {
- if ( _Count > _paletteSizeMax) {
- _palette_Clean_List.RemoveAt( _Count );
- }
- }
- _palette_Clean_List.Sort ( _paletteSortVal );
- for (_Count = 0; _Count < Mathf.Min( _paletteSizeMax , _palette_Clean_List.Count ); _Count++) {
- if (_palette_Clean_List [_Count]._count > 1) {
- _palette_Slots.Add (_palette_Clean_List [_Count]._colour_RGB);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement