From 90c2fef497ede950f984814c03cd8a72423482c2 Mon Sep 17 00:00:00 2001 From: Igor Timofeev Date: Tue, 8 May 2018 13:41:51 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=BB=D0=B8=D0=B7=D1=8B=D0=B2?= =?UTF-8?q?=D0=B0=D0=B5=D0=BC=20=D0=BF=D0=B8=D0=B7=D0=B4=D1=83=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BB=D0=BE=D1=80=D0=BB=D0=B8=D0=B1=D1=8B=20=D0=B8=20?= =?UTF-8?q?=D1=84=D0=B8=D0=BA=D1=81=D0=B8=D0=BC=20=D1=8D=D1=82=D1=83=20?= =?UTF-8?q?=D1=85=D1=83=D0=B9=D0=BD=D1=8E=20=D1=81=20GUI.list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Applications/PictureEdit/Main.lua | 6 +- lib/GUI.lua | 423 +++++++++++++++--------------- lib/MineOSInterface.lua | 10 +- lib/color.lua | 144 ++++++---- 4 files changed, 321 insertions(+), 262 deletions(-) diff --git a/Applications/PictureEdit/Main.lua b/Applications/PictureEdit/Main.lua index 4a87ed0a..2d321680 100644 --- a/Applications/PictureEdit/Main.lua +++ b/Applications/PictureEdit/Main.lua @@ -122,7 +122,7 @@ mainContainer.image = mainContainer:addChild(GUI.object(1, 1, 1, 1)) mainContainer.image.data = {} local function onToolTouch(index) - tool = mainContainer.toolsList.children[index].tool + tool = mainContainer.toolsList.itemsLayout.children[index].tool mainContainer.toolsList:select(index) mainContainer.currentToolOverlay:deleteChildren() @@ -257,8 +257,8 @@ mainContainer.image.eventHandler = function(mainContainer, object, eventData) elseif eventData[4] == 45 then swapColors() else - for i = 1, #mainContainer.toolsList.children do - if eventData[4] == mainContainer.toolsList.children[i].tool.keyCode then + for i = 1, #mainContainer.toolsList.itemsLayout.children do + if eventData[4] == mainContainer.toolsList.itemsLayout.children[i].tool.keyCode then onToolTouch(i) return end diff --git a/lib/GUI.lua b/lib/GUI.lua index f898786c..d14b95d5 100755 --- a/lib/GUI.lua +++ b/lib/GUI.lua @@ -3935,6 +3935,207 @@ end ----------------------------------------------------------------------- +function GUI.addPaletteWindowToContainer(parentContainer, color) + local palette = parentContainer:addChild(GUI.windowFromContainer(GUI.palette(1, 1, color or 0x9900FF))) + palette.localX, palette.localY = math.floor(parentContainer.width / 2 - palette.width / 2), math.floor(parentContainer.height / 2 - palette.height / 2) + + return palette +end + +----------------------------------------------------------------------- + +local function listUpdate(object) + object.backgroundPanel.width, object.backgroundPanel.height = object.width, object.height + object.backgroundPanel.colors.background = object.colors.default.background + object.itemsLayout.width, object.itemsLayout.height = object.width, object.height + + local step, child = false + for i = 1, #object.itemsLayout.children do + child = object.itemsLayout.children[i] + + -- Жмяканье пизды + child.pressed = i == object.selectedItem + + -- Цвет залупы + if step then + child.colors.default = object.colors.alternating + else + child.colors.default = object.colors.default + end + child.colors.pressed, step = object.colors.pressed, not step + + -- Размеры хуйни + if object.itemsLayout.cells[1][1].direction == GUI.directions.horizontal then + if object.offsetMode then + child.width, child.height = object.itemSize * 2 + unicode.len(child.text), object.height + else + child.width, child.height = object.itemSize, object.height + end + else + if object.offsetMode then + child.width, child.height = object.width, object.itemSize * 2 + 1 + else + child.width, child.height = object.width, object.itemSize + end + end + end + + return list +end + +local function listSelect(object, index) + object.selectedItem = index + object:update() + + return object +end + +local function listDeselect(object) + object.selectedItem = nil + object:update() + + return object +end + +local function listItemEventHandler(mainContainer, object, eventData) + if eventData[1] == "touch" or eventData[1] == "drag" then + object.parent.parent:select(object:indexOf()) + mainContainer:drawOnScreen() + + if object.onTouch then + object.onTouch(mainContainer, object, eventDat) + end + end +end + +local function listAddItem(object, text) + local item = object.itemsLayout:addChild(GUI.button(1, 1, 1, 1, 0, 0, 0, 0, text)) + + item.switchMode = true + item.animated = false + item.eventHandler = listItemEventHandler + + object:update() + + return item +end + +local function listSetAlignment(object, ...) + object.itemsLayout:setCellAlignment(1, 1, ...) + return object +end + +local function listSetSpacing(object, ...) + object.itemsLayout:setCellSpacing(1, 1, ...) + return object +end + +local function listSetDirection(object, ...) + object.itemsLayout:setCellDirection(1, 1, ...) + object:update() + + return object +end + +local function listGetItem(object, index) + return object.itemsLayout.children[index] +end + +function GUI.list(x, y, width, height, itemSize, spacing, backgroundColor, textColor, backgroundAlternatingColor, textAlternatingColor, backgroundSelectedColor, textSelectedColor, offsetMode) + local object = GUI.container(x, y, width, height) + + object.colors = { + default = { + background = backgroundColor, + text = textColor + }, + alternating = { + background = backgroundAlternatingColor, + text = textAlternatingColor + }, + pressed = { + background = backgroundSelectedColor, + text = textSelectedColor + }, + } + + object.selectedItem = 1 + object.select = listSelect + object.deselect = listDeselect + object.offsetMode = offsetMode + object.itemSize = itemSize + + object.backgroundPanel = object:addChild(GUI.panel(1, 1, width, height, backgroundColor)) + object.itemsLayout = object:addChild(GUI.layout(1, 1, width, height, 1, 1)) + + object.update = listUpdate + object.addItem = listAddItem + object.getItem = listGetItem + object.setAlignment = listSetAlignment + object.setSpacing = listSetSpacing + object.setDirection = listSetDirection + + object:setAlignment(GUI.alignment.horizontal.left, GUI.alignment.vertical.top) + object:setSpacing(spacing) + object:setDirection(GUI.directions.vertical) + + return object +end + +------------------------------------------------------------------------------------------ + +function GUI.highlightString(x, y, fromChar, limit, indentationWidth, colorScheme, patterns, s) + fromChar = fromChar or 1 + + local counter, symbols, colors, stringLength, bufferIndex, newFrameBackgrounds, newFrameForegrounds, newFrameSymbols, searchFrom, starting, ending = indentationWidth, {}, {}, unicode.len(s), buffer.getIndex(x, y), buffer.getNewFrameTables() + local toChar = math.min(stringLength, fromChar + limit - 1) + + for i = 1, stringLength do + symbols[i] = unicode.sub(s, i, i) + end + + for j = 1, #patterns do + searchFrom = 1 + + while true do + starting, ending = string.unicodeFind(s, patterns[j][1], searchFrom) + + if starting then + for i = starting + patterns[j][3], ending - patterns[j][4] do + colors[i] = colorScheme[patterns[j][2]] + end + else + break + end + + searchFrom = ending + 1 - patterns[j][4] + end + end + + -- Ебошим индентейшны + for i = fromChar, toChar do + if symbols[i] == " " then + colors[i] = colorScheme.indentation + + if counter == indentationWidth then + symbols[i], counter = "│", 0 + end + + counter = counter + 1 + else + break + end + end + + -- А тута уже сам текст + for i = fromChar, toChar do + newFrameForegrounds[bufferIndex], newFrameSymbols[bufferIndex] = colors[i] or colorScheme.text, symbols[i] or " " + bufferIndex = bufferIndex + 1 + end +end + +----------------------------------------------------------------------- + function windowDraw(window) GUI.drawContainerContent(window) GUI.windowShadow(window.x, window.y, window.width, window.height, GUI.colors.windows.shadowTransparency, true) @@ -4003,218 +4204,28 @@ function GUI.window(x, y, width, height) return window end ------------------------------------------------------------------------ - -function GUI.addPaletteWindowToContainer(parentContainer, color) - local palette = parentContainer:addChild(GUI.windowFromContainer(GUI.palette(1, 1, color or 0x9900FF))) - palette.localX, palette.localY = math.floor(parentContainer.width / 2 - palette.width / 2), math.floor(parentContainer.height / 2 - palette.height / 2) - - return palette -end - ------------------------------------------------------------------------ - -local function listUpdate(object) - local step = false - for i = 1, #object.children do - -- Жмяканье пизды - object.children[i].pressed = i == object.selectedItem - -- Цвет залупы - if step then - object.children[i].colors.default = object.colors.alternating - else - object.children[i].colors.default = object.colors.default - end - object.children[i].colors.pressed, step = object.colors.pressed, not step - -- Размеры хуйни - if object.cells[1][1].direction == GUI.directions.horizontal then - if object.offsetMode then - object.children[i].width, object.children[i].height = object.itemSize * 2 + unicode.len(object.children[i].text), object.height - else - object.children[i].width, object.children[i].height = object.itemSize, object.height - end - else - if object.offsetMode then - object.children[i].width, object.children[i].height = object.width, object.itemSize * 2 + 1 - else - object.children[i].width, object.children[i].height = object.width, object.itemSize - end - end - end - - return list -end - -local function listDraw(object) - buffer.square(object.x, object.y, object.width, object.height, object.colors.default.background, object.colors.default.text, " ") - layoutDraw(object) - - return object -end - -local function listSelect(object, index) - object.selectedItem = index - object:update() - - return object -end - -local function listDeselect(object) - object.selectedItem = nil - object:update() - - return object -end - -local function listItemEventHandler(mainContainer, object, eventData) - if eventData[1] == "touch" or eventData[1] == "drag" then - object.parent:select(object:indexOf()) - mainContainer:drawOnScreen() - - if object.onTouch then - object.onTouch(mainContainer, object, eventDat) - end - end -end - -local function listAddItem(object, text) - local item = object:addChild(GUI.button(1, 1, 1, 1, 0, 0, 0, 0, text)) - - item.switchMode = true - item.animated = false - item.eventHandler = listItemEventHandler - - object:update() - - return item -end - -local function listSetAlignment(object, ...) - object:setCellAlignment(1, 1, ...) - return object -end - -local function listSetSpacing(object, ...) - object:setCellSpacing(1, 1, ...) - return object -end - -local function listSetDirection(object, ...) - object:setCellDirection(1, 1, ...) - object:update() - - return object -end - -local function listGetItem(object, index) - return object.children[index] -end - -function GUI.list(x, y, width, height, itemSize, spacing, backgroundColor, textColor, backgroundAlternatingColor, textAlternatingColor, backgroundSelectedColor, textSelectedColor, offsetMode) - local object = GUI.layout(x, y, width, height, 1, 1) - - object.colors = { - default = { - background = backgroundColor, - text = textColor - }, - alternating = { - background = backgroundAlternatingColor, - text = textAlternatingColor - }, - pressed = { - background = backgroundSelectedColor, - text = textSelectedColor - }, - } - - object.selectedItem = 1 - object.select = listSelect - object.deselect = listDeselect - object.offsetMode = offsetMode - object.itemSize = itemSize - object.update = listUpdate - object.addItem = listAddItem - object.getItem = listGetItem - object.setAlignment = listSetAlignment - object.setSpacing = listSetSpacing - object.setDirection = listSetDirection - object.draw = listDraw - - object:setAlignment(GUI.alignment.horizontal.left, GUI.alignment.vertical.top) - object:setSpacing(spacing) - object:setDirection(GUI.directions.vertical) - - return object -end - ------------------------------------------------------------------------------------------ -function GUI.highlightString(x, y, fromChar, limit, indentationWidth, colorScheme, patterns, s) - fromChar = fromChar or 1 - - local counter, symbols, colors, stringLength, bufferIndex, newFrameBackgrounds, newFrameForegrounds, newFrameSymbols, searchFrom, starting, ending = indentationWidth, {}, {}, unicode.len(s), buffer.getIndex(x, y), buffer.getNewFrameTables() - local toChar = math.min(stringLength, fromChar + limit - 1) - - for i = 1, stringLength do - symbols[i] = unicode.sub(s, i, i) - end - - for j = 1, #patterns do - searchFrom = 1 - - while true do - starting, ending = string.unicodeFind(s, patterns[j][1], searchFrom) - - if starting then - for i = starting + patterns[j][3], ending - patterns[j][4] do - colors[i] = colorScheme[patterns[j][2]] - end - else - break - end - - searchFrom = ending + 1 - patterns[j][4] - end - end - - -- Ебошим индентейшны - for i = fromChar, toChar do - if symbols[i] == " " then - colors[i] = colorScheme.indentation - - if counter == indentationWidth then - symbols[i], counter = "│", 0 - end - - counter = counter + 1 - else - break - end - end - - -- А тута уже сам текст - for i = fromChar, toChar do - newFrameForegrounds[bufferIndex], newFrameSymbols[bufferIndex] = colors[i] or colorScheme.text, symbols[i] or " " - bufferIndex = bufferIndex + 1 - end -end - ------------------------------------------------------------------------------------------- - --- buffer.flush() -- local mainContainer = GUI.fullScreenContainer() -- mainContainer:addChild(GUI.panel(1, 1, mainContainer.width, mainContainer.height, 0x2D2D2D)) --- local codeView = mainContainer:addChild(GUI.codeView(2, 2, 130, 30, {}, 1, 1, 1, {}, {}, true, 2)) - --- local file = io.open("/lib/color.lua", "r") --- for line in file:lines() do --- line = line:gsub("\t", " "):gsub("\r\n", "\n") --- table.insert(codeView.lines, line) --- codeView.maximumLineLength = math.max(codeView.maximumLineLength, unicode.len(line)) +-- -- Создаем вертикально ориентированный список +-- local verticalList = mainContainer:addChild(GUI.list(3, 2, 25, 30, 3, 0, 0xE1E1E1, 0x4B4B4B, 0xD2D2D2, 0x4B4B4B, 0x3366CC, 0xFFFFFF, false)) +-- verticalList:addItem("Hello world") +-- verticalList:addItem("This is test").onTouch = function() +-- GUI.error("Selected item: " .. verticalList.selectedItem) -- end --- file:close() +-- verticalList:addItem("Beautiful") +-- verticalList:addItem("Like a shit") + +-- -- Создаем горизонтально ориентированный список +-- local horizontalList = mainContainer:addChild(GUI.list(34, 2, 100, 3, 2, 0, 0xE1E1E1, 0x4B4B4B, 0xE1E1E1, 0x4B4B4B, 0x696969, 0xFFFFFF, true)) +-- horizontalList:setDirection(GUI.directions.horizontal) +-- horizontalList:setAlignment(GUI.alignment.horizontal.center, GUI.alignment.vertical.top) +-- horizontalList:addItem("Applications") +-- horizontalList:addItem("Libraries") +-- horizontalList:addItem("Scripts") +-- horizontalList:addItem("Wallpapers") -- mainContainer:drawOnScreen(true) -- mainContainer:startEventHandling() diff --git a/lib/MineOSInterface.lua b/lib/MineOSInterface.lua index d70b3735..dbf710ba 100755 --- a/lib/MineOSInterface.lua +++ b/lib/MineOSInterface.lua @@ -1121,13 +1121,6 @@ end ----------------------------------------- Windows patterns ----------------------------------------- -local function windowResize(window, width, height) - window.width, window.height = width, height - if window.onResize then window.onResize(width, height) end - - return window -end - function MineOSInterface.addWindow(window, preserveCoordinates) -- Чекаем коорды if not preserveCoordinates then @@ -1187,7 +1180,6 @@ function MineOSInterface.addWindow(window, preserveCoordinates) window.localX, window.localY = MineOSInterface.mainContainer.windowsContainer.children[lastIndex].localX + 4, MineOSInterface.mainContainer.windowsContainer.children[lastIndex].localY + 2 end - window.resize = windowResize window.close = function(window) local sameIconExists = false for i = 1, #MineOSInterface.mainContainer.dockContainer.children do @@ -1455,7 +1447,7 @@ function MineOSInterface.showErrorWindow(path, line, traceback) end lineCounter = lineCounter + 1 if lineCounter % 200 == 0 then - os.sleep(0.1) + computer.pullSignal(0) end end diff --git a/lib/color.lua b/lib/color.lua index bdcec442..43959777 100755 --- a/lib/color.lua +++ b/lib/color.lua @@ -3,9 +3,9 @@ local computer = require("computer") -------------------------------------------------------------------------------- -local palette = { 0x000000, 0x000040, 0x000080, 0x0000BF, 0x0000FF, 0x002400, 0x002440, 0x002480, 0x0024BF, 0x0024FF, 0x004900, 0x004940, 0x004980, 0x0049BF, 0x0049FF, 0x006D00, 0x006D40, 0x006D80, 0x006DBF, 0x006DFF, 0x009200, 0x009240, 0x009280, 0x0092BF, 0x0092FF, 0x00B600, 0x00B640, 0x00B680, 0x00B6BF, 0x00B6FF, 0x00DB00, 0x00DB40, 0x00DB80, 0x00DBBF, 0x00DBFF, 0x00FF00, 0x00FF40, 0x00FF80, 0x00FFBF, 0x00FFFF, 0x0F0F0F, 0x1E1E1E, 0x2D2D2D, 0x330000, 0x330040, 0x330080, 0x3300BF, 0x3300FF, 0x332400, 0x332440, 0x332480, 0x3324BF, 0x3324FF, 0x334900, 0x334940, 0x334980, 0x3349BF, 0x3349FF, 0x336D00, 0x336D40, 0x336D80, 0x336DBF, 0x336DFF, 0x339200, 0x339240, 0x339280, 0x3392BF, 0x3392FF, 0x33B600, 0x33B640, 0x33B680, 0x33B6BF, 0x33B6FF, 0x33DB00, 0x33DB40, 0x33DB80, 0x33DBBF, 0x33DBFF, 0x33FF00, 0x33FF40, 0x33FF80, 0x33FFBF, 0x33FFFF, 0x3C3C3C, 0x4B4B4B, 0x5A5A5A, 0x660000, 0x660040, 0x660080, 0x6600BF, 0x6600FF, 0x662400, 0x662440, 0x662480, 0x6624BF, 0x6624FF, 0x664900, 0x664940, 0x664980, 0x6649BF, 0x6649FF, 0x666D00, 0x666D40, 0x666D80, 0x666DBF, 0x666DFF, 0x669200, 0x669240, 0x669280, 0x6692BF, 0x6692FF, 0x66B600, 0x66B640, 0x66B680, 0x66B6BF, 0x66B6FF, 0x66DB00, 0x66DB40, 0x66DB80, 0x66DBBF, 0x66DBFF, 0x66FF00, 0x66FF40, 0x66FF80, 0x66FFBF, 0x66FFFF, 0x696969, 0x787878, 0x878787, 0x969696, 0x990000, 0x990040, 0x990080, 0x9900BF, 0x9900FF, 0x992400, 0x992440, 0x992480, 0x9924BF, 0x9924FF, 0x994900, 0x994940, 0x994980, 0x9949BF, 0x9949FF, 0x996D00, 0x996D40, 0x996D80, 0x996DBF, 0x996DFF, 0x999200, 0x999240, 0x999280, 0x9992BF, 0x9992FF, 0x99B600, 0x99B640, 0x99B680, 0x99B6BF, 0x99B6FF, 0x99DB00, 0x99DB40, 0x99DB80, 0x99DBBF, 0x99DBFF, 0x99FF00, 0x99FF40, 0x99FF80, 0x99FFBF, 0x99FFFF, 0xA5A5A5, 0xB4B4B4, 0xC3C3C3, 0xCC0000, 0xCC0040, 0xCC0080, 0xCC00BF, 0xCC00FF, 0xCC2400, 0xCC2440, 0xCC2480, 0xCC24BF, 0xCC24FF, 0xCC4900, 0xCC4940, 0xCC4980, 0xCC49BF, 0xCC49FF, 0xCC6D00, 0xCC6D40, 0xCC6D80, 0xCC6DBF, 0xCC6DFF, 0xCC9200, 0xCC9240, 0xCC9280, 0xCC92BF, 0xCC92FF, 0xCCB600, 0xCCB640, 0xCCB680, 0xCCB6BF, 0xCCB6FF, 0xCCDB00, 0xCCDB40, 0xCCDB80, 0xCCDBBF, 0xCCDBFF, 0xCCFF00, 0xCCFF40, 0xCCFF80, 0xCCFFBF, 0xCCFFFF, 0xD2D2D2, 0xE1E1E1, 0xF0F0F0, 0xFF0000, 0xFF0040, 0xFF0080, 0xFF00BF, 0xFF00FF, 0xFF2400, 0xFF2440, 0xFF2480, 0xFF24BF, 0xFF24FF, 0xFF4900, 0xFF4940, 0xFF4980, 0xFF49BF, 0xFF49FF, 0xFF6D00, 0xFF6D40, 0xFF6D80, 0xFF6DBF, 0xFF6DFF, 0xFF9200, 0xFF9240, 0xFF9280, 0xFF92BF, 0xFF92FF, 0xFFB600, 0xFFB640, 0xFFB680, 0xFFB6BF, 0xFFB6FF, 0xFFDB00, 0xFFDB40, 0xFFDB80, 0xFFDBBF, 0xFFDBFF, 0xFFFF00, 0xFFFF40, 0xFFFF80, 0xFFFFBF, 0xFFFFFF } -local mathFloor, mathMax, mathMin, mathHuge, mathModf = math.floor, math.max, math.min, math.huge, math.modf -local integerToRGB, RGBToInteger, blend, transition +local palette = {0x000000, 0x000040, 0x000080, 0x0000BF, 0x0000FF, 0x002400, 0x002440, 0x002480, 0x0024BF, 0x0024FF, 0x004900, 0x004940, 0x004980, 0x0049BF, 0x0049FF, 0x006D00, 0x006D40, 0x006D80, 0x006DBF, 0x006DFF, 0x009200, 0x009240, 0x009280, 0x0092BF, 0x0092FF, 0x00B600, 0x00B640, 0x00B680, 0x00B6BF, 0x00B6FF, 0x00DB00, 0x00DB40, 0x00DB80, 0x00DBBF, 0x00DBFF, 0x00FF00, 0x00FF40, 0x00FF80, 0x00FFBF, 0x00FFFF, 0x0F0F0F, 0x1E1E1E, 0x2D2D2D, 0x330000, 0x330040, 0x330080, 0x3300BF, 0x3300FF, 0x332400, 0x332440, 0x332480, 0x3324BF, 0x3324FF, 0x334900, 0x334940, 0x334980, 0x3349BF, 0x3349FF, 0x336D00, 0x336D40, 0x336D80, 0x336DBF, 0x336DFF, 0x339200, 0x339240, 0x339280, 0x3392BF, 0x3392FF, 0x33B600, 0x33B640, 0x33B680, 0x33B6BF, 0x33B6FF, 0x33DB00, 0x33DB40, 0x33DB80, 0x33DBBF, 0x33DBFF, 0x33FF00, 0x33FF40, 0x33FF80, 0x33FFBF, 0x33FFFF, 0x3C3C3C, 0x4B4B4B, 0x5A5A5A, 0x660000, 0x660040, 0x660080, 0x6600BF, 0x6600FF, 0x662400, 0x662440, 0x662480, 0x6624BF, 0x6624FF, 0x664900, 0x664940, 0x664980, 0x6649BF, 0x6649FF, 0x666D00, 0x666D40, 0x666D80, 0x666DBF, 0x666DFF, 0x669200, 0x669240, 0x669280, 0x6692BF, 0x6692FF, 0x66B600, 0x66B640, 0x66B680, 0x66B6BF, 0x66B6FF, 0x66DB00, 0x66DB40, 0x66DB80, 0x66DBBF, 0x66DBFF, 0x66FF00, 0x66FF40, 0x66FF80, 0x66FFBF, 0x66FFFF, 0x696969, 0x787878, 0x878787, 0x969696, 0x990000, 0x990040, 0x990080, 0x9900BF, 0x9900FF, 0x992400, 0x992440, 0x992480, 0x9924BF, 0x9924FF, 0x994900, 0x994940, 0x994980, 0x9949BF, 0x9949FF, 0x996D00, 0x996D40, 0x996D80, 0x996DBF, 0x996DFF, 0x999200, 0x999240, 0x999280, 0x9992BF, 0x9992FF, 0x99B600, 0x99B640, 0x99B680, 0x99B6BF, 0x99B6FF, 0x99DB00, 0x99DB40, 0x99DB80, 0x99DBBF, 0x99DBFF, 0x99FF00, 0x99FF40, 0x99FF80, 0x99FFBF, 0x99FFFF, 0xA5A5A5, 0xB4B4B4, 0xC3C3C3, 0xCC0000, 0xCC0040, 0xCC0080, 0xCC00BF, 0xCC00FF, 0xCC2400, 0xCC2440, 0xCC2480, 0xCC24BF, 0xCC24FF, 0xCC4900, 0xCC4940, 0xCC4980, 0xCC49BF, 0xCC49FF, 0xCC6D00, 0xCC6D40, 0xCC6D80, 0xCC6DBF, 0xCC6DFF, 0xCC9200, 0xCC9240, 0xCC9280, 0xCC92BF, 0xCC92FF, 0xCCB600, 0xCCB640, 0xCCB680, 0xCCB6BF, 0xCCB6FF, 0xCCDB00, 0xCCDB40, 0xCCDB80, 0xCCDBBF, 0xCCDBFF, 0xCCFF00, 0xCCFF40, 0xCCFF80, 0xCCFFBF, 0xCCFFFF, 0xD2D2D2, 0xE1E1E1, 0xF0F0F0, 0xFF0000, 0xFF0040, 0xFF0080, 0xFF00BF, 0xFF00FF, 0xFF2400, 0xFF2440, 0xFF2480, 0xFF24BF, 0xFF24FF, 0xFF4900, 0xFF4940, 0xFF4980, 0xFF49BF, 0xFF49FF, 0xFF6D00, 0xFF6D40, 0xFF6D80, 0xFF6DBF, 0xFF6DFF, 0xFF9200, 0xFF9240, 0xFF9280, 0xFF92BF, 0xFF92FF, 0xFFB600, 0xFFB640, 0xFFB680, 0xFFB6BF, 0xFFB6FF, 0xFFDB00, 0xFFDB40, 0xFFDB80, 0xFFDBBF, 0xFFDBFF, 0xFFFF00, 0xFFFF40, 0xFFFF80, 0xFFFFBF, 0xFFFFFF} +local mathFloor, mathMax, mathMin, mathModf = math.floor, math.max, math.min, math.modf +local integerToRGB, RGBToInteger, blend, transition, to8Bit local color = {} @@ -13,7 +13,10 @@ local color = {} -- Optimized Lua 5.3 bitwise support if computer.getArchitecture and computer.getArchitecture() == "Lua 5.3" then - integerToRGB, RGBToInteger, blend, transition = load([[ + integerToRGB, RGBToInteger, blend, transition, to8Bit = load([[ + local palette = select(1, ...) + local mathHuge = math.huge + return function(integerColor) return integerColor >> 16, integerColor >> 8 & 0xFF, integerColor & 0xFF @@ -23,8 +26,8 @@ if computer.getArchitecture and computer.getArchitecture() == "Lua 5.3" then return r << 16 | g << 8 | b end, - function(firstColor, secondColor, transparency) - local invertedTransparency, r1, g1, b1, r2, g2, b2 = 1 - transparency, firstColor >> 16, firstColor >> 8 & 0xFF, firstColor & 0xFF, secondColor >> 16, secondColor >> 8 & 0xFF, secondColor & 0xFF + function(color1, color2, transparency) + local invertedTransparency, r1, g1, b1, r2, g2, b2 = 1 - transparency, color1 >> 16, color1 >> 8 & 0xFF, color1 & 0xFF, color2 >> 16, color2 >> 8 & 0xFF, color2 & 0xFF return (r2 * invertedTransparency + r1 * transparency) // 1 << 16 | @@ -39,29 +42,62 @@ if computer.getArchitecture and computer.getArchitecture() == "Lua 5.3" then (r1 + (r2 - r1) * position) // 1 << 16 | (g1 + (g2 - g1) * position) // 1 << 8 | (b1 + (b2 - b1) * position) // 1 - end - ]])() -else - integerToRGB, RGBToInteger, blend, transition = load([[ - local function integerToRGB(integerColor) - local r = integerColor / 65536 - r = r - r % 1 - local g = (integerColor - r * 65536) / 256 - g = g - g % 1 + end, + + function(color24Bit) + local r, g, b, closestDelta, closestIndex, delta, paletteColor, paletteR, paletteG, paletteB = color24Bit >> 16, color24Bit >> 8 & 0xFF, color24Bit & 0xFF, mathHuge, 1 + + for i = 1, #palette do + paletteColor = palette[i] + + if color24Bit == paletteColor then + return i - 1 + else + paletteR, paletteG, paletteB = paletteColor >> 16, paletteColor >> 8 & 0xFF, paletteColor & 0xFF + + delta = (paletteR - r) ^ 2 + (paletteG - g) ^ 2 + (paletteB - b) ^ 2 + if delta < closestDelta then + closestDelta, closestIndex = delta, i + end + end + end + + return closestIndex - 1 + end + ]])(palette) +else + integerToRGB, RGBToInteger, blend, transition, to8Bit = load([[ + local palette = select(1, ...) + local mathHuge = math.huge - return r, g, integerColor - r * 65536 - g * 256 - end - return - integerToRGB, + function(integerColor) + local r = integerColor / 65536 + r = r - r % 1 + local g = (integerColor - r * 65536) / 256 + g = g - g % 1 + + return r, g, integerColor - r * 65536 - g * 256 + end, function(r, g, b) return r * 65536 + g * 256 + b end, - function(firstColor, secondColor, transparency) - local invertedTransparency, r1, g1, b1 = 1 - transparency, integerToRGB(firstColor) - local r2, g2, b2 = integerToRGB(secondColor) + function(color1, color2, transparency) + local invertedTransparency = 1 - transparency + + local r1 = color1 / 65536 + r1 = r1 - r1 % 1 + local g1 = (color1 - r1 * 65536) / 256 + g1 = g1 - g1 % 1 + local b1 = color1 - r1 * 65536 - g1 * 256 + + local r2 = color2 / 65536 + r2 = r2 - r2 % 1 + local g2 = (color2 - r2 * 65536) / 256 + g2 = g2 - g2 % 1 + local b2 = color2 - r2 * 65536 - g2 * 256 local r, g, b = r2 * invertedTransparency + r1 * transparency, @@ -75,8 +111,17 @@ else end, function(color1, color2, position) - local r1, g1, b1 = integerToRGB(color1) - local r2, g2, b2 = integerToRGB(color2) + local r1 = color1 / 65536 + r1 = r1 - r1 % 1 + local g1 = (color1 - r1 * 65536) / 256 + g1 = g1 - g1 % 1 + local b1 = color1 - r1 * 65536 - g1 * 256 + + local r2 = color2 / 65536 + r2 = r2 - r2 % 1 + local g2 = (color2 - r2 * 65536) / 256 + g2 = g2 - g2 % 1 + local b2 = color2 - r2 * 65536 - g2 * 256 local r, g, b = r1 + (r2 - r1) * position, @@ -87,8 +132,38 @@ else (r - r % 1) * 65536 + (g - g % 1) * 256 + (b - b % 1) + end, + + function(color24Bit) + local closestDelta, closestIndex, delta, paletteColor, paletteR, paletteG, paletteB = mathHuge, 1 + + local r = color24Bit / 65536 + r = r - r % 1 + local g = (color24Bit - r * 65536) / 256 + g = g - g % 1 + local b = color24Bit - r * 65536 - g * 256 + + for index = 1, #palette do + paletteColor = palette[index] + if color24Bit == paletteColor then + return index - 1 + else + paletteR = color24Bit / 65536 + paletteR = paletteR - paletteR % 1 + paletteG = (color24Bit - paletteR * 65536) / 256 + paletteG = paletteG - paletteG % 1 + paletteB = color24Bit - paletteR * 65536 - paletteG * 256 + + delta = (paletteR - r) ^ 2 + (paletteG - g) ^ 2 + (paletteB - b) ^ 2 + if delta < closestDelta then + closestDelta, closestIndex = delta, index + end + end + end + + return closestIndex - 1 end - ]])() + ]])(palette) end -------------------------------------------------------------------------------- @@ -140,25 +215,6 @@ end -------------------------------------------------------------------------------- -local function to8Bit(color24Bit) - local closestDelta, r, g, b, closestIndex, delta, paletteR, paletteG, paletteB = mathHuge, integerToRGB(color24Bit) - - for index = 1, #palette do - if color24Bit == palette[index] then - return index - 1 - else - paletteR, paletteG, paletteB = integerToRGB(palette[index]) - delta = (paletteR - r) ^ 2 + (paletteG - g) ^ 2 + (paletteB - b) ^ 2 - - if delta < closestDelta then - closestDelta, closestIndex = delta, index - end - end - end - - return closestIndex - 1 -end - local function to24Bit(color8Bit) return palette[color8Bit + 1] end