~zethra/sasha-fetch

ref: 6bce7110605d09a0006b4217c490db7b72df4c5a sasha-fetch/generate.py -rwxr-xr-x 2.8 KiB
6bce7110 — Sashanoraa Don't print the seed 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python3

import sys
import math
import random

COLOR_ANSI = (
    (0x00, 0x00, 0x00), (0xcd, 0x00, 0x00),
    (0x00, 0xcd, 0x00), (0xcd, 0xcd, 0x00),
    (0x00, 0x00, 0xee), (0xcd, 0x00, 0xcd),
    (0x00, 0xcd, 0xcd), (0xe5, 0xe5, 0xe5),
    (0x7f, 0x7f, 0x7f), (0xff, 0x00, 0x00),
    (0x00, 0xff, 0x00), (0xff, 0xff, 0x00),
    (0x5c, 0x5c, 0xff), (0xff, 0x00, 0xff),
    (0x00, 0xff, 0xff), (0xff, 0xff, 0xff),
)

class LolCat:
    def __init__(self, mode=256):
        self.mode = mode
        self.os = random.randint(10, 25)
        self.spread = 5.0
        self.freq = 0.1

    def _distance(self, rgb1, rgb2):
        return sum(map(lambda c: (c[0] - c[1]) ** 2,
            zip(rgb1, rgb2)))

    def ansi(self, rgb):
        r, g, b = rgb

        if self.mode in (8, 16):
            colors = COLOR_ANSI[:self.mode]
            matches = [(self._distance(c, map(int, rgb)), i) for i, c in enumerate(colors)]
            matches.sort()
            color = matches[0][1]

            return '3%d' % (color,)
        else:
            gray_possible = True
            sep = 2.5
            gray = 0

            while gray_possible:
                if r < sep or g < sep or b < sep:
                    gray = r < sep and g < sep and b < sep
                    gray_possible = False

                sep += 42.5

            if gray:
                color = 232 + int(float(sum(rgb) / 33.0))
            else:
                color = sum([16]+[int(6 * float(val)/256) * mod
                    for val, mod in zip(rgb, [36, 6, 1])])

            return '38;5;%d' % (color,)

    def wrap(self, *codes):
        return '\x1b[%sm' % (''.join(codes),)

    def rainbow(self, freq, i):
        r = math.sin(freq * i) * 127 + 128
        g = math.sin(freq * i + 2 * math.pi / 3) * 127 + 128
        b = math.sin(freq * i + 4 * math.pi / 3) * 127 + 128
        return [r, g, b]

    def apply_color(self, s):
        output = ""
        for i, c in enumerate(s):
            rgb = self.rainbow(self.freq, self.os + i / self.spread)
            output += ''.join([
                self.wrap(self.ansi(rgb)),
                c
            ])
        return output

f = open('profile')
message = '''
Sashanoraa
----------
Pronouns:   any
Gender:     Non-binary / Gender Queer
Matrix:     @zethra:matrix.org
Email:      ben@benaaron.dev
Mastodon:   @zethra@fosstodon.org
Website:    sashanoraa.gay
Protonmail: sashanoraa@protonmail.com
'''.splitlines()

img = f.read().splitlines()

titles = LolCat()
data = LolCat()
data.os = ((titles.os - 10 + 7) % 15) + 10

output = open(sys.argv[1], 'w')

for (i, m) in zip(img, message):
    if ':' in m:
        parts = m.split(':')
        output.write(f'{i} {titles.apply_color(parts[0])}: {data.apply_color(":".join(parts[1:]))}\n')
    else:
        output.write(f'{i} {titles.apply_color(m)}\n')

for i in img[len(message):]:
    output.write(i + '\n')