The Wiki for Tale 6 is in read-only mode and is available for archival and reference purposes only. Please visit the current Tale 11 Wiki in the meantime.

If you have any issues with this Wiki, please post in #wiki-editing on Discord or contact Brad in-game.

Difference between revisions of "User:Selune"

From ATITD6
Jump to navigationJump to search
Line 833: Line 833:
 
 
 
gui_refresh();
 
gui_refresh();
 +
end
 +
end
 +
</pre>
 +
{{Collapse bottom}}
 +
 +
{{Collapse top|title=New Raeli Oven Monitor (IN TESTING)}}
 +
<pre>
 +
 +
loadfile("luaScripts/screen_reader_common.inc")();
 +
loadfile("luaScripts/common.inc")();
 +
loadfile("luaScripts/ui_utils.inc")();
 +
 +
askText = singleLine([[
 +
  Raeli Color Monitor v1.1a (edit by Selune) --
 +
  Make sure the Raeli Oven is pinned, then select the ATITD window and press shift.
 +
]]);
 +
 +
stop_at_CB = false;
 +
take_screenshots = true;
 +
stop_at_color = "White";
 +
saved_x = 0;
 +
saved_y = 0;
 +
go_now = false;
 +
 +
color_names = {"AliceBlue", "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige", "Bisque", "Black", "BlanchedAlmond", "Blue", "BlueViolet", "Brown", "Burlywood", "CadetBlue", "Carrot", "Chartreuse", "Chocolate", "Coral", "CornflowerBlue", "Cornsilk", "Crimson", "DarkBlue", "DarkCyan", "DarkGoldenrod", "DarkGray", "DarkGreen", "DarkKhaki", "DarkMagenta", "DarkOliveGreen", "DarkOrange", "DarkOrchid", "DarkRed", "DarkSalmon", "DarkSeaGreen", "DarkSlateBlue", "DarkSlateGray", "DarkTurquoise", "DarkViolet", "DeepPink", "DeepSkyBlue", "DimGray", "DodgerBlue", "Feldspar", "FireBrick", "FloralWhite", "ForestGreen", "Fuchsia", "Gainsboro", "GhostWhite", "Gold", "Goldenrod", "Gray", "Green", "GreenYellow", "Honeydew", "HotPink", "IndianRed", "Indigo", "Ivory", "Khaki", "Lavender", "LavenderBlush", "LawnGreen", "LemonChiffon", "LightBlue", "LightCoral", "LightCyan", "LightGoldenrodYellow", "LightGreen", "LightGrey", "LightPink", "LightSalmon", "LightSeaGreen", "LightSkyBlue", "LightSlateBlue", "LightSlateGray", "LightSteelBlue", "LightYellow", "Lime", "LimeGreen", "Linen", "Maroon ", "MediumAquamarine", "MediumBlue ", "MediumOrchid", "MediumPurple", "MediumSeaGreen", "MediumSlateBlue", "MediumSpringGreen", "MediumTurquoise", "MediumVioletRed", "MidnightBlue", "MintCream", "MistyRose", "Moccasin", "NavajoWhite", "Navy ", "OldLace", "Olive", "OliveDrab", "Orange", "OrangeRed", "Orchid", "PaleGoldenrod", "PaleGreen", "PaleTurquoise", "PaleVioletRed", "PapayaWhip", "PeachPuff", "Peru", "Pink", "Plum", "PowderBlue", "Purple ", "Red", "RosyBrown", "RoyalBlue", "SaddleBrown", "Salmon", "SandyBrown", "SeaGreen", "Seashell", "Sienna", "Silver", "SkyBlue", "SlateBlue", "SlateGray", "Snow", "SpringGreen", "SteelBlue ", "Tan", "Teal", "Thistle", "Tomato", "Turquoise", "Violet", "VioletRed", "Wheat", "White", "WhiteSmoke", "Yellow", "YellowGreen"};
 +
color_values = {0xF0F8FF, 0xFAEBD7, 0x00FFFF, 0x7FFFD4, 0xF0FFFF, 0xF5F5DC, 0xFFE4C4, 0x000000, 0xFFEBCD, 0x0000FF, 0x8A2BE2, 0xA52A2A, 0xDEB887, 0x5F9EA0, 0xE07020, 0x7FFF00, 0xD2691E, 0xFF7F50, 0x6495ED, 0xFFF8DC, 0xDC143C, 0x00008B, 0x008B8B, 0xB8860B, 0xA9A9A9, 0x006400, 0xBDB76B, 0x8B008B, 0x556B2F, 0xFF8C00, 0x9932CC, 0x8B0000, 0xE9967A, 0x8FBC8F, 0x483D8B, 0x2F4F4F, 0x00CED1, 0x9400D3, 0xFF1493, 0x00BFFF, 0x696969, 0x1E90FF, 0xD19275, 0xB22222, 0xFFFAF0, 0x228B22, 0xFF00FF, 0xDCDCDC, 0xF8F8FF, 0xFFD700, 0xDAA520, 0x808080, 0x008000, 0xADFF2F, 0xF0FFF0, 0xFF69B4, 0xCD5C5C, 0x4B0082, 0xFFFFF0, 0xF0E68C, 0xE6E6FA, 0xFFF0F5, 0x7CFC00, 0xFFFACD, 0xADD8E6, 0xF08080, 0xE0FFFF, 0xFAFAD2, 0x90EE90, 0xD3D3D3, 0xFFB6C1, 0xFFA07A, 0x20B2AA, 0x87CEFA, 0x8470FF, 0x778899, 0xB0C4DE, 0xFFFFE0, 0x00FF00, 0x32CD32, 0xFAF0E6, 0x800000, 0x66CDAA, 0x0000CD, 0xBA55D3, 0x9370DB, 0x3CB371, 0x7B68EE, 0x00FA9A, 0x48D1CC, 0xC71585, 0x191970, 0xF5FFFA, 0xFFE4E1, 0xFFE4B5, 0xFFDEAD, 0x000080, 0xFDF5E6, 0x808000, 0x6B8E23, 0xFFA500, 0xFF4500, 0xDA70D6, 0xEEE8AA, 0x98FB98, 0xAFEEEE, 0xDB7093, 0xFFEFD5, 0xFFDAB9, 0xCD853F, 0xFFC0CB, 0xDDA0DD, 0xB0E0E6, 0x800080, 0xFF0000, 0xBC8F8F, 0x4169E1, 0x8B4513, 0xFA8072, 0xF4A460, 0x2E8B57, 0xFFF5EE, 0xA0522D, 0xC0C0C0, 0x87CEEB, 0x6A5ACD, 0x708090, 0xFFFAFA, 0x00FF7F, 0x4682B4, 0xD2B48C, 0x008080, 0xD8BFD8, 0xFF6347, 0x40E0D0, 0xEE82EE, 0xD02090, 0xF5DEB3, 0xFFFFFF, 0xF5F5F5, 0xFFFF00, 0x9ACD32};
 +
 +
-- Decimal to Hex conversion
 +
function DEC_HEX(IN)
 +
    local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
 +
    while IN>0 do
 +
        I=I+1
 +
        IN,D=math.floor(IN/B),math.mod(IN,B)+1
 +
        OUT=string.sub(K,D,D)..OUT
 +
    end
 +
    return OUT
 +
end
 +
 +
--Color Conversions
 +
function rgb2hsl(src_r, src_g, src_b)
 +
local h, s, l, d;
 +
src_r = src_r / 255;
 +
src_g = src_g / 255;
 +
src_b = src_b / 255;
 +
    local max_rgb = math.max(src_r, src_g, src_b);
 +
local min_rgb = math.min(src_r, src_g, src_b);
 +
    l = (max_rgb + min_rgb) / 2;
 +
 +
    if(max_rgb == min_rgb) then
 +
        h = 0; --achromatic
 +
s = 0;
 +
    else
 +
        d = max_rgb - min_rgb;
 +
if l > 0.5 then
 +
s = d / (2 - max_rgb - min_rgb);
 +
else
 +
s = d / (max_rgb + min_rgb);
 +
end
 +
 +
if src_g > src_b then
 +
if src_r > src_g then
 +
h = (src_g - src_b) / d;
 +
else
 +
h = (src_b - src_r) / d + 2;
 +
end
 +
else
 +
if src_r > src_g then
 +
h = (src_g - src_b) / d + 6;
 +
else
 +
h = (src_r - src_g) / d + 4;
 +
end
 +
end
 +
        h = h / 6;
 +
    end
 +
 +
if h == nil then
 +
error "NO H";
 +
end
 +
 +
if s == nil then
 +
error ("NO S  D:" .. d);
 +
end
 +
 +
if l == nil then
 +
