Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2017 Richard Hull and contributors | ||
# See LICENSE.rst for details. | ||
|
||
# From a comment by @TerrorBite on https://gist.github.com/MicahElliott/719710 | ||
|
||
# Default color levels for the color cube | ||
cubelevels = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff] | ||
|
||
# Generate a list of midpoints of the above list | ||
snaps = [(x + y) / 2 for x, y in zip(cubelevels, [0] + cubelevels)[1:]] | ||
|
||
|
||
def rgb2short(r, g, b): | ||
""" | ||
Converts RGB values to the nearest equivalent xterm-256 color. | ||
""" | ||
# Using list of snap points, convert RGB value to cube indexes | ||
r, g, b = map(lambda x: len(tuple(s for s in snaps if s < x)), (r, g, b)) | ||
|
||
# Simple colorcube transform | ||
return (r * 36) + (g * 6) + b + 16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters