Hi, I want to choose a colour by typing the hex (or the RGB) into a field. Is my rough sketch (see below) the way to go?
Later, I will add a value for the Alpha to the string rather than type it into the text field and I will also add constraints so only correct numbers, etc. can be entered but first I wanted to check the general approach is correct.
String myText = "ff6fe442";
PFont font;
void setup() {
colorMode(RGB, 255, 255, 255);
size(300, 300);
// create the font
font = createFont("SansSerif", 14); //First use Processing > Tools > Create Font...
textFont(font);
textAlign(LEFT);
//String[] fontList = PFont.list();
//printArray(fontList);
}
void draw() {
background(255); //white
// text input
fill(0, 102, 153);
text(myText, 100, 20, 300, height);
// mangle text from string to int
String hs = myText;
int hi = unhex(hs); // need the alpha but could add it here
// text input presented as colour
fill(hi);
rect(100, 100, 60, 60);
}
void keyPressed() { // later, contrain so only usable characters poss,
if (keyCode == BACKSPACE) {
if (myText.length() > 0) {
myText = myText.substring(0, myText.length()-1);
}
} else if (keyCode == DELETE) {
myText = "";
} else if (keyCode != SHIFT) {
myText = myText + key;
}
}