error "NO L";
 +
end
 +
 +
    return h, s, l;
 +
end
 +
 +
function rgb2lab(src_r, src_g, src_b)
 +
--R from 0 to 255
 +
--G from 0 to 255
 +
--B from 0 to 255
 +
local r = src_r/255;
 +
local g = src_g/255;
 +
local b = src_b/255;
 +
 +
if (r > 0.04045) then
 +
r = math.pow((r + 0.055) / 1.055, 2.4);
 +
else
 +
r = r / 12.92;
 +
end
 +
if ( g > 0.04045) then
 +
g = math.pow((g + 0.055) / 1.055, 2.4);
 +
else
 +
g = g / 12.92;
 +
end
 +
if (b > 0.04045) then
 +
b = math.pow((b + 0.055) / 1.055, 2.4);
 +
else
 +
b = b / 12.92;
 +
end
 +
 +
r = r * 100;
 +
g = g * 100;
 +
b = b * 100;
 +
 +
local x = r * 0.4124 + g * 0.3576 + b * 0.1805;
 +
local y = r * 0.2126 + g * 0.7152 + b * 0.0722;
 +
local z = r * 0.0193 + g * 0.1192 + b * 0.9505;
 +
 +
x = x / 95.047;
 +
y = y / 100.000; 
 +
z = z / 108.883;
 +
 +
if ( x > 0.008856 ) then
 +
x = math.pow( x , 1/3 );
 +
else
 +
x = ( 7.787 * x ) + ( 16/116 );
 +
end
 +
if ( y > 0.008856 ) then
 +
y = math.pow( y , 1/3 );
 +
else
 +
y = ( 7.787 * y ) + ( 16/116 );
 +
end
 +
if ( z > 0.008856 ) then
 +
z = math.pow( z , 1/3 );
 +
else
 +
z = ( 7.787 * z ) + ( 16/116 );
 +
end
 +
 +
local lab_l = ( 116 * y ) - 16;
 +
local lab_a = 500 * ( x - y );
 +
local lab_b = 200 * ( y - z );
 +
 +
return lab_l, lab_a, lab_b;
 +
end
 +
 +
--Get nearest color name
 +
function nearestColor(px)
 +
local rgb_r, rgb_g, rgb_b, tst_r, tst_g, tst_b, test_px;
 +
local lab_l, lab_a, lab_b, lab_tst_l, lab_tst_a, lab_tst_b;
 +
local hsl_h, hsl_s, hsl_l, hsl_tst_h, hsl_tst_s, hsl_tst_l;
 +
local offset, best_name, best_offset;
 +
best_offset = 100000;
 +
rgb_r = (math.floor(px/256/256/256) % 256);
 +
rgb_g = (math.floor(px/256/256) % 256);
 +
rgb_b = (math.floor(px/256) % 256);
 +
lab_l, lab_a, lab_b = rgb2lab(rgb_r, rgb_g, rgb_b);
 +
hsl_h, hsl_s, hsl_l = rgb2hsl(rgb_r, rgb_g, rgb_b);
 +
 +
for i=1, #color_names do
 +
checkBreak();
 +
test_px = color_values[i];
 +
tst_r = (math.floor(test_px/256/256) % 256);
 +
tst_g = (math.floor(test_px/256) % 256);
 +
tst_b = (math.floor(test_px) % 256);
 +
lab_tst_l, lab_tst_a, lab_tst_b = rgb2lab(tst_r, tst_g, tst_b);
 +
hsl_tst_h, hsl_tst_s, hsl_tst_l = rgb2hsl(tst_r, tst_g, tst_b);
 +
 +
---- Different methods to determine "distance" from the ideal colors. Change these to test different options.
 +
 +
--offset = math.abs(rgb_r - tst_r) + math.abs(rgb_g - tst_g) + math.abs(rgb_b - tst_b);
 +
--offset = math.max(math.max((math.abs(rgb_r - tst_r)), math.abs(rgb_g - tst_g)), math.abs(rgb_b - tst_b));
 +
--offset = (rgb_r - tst_r)^2 + (rgb_g - tst_g)^2 + (rgb_b - tst_b)^2;
 +
--offset = (lab_l - lab_tst_l)^2 + (lab_a - lab_tst_a)^2 + (lab_b - lab_tst_b)^2;
 +
offset = math.max(math.max((math.abs(lab_l - lab_tst_l)), math.abs(lab_a - lab_tst_a)), math.abs(lab_b - lab_tst_b));
 +
