项目作者: nick3499

项目描述 :
Generate random design in terminal emulator which could be screen captured and cropped to be used as a background image design.
高级语言: Jupyter Notebook
项目地址: git://github.com/nick3499/generate_random_design_nick3499.git
创建时间: 2020-12-13T09:06:29Z
项目社区:https://github.com/nick3499/generate_random_design_nick3499

开源协议:MIT License

下载


generate_random_design_nick3499

Generate random design which displays in terminal emulator

screen capture

Unix Shebang

The human-readable Unix shebang #! translates to the magic number 0x23 0x21. Based on the shebang line #!/usr/bin/env python, the env shell command interprets the generate_design.py text file as a Python app.

  1. >>> hex(ord('#'))
  2. '0x23'
  3. >>> hex(ord('!'))
  4. '0x21'

Documentation

  1. >>> __doc__
  2. 'Generate random colored background design.'
  3. >>> generate_design.__doc__
  4. 'Generate random background design.'
  • __doc__ string contains module’s documentation
  • generate_design.__doc__ string contains the function’s documentation

Import

  1. from random import randrange

The random.randrange() method is used to generate a random number from 1 to 5, in order to randomly select one color from the scheme.

function

  1. def generate_design():

The block keyword def starts the definition and organization of the function’s code; the generate_design() method’s code block.

Inputs

  1. print('\x1b[1;34mEnter hex color\x1b[0m: (example: 50514f) or leave blank')
  2. hex1 = input('hex 1: ').upper() or '000000'
  3. hex2 = input('hex 2: ').upper() or '000000'
  4. hex3 = input('hex 3: ').upper() or '000000'
  5. hex4 = input('hex 4: ').upper() or '000000'
  6. hex5 = input('hex 5: ').upper() or '000000'

The input() builtin function returns a prompt for user input. The following is an example of such input:

  1. Enter hex color: (example: 50514f) or leave blank
  2. hex 1: 020202
  3. hex 2: 0d2818
  4. hex 3: 04471c
  5. hex 4: 058c42
  6. hex 5:

Since the hex 5: input was left blank, hex5 will default to '000000'

Concatenated Hex Strings

  1. hex_strs = hex1 + hex2 + hex3 + hex4 + hex5

Hex color code inputs are all concatenated into a single string.

  1. '0202020d281804471c058c42000000'

Generate Hex Pair List

  1. hex_pairs = []
  2. n = 0
  3. for i in range(0, 5):
  4. hex_pairs.append([hex_strs[n:n+2], hex_strs[n+2:n+4], hex_strs[n+4:n+6]])
  5. n += 6

n is used to set the first index point in the long string for the start of each hex pair list. Slice notation is used to separate each 6-character code into three items. For example, the hex color code '058c42' splits to ['05', '8c', '42'], which represents a specific color.

  1. [['02', '02', '02'], ['0d', '28', '18'], ['04', '47', '1c'], ['05', '8c', '42'], ['00', '00', '00']]

Convert to ASCII

  1. ascii_nums = []
  2. for i in range(0, 5):
  3. ascii_nums.append([int(hex_pairs[i][0], 16), int(hex_pairs[i][0], 16),
  4. int(hex_pairs[i][0], 16)])

Hex pairs are converted to ASCII code decimal numbers, as demonstrated below:

  1. >>> int('05', 16)
  2. 5
  3. >>> int('8c', 16)
  4. 140
  5. >>> int('42', 16)
  6. 66
  1. [[2, 2, 2], [13, 40, 24], [4, 71, 28], [5, 140, 66], [0, 0, 0]]

The ASCII value numbers will used for color formatting: f'\x1b[48;2;5;140;66m \x1b[0m'.

  • \x1b[ sequence escapes the color formatting.
  • 48;2; sequence sets formatting to background color.
  • 5;140;66m sequence sets RGB color values.
  • \x1b[0m sequence sets color to default.

Generate Design

  1. colors = [] # color scheme strings
  2. for i in range(0, 5):
  3. colors.append(f'\x1b[48;2;{ascii_nums[i][0]};{ascii_nums[i][1]};{ascii_nums[i][2]}m \x1b[0m')
  4. color_strings = ''
  5. for i in range(0, 160):
  6. color_strings += colors[randrange(0, 5)]
  7. for i in range(0, 40):
  8. print(color_strings)

The f-string f'\x1b[48;2;{ascii_nums[0][0]};{ascii_nums[0][1]};{ascii_nums[0][2]}m \x1b[0m' background color formats a single space. The sequence 48;2; is for background color modification, followed by 13;40;24, which sets RGB values, for example.

The first for loop generates a list which is assigned to colors variable. A list of formatted background-colored spaces.

The second for loop interates the color_pattern += colors[randrange(1, 5)] instruction which appends a random background-color space to the color_pattern attribute. The randrange(1, 5) call returns an integer from 1 to 5 which is used to pick a random color index.

The third for loop simply repeats the generated random pattern stored in color_pattern to the extent of 40 identical lines.

If Block

  1. if __name__ == '__main__':
  2. generate_design()

The final if block executes the generate_design() method of the script runs as a standalone app. For example, if this app was imported into another app, its __name__ would no longer be __main__, so generate_design() would not execute unless it was called.