Coder Social home page Coder Social logo

kotlin-kolour's Introduction

Kolour for Kotlin

String class extension functions to help with adding colour (color) to terminal / console output using ANSI escape codes: https://en.wikipedia.org/wiki/ANSI_escape_code. Created for students in my coding classes to use.

Provides functions to specify foreground and background colour using...

  • colour names
  • RGB colour
  • HSV colour
  • Hex colour codes

Installation

Just add Kolour.kt to your project and you should be good to go.

Main.kt includes a colourTest() function that should produce this output, so you can check the compatability of your terminal / IDE:

Colour Test

Usage - Standard Colours (3-Bit)

A limited set of named colours (compatible with most terminals) is included. (Note that the colours shown are representative of my IDE's colour config - your's will likely look different.)

"Hello World!".red()

Red text

"Hello World!".yellow().bgRed()

Yellow text on red

"Hello World!".bold().black().bgYellow()

Bold black text on yellow

A list of the colour functions for standard colours (see notes at end re. bright colours, etc.), both foreground and backgrond:

  • .normal() and .bgNone()
  • .black() and .bgBlack()
  • .white() or .grey() and .bgGrey()
  • .red() and .bgRed()
  • .yellow() and .bgYellow()
  • .green() and .bgGreen()
  • .cyan() and .bgCyan()
  • .blue() and .bgBlue()
  • .magenta() and .bgMagenta()

Additional styling:

  • .bold()
  • .italic()
  • .underline()

Usage - True-Colour (24-Bit)

On termnals that support 24-Bit colour, there are a number of functions that allow you to define colours in various, convenient ways:

RGB Colour

  • .col(r, g, b)
  • .bgCol(r, g, b)

Where r, g and b are Ints in the range 0 to 255

"Hello World!".col(255, 255, 255).bgCol(50, 75, 100)

White text on blue via RGB

HSV Colour

  • .col(hue, saturation, value)
  • .bgCol(hue, saturation, value)

Where hue, saturation and value are Doubles in the range 0.0 to 1.0

"Hello World!".col(0.9, 1.0, 1.0).bgCol(0.9, 1.0, 0.2)

Pink text on pink

Hex Colour

  • .col(hex)
  • .bgCol(hex)

Where hex is a String of the fomat "#rrggbb" or "#rgb"

"Hello World!".col("#ff0").bgCol("#33691e")

Yellow text on green

String.length()

Since the escape sequences used to colour the text add characters to a string, the length of the resulting string is far longer than the visible characters displayed in the terminal.

e.g. "Hello".length is 5, but "Hello".red().bgYellow().length is 23!

So a utility function, length() is provided which will return the length of a string, less any escape sequences

e.g. "Hello".red().bgYellow().length() correctly returns 5, even though the string with its added escape sequences is far longer.

Notes

  • I haven't bothered with the 'bright' colours as they're rendered so inconsistently by different terminal / IDEs, depending on the config: in some cases the colours are different, others not; sometimes the font is made bold; etc. I've never found the bright colours particularly useful. Easy enough to add them in if you need them.

  • I've called the colour which is normally refered to as 'white', 'grey' since it's clearly not white! Likewise for the 'white' background, it's called 'bgGrey'.

kotlin-kolour's People

Contributors

stevecopley avatar

Watchers

 avatar

Forkers

waimea-dt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.