--offset = (hsl_h - hsl_tst_h)^2 + (hsl_s - hsl_tst_s)^2 + (hsl_l - hsl_tst_l)^2;
 +
--offset = math.max(math.max((math.abs(hsl_h - hsl_tst_h)), math.abs(hsl_s - hsl_tst_s)), math.abs(hsl_l - hsl_tst_l));
 +
 +
if (offset < best_offset) then
 +
best_name = color_names[i] .. "  Hex: " .. DEC_HEX(color_values[i]);
 +
best_offset = offset;
 +
end
 +
 +
end
 +
 +
return best_name;
 +
end
 +
 +
--Check pixel against target
 +
function checkColor(px)
 +
local color_name = nearestColor(px);
 +
if (color_name:upper() == stop_at_color:upper()) then
 +
return true;
 +
end
 +
 +
return false;
 +
end
 +
 +
-- optional message
 +
function askForPixel()
 +
local is_done = false;
 +
local have_coords = false;
 +
local color_found = false;
 +
local color_guess = "NONE";
 +
local tst_r, tst_g, tst_b, rgb_r, rgb_g, rgb_b;
 +
-- Display message until shift is held
 +
while not go_now do
 +
checkBreak();
 +
y = 0;
 +
mouse_x, mouse_y = srMousePos();
 +
if not have_coords then
 +
lsPrintWrapped(0, y, 1, lsScreenX, 0.7, 0.7, 0xFFFFFFff,
 +
"Mouse over one of the Raeli Oven color pixels and press Shift.");
 +
if lsShiftHeld() then
 +
saved_x = mouse_x;
 +
saved_y = mouse_y;
 +
have_coords = true;
 +
end
 +
else
 +
lsPrintWrapped(0, y, 1, lsScreenX, 0.7, 0.7, 0xFFFFFFff,
 +
"Coords chosen, press 'Reset' to change or 'Do It' to start.");
 +
end
 +
 +
y = y + 40;
 +
if have_coords then
 +
px = srReadPixel(saved_x, saved_y);
 +
lsPrintWrapped(0, y, 1, lsScreenX, 0.7, 0.7, 0xffffff,
 +
saved_x .. ", " .. saved_y);
 +
else
 +
px = srReadPixel(mouse_x, mouse_y);
 +
lsPrintWrapped(0, y, 1, lsScreenX, 0.7, 0.7, 0xB0B0B0ff,
 +
mouse_x .. ", " .. mouse_y);
 +
end
 +
 +
y = y + 15;
 +
color_guess = nearestColor(px);
 +
lsPrintWrapped(0, y, 1, lsScreenX, 0.7, 0.7, px,
 +
"(" .. (math.floor(px/256/256/256) % 256) .. "," .. (math.floor(px/256/256) % 256) .. "," .. (math.floor(px/256) % 256) .. "," .. (px % 256) .. ") Hex: " .. DEC_HEX(px));
 +
 +
y = y + 15;
 +
lsPrintWrapped(0, y, 1, lsScreenX, 0.7, 0.7, px, "Best guess: " .. color_guess);
 +
y = y + 30;
 +
lsPrintWrapped(0, y, 1, lsScreenX, 0.7, 0.7, px, "Dec: " .. math.floor((px/256) % 16777216) .. "  Hex: " .. DEC_HEX(math.floor((px/256) % 16777216)));
 +
 +
-- Testing other methods of grabbing the pixel, making sure RGBA values match
 +
-- srReadScreen();
 +
-- px2 = srReadPixelFromBuffer(mouse_x, mouse_y);
 +
-- lsPrintWrapped(0, 80, 1, lsScreenX, 0.7, 0.7, 0xB0B0B0ff,
 +
-- mouse_x .. ", " .. mouse_y .. " = " .. (math.floor(px2/256/256/256) % 256) .. "," .. (math.floor(px2/256/256) % 256) .. "," .. (math.floor(px2/256) % 256) .. "," .. (px2 % 256) );
 +
-- lsButtonText(lsScreenX - 110, lsScreenY - 90, 0, 100, px, "test1");
 +
-- lsButtonText(lsScreenX - 110, lsScreenY - 60, 0, 100, px2, "test2");
 +
 +
y = y + 20;
 +
take_screenshots = lsCheckBox(15, y, z, 0xffffffff, " Take Screenshots", take_screenshots);
 +
 +
