{"id":16364,"date":"2020-06-20T22:33:54","date_gmt":"2020-06-21T02:33:54","guid":{"rendered":"https:\/\/scruss.com\/blog\/?p=16364"},"modified":"2020-06-21T10:57:16","modified_gmt":"2020-06-21T14:57:16","slug":"terminal-colour-silliness-with-python","status":"publish","type":"post","link":"https:\/\/scruss.com\/blog\/2020\/06\/20\/terminal-colour-silliness-with-python\/","title":{"rendered":"terminal colour silliness with Python"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"584\" height=\"327\" src=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2020\/06\/2020-06-20-222032_584x327_scrot.png\" alt=\"\" class=\"wp-image-16365\" srcset=\"https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2020\/06\/2020-06-20-222032_584x327_scrot.png 584w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2020\/06\/2020-06-20-222032_584x327_scrot-320x179.png 320w, https:\/\/scruss.com\/wordpress\/wp-content\/uploads\/2020\/06\/2020-06-20-222032_584x327_scrot-160x90.png 160w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><figcaption>terminal text in rainbows<\/figcaption><\/figure>\n\n\n\n<p>Using <a href=\"https:\/\/pypi.org\/project\/ansicolors\/\">ansicolors<\/a>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#!\/usr\/bin\/python3\n# -*- coding: utf-8 -*-\n# colourshen.py - stdin to rainbow stdout\n# scruss, 2020-06\n\nfrom colors import *            # see https:\/\/pypi.org\/project\/ansicolors\/\nimport sys\nwheel_pos = 0\n\n\ndef cos_wheel(pos):\n    # Input a value 0 to 255 to get a colour value.\n    # scruss (Stewart Russell) - 2019-03 - CC-BY-SA\n    from math import cos, pi\n    if pos &lt; 0:\n        return (0, 0, 0)\n    pos %= 256\n    pos \/= 255.0\n    return (int(255 * (1 + cos(pos * 2 * pi)) \/ 2),\n            int(255 * (1 + cos((pos - 1 \/ 3.0) * 2 * pi)) \/ 2),\n            int(255 * (1 + cos((pos - 2 \/ 3.0) * 2 * pi)) \/ 2))\n\n\ndef hex_wheel(pos):\n    rgb = cos_wheel(pos)\n    return(&#039;#%02x%02x%02x&#039; % rgb)\n\n\ndef wheel_print(s):\n    global wheel_pos\n    incr = int(256\/(1+len(s)))-1\n    if incr &lt; 1:\n        incr = 1\n    for c in s:\n        print(color(c, fg=hex_wheel(wheel_pos)), end=&#039;&#039;)\n        wheel_pos = (wheel_pos+incr) % 256\n    print()\n\n\nfor txt in sys.stdin:\n    wheel_print(txt.rstrip())\n\n<\/pre><\/div>\n\n\n<p>(fixed a very obvious <em>ahem!<\/em> in the code, hope no-one noticed \u00e2\u20ac\u00a6)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using ansicolors: (fixed a very obvious ahem! in the code, hope no-one noticed \u00e2\u20ac\u00a6)<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2],"tags":[],"class_list":["post-16364","post","type-post","status-publish","format-standard","hentry","category-goatee-stroking-musing-or-something"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pQNZZ-4fW","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/16364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/comments?post=16364"}],"version-history":[{"count":3,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/16364\/revisions"}],"predecessor-version":[{"id":16384,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/posts\/16364\/revisions\/16384"}],"wp:attachment":[{"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/media?parent=16364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/categories?post=16364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scruss.com\/blog\/wp-json\/wp\/v2\/tags?post=16364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}