JasonJohnson
7 years agoQrew Assistant Captain
How do I make a random color button?
We had a user request this thinking since there is no random command it could not be done. We tried a few methods but found a native method that works best and is really fun.
First create a rich text formula field that will generate the random color - [Randomish Button Color]
Note: We used Now() and built in fields to generate the Base 16 and at the bottom we have only listed 20 permutations of the over 700 available.
var text hh = Case(ToText(Mod(Hour(ToTimeOfDay(Now())), 16)),
"0", "0",
"1", "1",
"2", "2",
"3", "3",
"4", "4",
"5", "5",
"6", "6",
"7", "7",
"8", "8",
"9", "9",
"10", "a",
"11", "b",
"12", "c",
"13", "d",
"14", "e",
"15", "f",
"");
var text mm = Case(ToText(Mod(Minute(ToTimeOfDay(Now())), 16)),
"0", "0",
"1", "1",
"2", "2",
"3", "3",
"4", "4",
"5", "5",
"6", "6",
"7", "7",
"8", "8",
"9", "9",
"10", "a",
"11", "b",
"12", "c",
"13", "d",
"14", "e",
"15", "f",
"");
var text ss = Case(ToText(Mod(Second(ToTimeOfDay(Now())), 16)),
"0", "0",
"1", "1",
"2", "2",
"3", "3",
"4", "4",
"5", "5",
"6", "6",
"7", "7",
"8", "8",
"9", "9",
"10", "a",
"11", "b",
"12", "c",
"13", "d",
"14", "e",
"15", "f",
"");
var text ms = Case(ToText(Mod(MSecond(ToTimeOfDay(Now())), 16)),
"0", "0",
"1", "1",
"2", "2",
"3", "3",
"4", "4",
"5", "5",
"6", "6",
"7", "7",
"8", "8",
"9", "9",
"10", "a",
"11", "b",
"12", "c",
"13", "d",
"14", "e",
"15", "f",
"");
var text dd = Case(ToText(Mod(Day(ToDate([Date Modified])), 16)),
"0", "0",
"1", "1",
"2", "2",
"3", "3",
"4", "4",
"5", "5",
"6", "6",
"7", "7",
"8", "8",
"9", "9",
"10", "a",
"11", "b",
"12", "c",
"13", "d",
"14", "e",
"15", "f",
"");
var text zc = Case(ToText(Mod(Day(ToDate([Date Created])),16)),
"0", "0",
"1", "1",
"2", "2",
"3", "3",
"4", "4",
"5", "5",
"6", "6",
"7", "7",
"8", "8",
"9", "9",
"10", "a",
"11", "b",
"12", "c",
"13", "d",
"14", "e",
"15", "f",
"");
// Permutations
Case(Mod([Record ID#],20),
0, "#"&$mm&$dd&$hh&$ss&$ms&$zc,
1, "#"&$ms&$hh&$zc&$dd&$mm&$ss,
2, "#"&$ss&$mm&$ms&$dd&$hh&$zc,
3, "#"&$zc&$mm&$hh&$ss&$ms&$ms,
4, "#"&$zc&$mm&$ss&$hh&$dd&$ms,
5, "#"&$ss&$dd&$mm&$ms&$hh&$zc,
6, "#"&$hh&$zc&$mm&$ms&$dd&$ss,
7, "#"&$ss&$ms&$hh&$mm&$dd&$zc,
8, "#"&$hh&$ms&$ss&$mm&$zc&$dd,
9, "#"&$dd&$mm&$hh&$zc&$ss&$ms,
10, "#"&$dd&$ms&$mm&$ss&$zc&$hh,
11, "#"&$zc&$mm&$ss&$ms&$hh&$dd,
12, "#"&$mm&$hh&$ss&$dd&$ms&$zc,
13, "#"&$ms&$ss&$dd&$hh&$mm&$zc,
14, "#"&$mm&$hh&$ms&$dd&$ss&$zc,
15, "#"&$zc&$ss&$dd&$hh&$ms&$mm,
16, "#"&$zc&$hh&$ms&$dd&$mm&$ss,
17, "#"&$ss&$hh&$dd&$mm&$zc&$ms,
18, "#"&$mm&$hh&$dd&$ms&$zc&$ss,
19, "#"&$ss&$ms&$zc&$hh&$dd&$mm,
"")
Then create a formula checkbox field that will determine if the text should be black or white. [Threshold] -
var number threshold = 105;
var text R = Right(Left([Randomish Button Color],3),2);
var text G = Right(Left([Randomish Button Color],5),2);
var text B = Right([Randomish Button Color],2);
var text RL = Case(Left($R,1),
"f", "15",
"e", "14",
"d", "13",
"c", "12",
"b", "11",
"a", "10",
"9","9",
"8","8",
"7","7",
"6","6",
"5","5",
"4","4",
"3","3",
"2","2",
"1","1",
"0");
var text RR = Case(Right($R,1),
"f", "15",
"e", "14",
"d", "13",
"c", "12",
"b", "11",
"a", "10",
"9","9",
"8","8",
"7","7",
"6","6",
"5","5",
"4","4",
"3","3",
"2","2",
"1","1",
"0");
var text GL = Case(Left($G,1),
"f", "15",
"e", "14",
"d", "13",
"c", "12",
"b", "11",
"a", "10",
"9","9",
"8","8",
"7","7",
"6","6",
"5","5",
"4","4",
"3","3",
"2","2",
"1","1",
"0");
var text GR = Case(Right($G,1),
"f", "15",
"e", "14",
"d", "13",
"c", "12",
"b", "11",
"a", "10",
"9","9",
"8","8",
"7","7",
"6","6",
"5","5",
"4","4",
"3","3",
"2","2",
"1","1",
"0");
var text BL = Case(Left($B,1),
"f", "15",
"e", "14",
"d", "13",
"c", "12",
"b", "11",
"a", "10",
"9","9",
"8","8",
"7","7",
"6","6",
"5","5",
"4","4",
"3","3",
"2","2",
"1","1",
"0");
var text BR = Case(Right($B,1),
"f", "15",
"e", "14",
"d", "13",
"c", "12",
"b", "11",
"a", "10",
"9","9",
"8","8",
"7","7",
"6","6",
"5","5",
"4","4",
"3","3",
"2","2",
"1","1",
"0");
var number Rvalue = (ToNumber($RL) * 16 + ToNumber($RR))*0.299;
var number Gvalue = (ToNumber($GL) * 16 + ToNumber($GR))*0.587;
var number Bvalue = (ToNumber($BL) * 16 + ToNumber($BR))*0.144;
If(255-($Rvalue+$Gvalue+$Bvalue)<$Threshold, true, false)
Now you have the 2 pieces and need to create the rich text formula button -
// Begin button style
var text bgcolor = [Randomish Button Color];
var text txtcolor = If([Threshold]=false,"white","black");
var text style = "style=\"text-decoration: none; box-shadow: 3px 3px 1px #888888; background:" & $bgcolor & "; border-radius: 3px; padding: 5px 8px; color: " & $txtcolor & "; display: inline-block; font-weight: normal;font: bold 700 24px/1 \"Calibri\", sans-serif; text-align: center; text-shadow:none;";
// End button style
"<a " & $style & " href=\"javascript:" & "$.get('" & $URL & "', function(){" & "location.reload();" &
"});" & "void(0);\">Crazy Button Name</a>"
Note: $URL is the variable you can create to perform the button actions and the button is designed to return to any location that the button is used.
First create a rich text formula field that will generate the random color - [Randomish Button Color]
Note: We used Now() and built in fields to generate the Base 16 and at the bottom we have only listed 20 permutations of the over 700 available.
var text hh = Case(ToText(Mod(Hour(ToTimeOfDay(Now())), 16)),
"0", "0",
"1", "1",
"2", "2",
"3", "3",
"4", "4",
"5", "5",
"6", "6",
"7", "7",
"8", "8",
"9", "9",
"10", "a",
"11", "b",
"12", "c",
"13", "d",
"14", "e",
"15", "f",
"");
var text mm = Case(ToText(Mod(Minute(ToTimeOfDay(Now())), 16)),
"0", "0",
"1", "1",
"2", "2",
"3", "3",
"4", "4",
"5", "5",
"6", "6",
"7", "7",
"8", "8",
"9", "9",
"10", "a",
"11", "b",
"12", "c",
"13", "d",
"14", "e",
"15", "f",
"");
var text ss = Case(ToText(Mod(Second(ToTimeOfDay(Now())), 16)),
"0", "0",
"1", "1",
"2", "2",
"3", "3",
"4", "4",
"5", "5",
"6", "6",
"7", "7",
"8", "8",
"9", "9",
"10", "a",
"11", "b",
"12", "c",
"13", "d",
"14", "e",
"15", "f",
"");
var text ms = Case(ToText(Mod(MSecond(ToTimeOfDay(Now())), 16)),
"0", "0",
"1", "1",
"2", "2",
"3", "3",
"4", "4",
"5", "5",
"6", "6",
"7", "7",
"8", "8",
"9", "9",
"10", "a",
"11", "b",
"12", "c",
"13", "d",
"14", "e",
"15", "f",
"");
var text dd = Case(ToText(Mod(Day(ToDate([Date Modified])), 16)),
"0", "0",
"1", "1",
"2", "2",
"3", "3",
"4", "4",
"5", "5",
"6", "6",
"7", "7",
"8", "8",
"9", "9",
"10", "a",
"11", "b",
"12", "c",
"13", "d",
"14", "e",
"15", "f",
"");
var text zc = Case(ToText(Mod(Day(ToDate([Date Created])),16)),
"0", "0",
"1", "1",
"2", "2",
"3", "3",
"4", "4",
"5", "5",
"6", "6",
"7", "7",
"8", "8",
"9", "9",
"10", "a",
"11", "b",
"12", "c",
"13", "d",
"14", "e",
"15", "f",
"");
// Permutations
Case(Mod([Record ID#],20),
0, "#"&$mm&$dd&$hh&$ss&$ms&$zc,
1, "#"&$ms&$hh&$zc&$dd&$mm&$ss,
2, "#"&$ss&$mm&$ms&$dd&$hh&$zc,
3, "#"&$zc&$mm&$hh&$ss&$ms&$ms,
4, "#"&$zc&$mm&$ss&$hh&$dd&$ms,
5, "#"&$ss&$dd&$mm&$ms&$hh&$zc,
6, "#"&$hh&$zc&$mm&$ms&$dd&$ss,
7, "#"&$ss&$ms&$hh&$mm&$dd&$zc,
8, "#"&$hh&$ms&$ss&$mm&$zc&$dd,
9, "#"&$dd&$mm&$hh&$zc&$ss&$ms,
10, "#"&$dd&$ms&$mm&$ss&$zc&$hh,
11, "#"&$zc&$mm&$ss&$ms&$hh&$dd,
12, "#"&$mm&$hh&$ss&$dd&$ms&$zc,
13, "#"&$ms&$ss&$dd&$hh&$mm&$zc,
14, "#"&$mm&$hh&$ms&$dd&$ss&$zc,
15, "#"&$zc&$ss&$dd&$hh&$ms&$mm,
16, "#"&$zc&$hh&$ms&$dd&$mm&$ss,
17, "#"&$ss&$hh&$dd&$mm&$zc&$ms,
18, "#"&$mm&$hh&$dd&$ms&$zc&$ss,
19, "#"&$ss&$ms&$zc&$hh&$dd&$mm,
"")
Then create a formula checkbox field that will determine if the text should be black or white. [Threshold] -
var number threshold = 105;
var text R = Right(Left([Randomish Button Color],3),2);
var text G = Right(Left([Randomish Button Color],5),2);
var text B = Right([Randomish Button Color],2);
var text RL = Case(Left($R,1),
"f", "15",
"e", "14",
"d", "13",
"c", "12",
"b", "11",
"a", "10",
"9","9",
"8","8",
"7","7",
"6","6",
"5","5",
"4","4",
"3","3",
"2","2",
"1","1",
"0");
var text RR = Case(Right($R,1),
"f", "15",
"e", "14",
"d", "13",
"c", "12",
"b", "11",
"a", "10",
"9","9",
"8","8",
"7","7",
"6","6",
"5","5",
"4","4",
"3","3",
"2","2",
"1","1",
"0");
var text GL = Case(Left($G,1),
"f", "15",
"e", "14",
"d", "13",
"c", "12",
"b", "11",
"a", "10",
"9","9",
"8","8",
"7","7",
"6","6",
"5","5",
"4","4",
"3","3",
"2","2",
"1","1",
"0");
var text GR = Case(Right($G,1),
"f", "15",
"e", "14",
"d", "13",
"c", "12",
"b", "11",
"a", "10",
"9","9",
"8","8",
"7","7",
"6","6",
"5","5",
"4","4",
"3","3",
"2","2",
"1","1",
"0");
var text BL = Case(Left($B,1),
"f", "15",
"e", "14",
"d", "13",
"c", "12",
"b", "11",
"a", "10",
"9","9",
"8","8",
"7","7",
"6","6",
"5","5",
"4","4",
"3","3",
"2","2",
"1","1",
"0");
var text BR = Case(Right($B,1),
"f", "15",
"e", "14",
"d", "13",
"c", "12",
"b", "11",
"a", "10",
"9","9",
"8","8",
"7","7",
"6","6",
"5","5",
"4","4",
"3","3",
"2","2",
"1","1",
"0");
var number Rvalue = (ToNumber($RL) * 16 + ToNumber($RR))*0.299;
var number Gvalue = (ToNumber($GL) * 16 + ToNumber($GR))*0.587;
var number Bvalue = (ToNumber($BL) * 16 + ToNumber($BR))*0.144;
If(255-($Rvalue+$Gvalue+$Bvalue)<$Threshold, true, false)
Now you have the 2 pieces and need to create the rich text formula button -
// Begin button style
var text bgcolor = [Randomish Button Color];
var text txtcolor = If([Threshold]=false,"white","black");
var text style = "style=\"text-decoration: none; box-shadow: 3px 3px 1px #888888; background:" & $bgcolor & "; border-radius: 3px; padding: 5px 8px; color: " & $txtcolor & "; display: inline-block; font-weight: normal;font: bold 700 24px/1 \"Calibri\", sans-serif; text-align: center; text-shadow:none;";
// End button style
"<a " & $style & " href=\"javascript:" & "$.get('" & $URL & "', function(){" & "location.reload();" &
"});" & "void(0);\">Crazy Button Name</a>"
Note: $URL is the variable you can create to perform the button actions and the button is designed to return to any location that the button is used.