y = y + 20;
 +
stop_at_CB = lsCheckBox(15, y, z, 0xffffffff, " Stop at Color", stop_at_CB);
 +
 +
if stop_at_CB then
 +
y = y + 25;
 +
is_done = false;
 +
color_found = false;
 +
-- Edit box and text display
 +
lsPrint(10, y, z, 0.7, 0.7, 0xFFFFFFff, "Desired Color:");
 +
 +
y = y + 20;
 +
-- lsEditBox returns two different things (a state and a value)
 +
is_done, stop_at_color = lsEditBox("Desired Color:", 10, y, z, 250, 30, 0.7, 0.7, 0x000000ff, "White");
 +
--dropdown_color_cur_value = lsDropdown("ColorDropDown1", 5, y, 0, 200, dropdown_color_cur_value, color_names);
 +
 +
for i=0, #color_names do
 +
if stop_at_color and color_names[i] then
 +
if (stop_at_color:upper() == (color_names[i]):upper()) then
 +
color_found = true;
 +
end
 +
end
 +
end
 +
 +
y = y + 30;
 +
if not color_found then
 +
lsPrint(10, y, z, 0.7, 0.7, 0xFF0000ff, "No such color");
 +
end
 +
 +
srReadScreen();
 +
 +
y = y + 30;
 +
 +
else
 +
is_done = true;
 +
color_found = true;
 +
end
 +
 +
if have_coords then
 +
if color_found then
 +
if lsButtonText(10, lsScreenY - 30, 0, 80, 0xFFFFFFff, "Do It") then
 +
go_now = true;
 +
end
 +
end
 +
 +
if lsButtonText(100, lsScreenY - 30, 0, 80, 0xFFA500ff, "Reset") then
 +
have_coords = false;
 +
end
 +
end
 +
 +
if lsButtonText(lsScreenX - 110, lsScreenY - 30, 0, 100, 0xFFFFFFff, "Exit") then
 +
error "Canceled";
 +
end
 +
lsDoFrame();
 +
end
 +
-- Wait for shift to be released
 +
while lsShiftHeld() do end;
 +
xyWindowSize = srGetWindowSize();
 +
end
 +
 +
 +
function doit()
 +
askForWindow(askText);
 +
while lsShiftHeld() do
 +
-- Make user release shift key
 +
lsCheckBreak();
 +
end
 +
 +
askForPixel();
 +
 +
local t0 = lsGetTimer();
 +
local px = 0;
 +
local index=0;
 +
while 1 do
 +
checkBreak();
 +
lsSleep(100);
 +
srReadScreen();
 +
new_px = srReadPixel(mouse_x, mouse_y);
 +
local t = (lsGetTimer() - t0) / 1000 / 60;
 +
t = math.floor(t*10 + 0.5)/10;
 +
local t_string = t;
 +
if not (new_px == px) then
 +
index = index+1;
 +
if take_screenshots then
 +
srSaveLastReadScreen("screen_" .. index .. "_" .. t_string .. ".png");
 +
end
 +
px = new_px;
 +
if stop_at_CB then
 +
if checkColor(px) then
 +
clickAllText("Interrupt");
 +
lsPlaySound("Complete.wav");
 +
break;
 +
end
 +
end
 +
 +
lsPlaySound("Clank.wav");
 +
end
 +
 +
lsPrintWrapped(0, 0, 1, lsScreenX, 1, 1, 0xFFFFFFff,
 +
" Color: " .. DEC_HEX(new_px) .. "  screen: " .. index .. " timer: " .. t_string);
 +
lsPrintWrapped(0, 60, 1, lsScreenX, 0.7, 0.7, 0xFFFFFFff,
 +
"(" .. (math.floor(px/256/256/256) % 256) .. "," .. (math.floor(px/256/256) % 256) .. "," .. (math.floor(px/256) % 256) .. "," .. (px % 256) .. ")" );
 +
 +
if lsButtonText(lsScreenX - 110, lsScreenY - 30, 0, 100, 0xFFFFFFff, "Exit") then
 +
error "Canceled";
 +
end
 +
lsDoFrame();
 
end
 
end
 
end
 
end
 
</pre>
 
</pre>
 
{{Collapse bottom}}
 
{{Collapse bottom}}

Revision as of 05:54, 15 January 2013

Selune's Macros have been updated to the TaleScripts repository. Please leave any ideas for improvements or new macros here.