void OnEnable()
    {
        // Title icon
#if UNITY_5_1_PLUS
        GUIContent windowTitle = new GUIContent(" Lutify", LoadIcon());
        titleContent = windowTitle;
#else
        title = "Lutify";
        GUIContent windowTitle = InternalGUIUtility.GetTitleContent(this);

        if (windowTitle != null)
        {
            windowTitle.text  = " Lutify";
            windowTitle.image = LoadIcon();
        }
#endif

        inst = this;
        FetchLuts();
    }
    void DrawPreview(Rect r, Texture2D lut, FilterMode filterMode)
    {
        Event e = Event.current;

        if (e.type == EventType.Repaint)
        {
            bool selected = lut == m_Lutify.LookupTexture;

            // Preview texture
            lut.filterMode = filterMode;
            float tileSize = Mathf.Sqrt((float)lut.width);
            PreviewMaterial.SetTexture("_LookupTex2D", lut);
            PreviewMaterial.SetVector("_Params", new Vector4(1f / (float)lut.width, 1f / (float)lut.height, tileSize - 1f, filterMode == FilterMode.Point ? 1f : 0f));
            PreviewMaterial.SetFloat("_BottomFrame", 17f * (1f / (float)m_Lutify._ThumbHeight));
            PreviewMaterial.SetColor("_FrameColor", selected ? Color.white : Color.black);
            EditorGUI.DrawPreviewTexture(r, m_Lutify._PreviewRT, PreviewMaterial);

            // Borders
            Handles.color = selected ? new Color(1f, 1f, 1f, 2f) : new Color(0f, 0f, 0f, 2f);             // Removes default alpha on handles
            Handles.DrawLine(new Vector3(r.x, r.y + 1, 0f), new Vector3(r.xMax, r.y + 1, 0f));
            Handles.DrawLine(new Vector3(r.x + 1, r.y + 1, 0f), new Vector3(r.x + 1, r.yMax, 0f));
            Handles.DrawLine(new Vector3(r.xMax, r.y + 1, 0f), new Vector3(r.xMax, r.yMax, 0f));

            // Name overlay (text)
            Color oldColor = GUI.color;
            GUI.color = selected ? new Color32(20, 20, 20, 255) : new Color32(156, 216, 246, 255);
            GUI.Label(new Rect(r.x + 4, r.yMax - 17, r.width - 8, 16), lut.name, EditorStyles.miniLabel);
            GUI.color = oldColor;

            // Selection shape
            if (selected)
            {
                Vector3[] verts = new Vector3[] {
                    new Vector3(r.xMax, r.y + 1, 0),
                    new Vector3(r.xMax, r.y + 18, 0),
                    new Vector3(r.xMax - 17, r.y + 1, 0),
                    new Vector3(r.xMax - 17, r.y + 1, 0)
                };

                Handles.DrawSolidRectangleWithOutline(verts, new Color(1f, 1f, 1f, 2f), new Color(1f, 1f, 1f, 2f));
            }
        }
        else if (e.type == EventType.MouseDown)
        {
            if (r.Contains(e.mousePosition))
            {
                if (e.button == 0)
                {
                    Undo.RecordObject(m_Lutify, "Set Lutify LUT");
                    m_Lutify.LutFiltering  = m_CollectionFilters[m_SelectedCollection];
                    m_Lutify.LookupTexture = lut;
                    InternalGUIUtility.RepaintGameView();
                }
                else
                {
                    GenericMenu menu = new GenericMenu();
                    menu.AddItem(new GUIContent("Show in project (Standard)"), false, () => EditorGUIUtility.PingObject(lut));
                    menu.AddItem(new GUIContent("Show in project (Scion)"), false, () => PingExtTexture(lut, "Scion"));
                    menu.AddItem(new GUIContent("Show in project (Amplify)"), false, () => PingExtTexture(lut, "AmplifyColor"));
                    menu.ShowAsContext();
                }

                e.Use();
            }
        }
    }
    void OnGUI()
    {
        if (m_Lutify == null)
        {
            return;             // Can happen when Unity is restarted and the LUT browser is still opened
        }
        // Header
        GUILayout.BeginHorizontal(EditorStyles.toolbar, GUILayout.ExpandWidth(true));
        {
            EditorGUI.BeginChangeCheck();

            m_SelectedCollection = EditorGUILayout.Popup(m_SelectedCollection, m_CollectionMenu, EditorStyles.toolbarPopup, GUILayout.MaxWidth(170f));

            if (EditorGUI.EndChangeCheck())
            {
                m_ScrollView = Vector2.zero;
                m_Lutify._LastSelectedCategory = m_SelectedCollection;
            }

            GUILayout.FlexibleSpace();

            EditorGUI.BeginChangeCheck();

            m_Lutify._ThumbWidth = Mathf.RoundToInt(GUILayout.HorizontalSlider(m_Lutify._ThumbWidth, 100f, 300f, new GUIStyle("preSlider"), new GUIStyle("preSliderThumb"), GUILayout.Width(64f)));

            if (EditorGUI.EndChangeCheck())
            {
                InternalGUIUtility.RepaintGameView();
            }

            if (GUILayout.Button("?", EditorStyles.toolbarButton))
            {
                Application.OpenURL("http://www.thomashourdel.com/lutify/");
            }
        }
        GUILayout.EndHorizontal();

        // Component check
        if (!m_Lutify.enabled)
        {
            EditorGUILayout.HelpBox("Lutify is disabled ! Please enable the component get the LUT browser to work.", MessageType.Error);

            if (GUILayout.Button("Enable"))
            {
                m_Lutify.enabled = true;
            }

            return;
        }

        // Gallery
        List <Texture2D> luts = null;

        if (!m_Collections.TryGetValue(m_CollectionNames[m_SelectedCollection], out luts) || luts.Count == 0)
        {
            return;
        }

        m_ScrollView = GUILayout.BeginScrollView(m_ScrollView, false, false, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));
        {
            int w       = Mathf.FloorToInt(position.width);
            int tw      = m_Lutify._ThumbWidth;
            int th      = m_Lutify._ThumbHeight;
            int spacing = 4;
            int margin  = 16;
            int c       = (w - margin) / (tw + spacing);

            GUILayout.BeginVertical();
            {
                GUILayout.Space(spacing);

                FilterMode filterMode  = m_CollectionFilters[m_SelectedCollection];
                bool       layoutState = false;
                int        j           = 0;

                for (int i = 0; i < luts.Count; i++)
                {
                    j++;

                    if (!layoutState)
                    {
                        GUILayout.BeginHorizontal();
                        GUILayout.FlexibleSpace();
                        layoutState = true;
                    }

                    Rect r = GUILayoutUtility.GetRect(tw, th);

                    if (m_Lutify._PreviewRT != null)
                    {
                        DrawPreview(r, luts[i], filterMode);
                    }

                    if (j < c && c != 1 && i != luts.Count - 1)
                    {
                        GUILayout.Space(spacing);
                    }

                    if (layoutState && j == c)
                    {
                        GUILayout.FlexibleSpace();
                        GUILayout.EndHorizontal();
                        GUILayout.Space(spacing);
                        layoutState = false;
                        j           = 0;
                    }
                }

                if (layoutState && j < c)
                {
                    GUILayout.FlexibleSpace();
                    GUILayout.EndHorizontal();
                }

                GUILayout.Space(spacing);
            }
            GUILayout.EndVertical();
        }
        GUILayout.EndScrollView();
    }