From 72c942c5ca3d1ebcef0321c74cb6fb06bc1f7d0f Mon Sep 17 00:00:00 2001 From: Igor Timofeev Date: Thu, 21 Apr 2016 11:19:46 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D1=81=D1=82=D0=B8,=20=D0=B1?= =?UTF-8?q?=D1=80=D0=B0=D1=82!=20=D0=9C=D1=8B=20=D0=BE=D0=B1=D0=BE=D1=81?= =?UTF-8?q?=D1=80=D0=B0=D0=BB=D0=B8=D1=81=D1=8C!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/colorlib.lua | 62 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/lib/colorlib.lua b/lib/colorlib.lua index 5fa24ec1..c7ce63bc 100644 --- a/lib/colorlib.lua +++ b/lib/colorlib.lua @@ -122,31 +122,57 @@ local palette = { 0xFF92FF, 0xFFB600, 0xFFB640, 0xFFB680, 0xFFB6BF, 0xFFB6FF, 0xFFDB00, 0xFFDB40, 0xFFDB80, 0xFFDBBF, 0xFFDBFF, 0xFFFF00, 0xFFFF40, 0xFFFF80, 0xFFFFBF, 0xFFFFFF, } -local function searchClosestColor(startIndex, endIndex, requestedColor) - local difference = endIndex - startIndex - local centerIndex = math.floor(difference / 2 + startIndex) +-- local function searchClosestColor(startIndex, endIndex, requestedColor) +-- local difference = endIndex - startIndex +-- local centerIndex = math.floor(difference / 2 + startIndex) - if difference > 1 then - if requestedColor >= palette[centerIndex] then - return searchClosestColor(centerIndex, endIndex, requestedColor) - else - return searchClosestColor(startIndex, centerIndex, requestedColor) - end - else - if math.abs(requestedColor - palette[startIndex]) > math.abs(palette[endIndex] - requestedColor) then - return endIndex - 1 - else - return startIndex - 1 - end - end -end +-- if difference > 1 then +-- if requestedColor >= palette[centerIndex] then +-- return searchClosestColor(centerIndex, endIndex, requestedColor) +-- else +-- return searchClosestColor(startIndex, centerIndex, requestedColor) +-- end +-- else +-- if math.abs(requestedColor - palette[startIndex]) > math.abs(palette[endIndex] - requestedColor) then +-- return endIndex - 1 +-- else +-- return startIndex - 1 +-- end +-- end +-- end function colorlib.convert24BitTo8Bit(hex24) - return searchClosestColor(1, #palette, hex24) + local encodedIndex = nil + local colorMatchFactor = nil + local colorMatchFactor_min = math.huge + + local red24, green24, blue24 = colorlib.HEXtoRGB(hex24) + + for colorIndex, colorPalette in ipairs(palette) do + local redPalette, greenPalette, bluePalette = colorlib.HEXtoRGB(colorPalette) + + colorMatchFactor = (redPalette-red24)^2 + (greenPalette-green24)^2 + (bluePalette-blue24)^2 + + if (colorMatchFactor < colorMatchFactor_min) then + encodedIndex = colorIndex + colorMatchFactor_min = colorMatchFactor + end + end + + return encodedIndex - 1 + -- return searchClosestColor(1, #palette, hex24) end function colorlib.convert8BitTo24Bit(hex8) return palette[hex8 + 1] end +function colorlib.debugColorCompression(color) + local compressedColor = colorlib.convert24BitTo8Bit(color) + local decompressedColor = colorlib.convert8BitTo24Bit(compressedColor) + print("Исходный цвет: " .. string.format("0x%06X", color)) + print("Сжатый цвет: " .. string.format("0x%02X", compressedColor)) + print("Расжатый цвет: " .. string.format("0x%06X", decompressedColor)) +end + return colorlib