Jump to content

XKCD Color Viewer


Recommended Posts

While trying to find good colors for my new CapCom mod I realized I was almost randomly picking things from the huge XKCDColors list. There are 954 colors in there and only the names to go by when trying to choose which one.

So I made this simple in-game UI to show each color, print its name, show a few variations, and print the RGB values. You can drag the window and scroll through the available colors, there is no way to close it.

You can change which scene it shows up in by changing the KSPAddon startup value in the code shown below.

Download the compiled plugin from DropBox. Unzip and put the file anywhere in the GameData folder.

JqBgyhE.png

For the code below all that you need to do is setup a project with references to KSP's Assembly-CSharp and UnityEngine.


/* License: Public Domain
*
* By DMagic - 2015
*/

using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace XKCDColorViewer
{
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
class XKCDColorPrintout : MonoBehaviour
{
Rect windowRect = new Rect(50, 50, 600, 600);
Vector2 scroll = new Vector2();
GUIStyle title, fonts, fontStyle, rgbValue;
Dictionary<string, Color> xColors = new Dictionary<string, Color>();
List<KeyValuePair<string, Color>> sortedColors = new List<KeyValuePair<string, Color>>();
bool loaded;

private void Start()
{
assignColors();
}

private void OnGUI()
{
if (!loaded)
{
title = new GUIStyle(GUI.skin.label);
title.fontSize = 14;
title.alignment = TextAnchor.MiddleLeft;
title.wordWrap = false;

fonts = new GUIStyle(title);
fonts.alignment = TextAnchor.MiddleCenter;
Texture2D blackBackground = new Texture2D(1, 1);
blackBackground.SetPixel(1, 1, Color.black);
blackBackground.Apply();
fonts.normal.background = blackBackground;
fonts.fontSize = 16;

fontStyle = new GUIStyle(title);
fontStyle.fontStyle = FontStyle.Bold;
fontStyle.alignment = TextAnchor.MiddleCenter;
Texture2D whiteBackground = new Texture2D(1, 1);
whiteBackground.SetPixel(1, 1, Color.white);
whiteBackground.Apply();
fontStyle.normal.background = whiteBackground;

rgbValue = new GUIStyle(title);
rgbValue.fontSize = 12;
loaded = true;
}

windowRect = GUI.Window("xkcdColors".GetHashCode(), windowRect, drawWindow, new GUIContent("XKCD Colors"));
}

private void drawWindow(int id)
{
Rect r = new Rect(2, 20, 595, 570);
Rect rView = new Rect(0, 0, 575, 30540);

scroll = GUI.BeginScrollView(r, scroll, rView);

Rect label = new Rect(4, 2, 140, 28);
int i = 0;
foreach (var c in sortedColors)
{
i++;
if (label.yMin >= (scroll.y - 20) && label.yMax <= (scroll.y + windowRect.height - 30))
{
title.normal.textColor = c.Value;
fonts.normal.textColor = c.Value;
fontStyle.normal.textColor = c.Value;
rgbValue.normal.textColor = c.Value;

string s = c.Key;


GUI.Label(label, i + ": " + s + ": ", title);
label.x += 140;
label.width = 110;
GUI.Label(label, "Font Size 16 ", fonts);
label.x += 120;
label.width = 115;
GUI.Label(label, "Font Style Bold", fontStyle);
label.x += 125;
label.width = 160;
GUI.Label(label, string.Format("R: {0:N3}, G: {1:N3}, B: {2:N3}", c.Value.r, c.Value.g, c.Value., rgbValue);
}
label.y += 32;
label.x = 4;
label.width = 140;
}

GUI.EndScrollView();
GUI.DragWindow();
}

private void assignColors()
{
Type xkcd = typeof(XKCDColors);

var properties = xkcd.GetProperties(BindingFlags.Static | BindingFlags.Public);

foreach (var prop in properties)
{
try
{
Color c = (Color)prop.GetValue(null, null);
string s = prop.Name;

if (xColors.ContainsKey(s))
continue;

xColors.Add(s, c);
}
catch
{
print("Failed to assign a color value");
continue;
}
}

foreach (var p in xColors)
sortedColors.Add(p);

sortedColors.Sort((a, => a.Key.CompareTo(b.Key));
}
}
}

Edited by DMagic
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...