Skip to content

Num to Sprite

Irtsa edited this page Feb 22, 2025 · 4 revisions

Will allow you to print off pictures into the terminal using sprite. One must provide a file that contains a set of values. One may either use the monoSprite() function or the rgbSprite() function. The monoSprite() function expects a file that contains "brightness" values ranging from 000-255. Note that if you must pad values on the left side with 0s if it isn't 3 digits long (50 becomes 050). Every new line in the given file will be a new line in the resulting picture. Note that the final line represents how large the picture will be. The rgbSprite() function works in the same way as monoSprite() but note that rgb values are 9 digits long (3 groups of 3 digit long values) (This means 122103220 is how you would store a single value).

Note that if it seems that the picture is only a single pixel, check the given file and make sure that the final line is indeed the size and not an empty line (can happen).


Source Code

//Additional Functions (from bltings)
list.applyFunction = function(func)
    for i in range(self.len - 1)
        self[i] = func(self[i])
    end for
    return self
end function



string.group = function(groupsize)
    strings = []
    for i in range(0, str(self).len, groupsize)
        strings.push(str(self)[i : [i + groupsize, str(self).len].min])
    end for
    return strings
end function





//Necessary separation of the lfill function from String class. (custom)
__lfill = function(string)
    return ("0" * [0, 3 - string.len]).max + string
end function





//Public Functions
monoSprite = function(filepath, zeroIsInvisible = true)
    file = get_shell.host_computer.File(filepath)
    if not file then exit("<color=red>Error: File does not exist.")
    
    imageData = file.get_content.split("\n")
    imageSize = imageData[-1].to_int
    imageData.pop

    for i in range(imageData.len - 1)
        imageData[i] = imageData[i].group(3)
        if imageData[i][-1].len < 3 then exit("<color=red>Error: Could not correctly parse image data in file.")
    end for

    picture = ""
    for a in range(0, imageData.len - 1)
        for b in range(0, imageData[a].len - 1)
            color = hex(imageData[a][b])
            if color == "0" and zeroIsInvisible then color = "0b0b0c" else color = color + color * (6 / color.len)
            picture = picture + "<pos=" + floor(b * (imageSize * 0.6)) + "px><size=" + imageSize + "><sprite=0 color=#" + color + ">"
        end for
        picture = picture + "<voffset=-" + floor((a + 1) * imageSize * 1.2) + "px>"
    end for
    
    return picture
end function



rgbSprite = function(filepath, zeroIsInvisible = true)
    file = get_shell.host_computer.File(filepath)
    if not file then exit("<color=red>Error: File does not exist.")
    
    imageData = file.get_content.split("\n")
    imageSize = imageData[-1].to_int
    imageData.pop

    for i in range(imageData.len - 1)
        imageData[i] = imageData[i].group(9)
        if imageData[i][-1].len < 9 then exit("<color=red>Error: Could not correctly parse image data in file.")
    end for

    picture = ""
    for a in range(0, imageData.len - 1)
        for b in range(0, imageData[a].len - 1)
            color = imageData[a][b].group(3)
            if color[-1].len < 3 then exit("<color=red>Error: Could not correctly parse image data in file.")
            color.applyFunction(@hex)
            color.applyFunction(@__lfill)
            color = color.join("")
            if color == "000000" and zeroIsInvisible then color = "0b0b0c"
            picture = picture + "<pos=" + floor(b * imageSize * 0.6) + "px><size=" + imageSize + "><sprite=0 color=#" + color + ">"
        end for
        picture = picture + "<voffset=-" + floor((a + 1) * imageSize * 1.2) + "px>"
    end for
    
    return picture
end function



Example File for monoSprite()

100100100100100100
100100100100100100
100100100100100100
100100100100100100
100100100100100100
100100100100100100
20

Example File for rgbSprite()

100100200100100200100100200100100200100100200100100200
100100200100100200100100200100100200100100200100100200
100100200100100200100100200100100200100100200100100200
100100200100100200100100200100100200100100200100100200
100100200100100200100100200100100200100100200100100200
100100200100100200100100200100100200100100200100100200
20
Clone this wiki locally