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