From bd03c17b6b197d372e3928ad4c03ae2865ea613d Mon Sep 17 00:00:00 2001 From: Igor Timofeev Date: Sat, 16 Apr 2016 02:16:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D1=85,=20=D1=81=D1=83=D0=BA=D0=B0!=20?= =?UTF-8?q?=D0=9E=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BE=D1=82=20=D0=B1=D0=BE=D0=B3=D0=B0!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/colorlib.lua | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/lib/colorlib.lua b/lib/colorlib.lua index 28d3f4d2..5fa24ec1 100644 --- a/lib/colorlib.lua +++ b/lib/colorlib.lua @@ -122,26 +122,27 @@ local palette = { 0xFF92FF, 0xFFB600, 0xFFB640, 0xFFB680, 0xFFB6BF, 0xFFB6FF, 0xFFDB00, 0xFFDB40, 0xFFDB80, 0xFFDBBF, 0xFFDBFF, 0xFFFF00, 0xFFFF40, 0xFFFF80, 0xFFFFBF, 0xFFFFFF, } --- Конвертер -function colorlib.convert24BitTo8Bit(hex24) - local encodedIndex = nil - local colorMatchFactor = nil - local colorMatchFactor_min = math.huge +local function searchClosestColor(startIndex, endIndex, requestedColor) + local difference = endIndex - startIndex + local centerIndex = math.floor(difference / 2 + startIndex) - 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 + 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 +end - return encodedIndex - 1 +function colorlib.convert24BitTo8Bit(hex24) + return searchClosestColor(1, #palette, hex24) end function colorlib.convert8BitTo24Bit(hex8)