treeform / chroma Goto Github PK
View Code? Open in Web Editor NEWEverything you want to do with colors, in Nim.
License: MIT License
Everything you want to do with colors, in Nim.
License: MIT License
The Oklab->RGB transform is currently broken: in the last matrix multiplication it uses the LMS components instead of the LMS^3 ones it calculates just above.
Minimal reproducible example:
import std/strutils, chroma
proc `$`(c: ColorRGB): string = "#" & c.r.toHex(2) & c.g.toHex(2) & c.b.toHex(2)
proc `$`(c: ColorOklab): string = $c.asRgb
let c = ColorRGB(r: 0, g: 255, b: 0)
echo c, c.asOklab
Expected output: #00FF00#00FF00
Actual output: #00FF00#8D0A7F
hii!
i'm struggling to use this library for some image processing and palette mapping tasks. I've found that transforming to oklab and back significantly brightens and desaturates colors.
import chroma
echo parseHex("F8DB57").to(ColorOklab).to(ColorRGB).color.toHex()
# output: FCF4BA
Color #F8DB57
turns into #FCF4BA
.
Here's some assorted roundtrips:
import chroma
import random
import strformat
proc chip(c: ColorRGB): string =
## nice-looking color chip for github-flavored markdown
let hex = c.color.toHex()
fmt"`#{hex}` <img valign='middle' alt='blue' src='https://readme-swatches.vercel.app/{hex}'/>"
randomize()
for _ in 1..10:
let color = ColorRGB(
r: random.rand(255).uint8,
g: random.rand(255).uint8,
b: random.rand(255).uint8,
)
let color2 = color.to(ColorOklab).to(ColorRGB)
echo fmt"{color.chip} -> {color2.chip}"
Output:
#FF1580
-> #1A89CC
#B543B5
-> #E9A8E6
#DBCA21
-> #F1EF99
#28B837
-> #9CE9A0
#E254E2
-> #FBB5F8
#5103BC
-> #B06BF8
#3F51B2
-> #A0B0E7
#DB26C5
-> #0196EF
#B44D3E
-> #EAAEA1
#803A22
-> #D09E86
Each pair of colors should be identical (before converting to OkLab and after). But for many of these pairs, even the hue is wildly off.
With the doc updates, the additions of the other color spaces and especially the Nim 1.0 release I think it'd be a good idea to release a new version of chroma, so that nimble install
gives a reasonably new package.
hey, first of all I appreciate your work for Nim community. you made dozens of useful libraries.
i have a sample image named sample.png
: [the background is transparent]
here's my code:
import pixie, chroma
var img = readImage("./sample.png")
func reverse(clr: Color, reverseAlpha = false): Color=
Color(
r: 255 - clr.r,
g: 255 - clr.g,
b: 255 - clr.b,
a: if reverseAlpha: 1 - clr.a else: clr.a)
for i in 0..<img.data.len:
template clr: untyped = img.data[i]
# clr = clr.asColor.spin(0).asRgbx # 1
# clr = clr.asColor.reverse.asRgbx # 2
# clr = clr.asColor.reverse.spin(0).asRgbx # 3
img.writeFile("./res.png")
if I comment out the first commented line [#1
] I get the same image: [which is obviously correct - it spins the colors 0 degrees]
however, if i comment out [#3
] i get this: [which is strange]:
I'm really not sure if it's a bug or not, please tell me if I did anything wrong
Hi there Treeform,
You probably know about this one already, but in case you don't: https://bottosson.github.io/posts/oklab/
I'm using this package in clr and would appreciate having a tagged release I could specify in my .nimble
file (preferably following Semantic Versioning). I'm currently pinning a commit hash there, but it's better to rely on commits which are known to be stable and compatible ๐
Support for grey / grayscale uint8, 16, 32, float 32 [0, 1] & alpha
transformations, conversion to: greyscale
equal weight 1/3 * r + 1/3 * g + 1/3 * b
ITU-R BT.709 (HDTV) 0.21 * r + 0.72 * g + 0.07 * b
ITU-R BT.601 (PAL) 0.299 * r + 0.587 * g + 0.114 * b
user defined weight
ITU-R BT.709 (HDTV) as default(?)
convenience operator:
let value = color(0.5,0.5,0.1).gray
in the hope it will trickle into Pixie
This is the root cause of #41.
Running nimble install chroma
gets commit b2e7117, which is two years old.
$ nimble install chroma
Downloading https://github.com/treeform/chroma using git
Verifying dependencies for [email protected]
Installing [email protected]
Success: chroma installed successfully.
$ cat ~/.nimble/pkgs2/chroma-0.2.7-f873f4b97e87a82e27e0b85bf9130fe3e320a2b8/nimblemeta.json
{
"version": 1,
"metaData": {
"url": "https://github.com/treeform/chroma",
"downloadMethod": "git",
"vcsRevision": "b2e71179174e040884ebf6a16cbac711c84620b9",
"files": [
"/chroma/distance.nim",
"/chroma/csstint.nim",
"/chroma/transformations.nim",
"/chroma/colortypes.nim",
"/chroma.nim",
"/chroma/blends.nim",
"/chroma/temperature.nim",
"/chroma.nimble",
"/chroma/names.nim"
],
"binaries": [],
"specialVersions": [
"0.2.7"
]
}
}
Title says it all. When running nim js bars.nim
I get the following error:
/Users/jamen/.nimble/pkgs/chroma-0.1.1/chroma/distance.nim(10, 32)
/usr/local/Cellar/nim/1.2.6/nim/lib/pure/math.nim(1014, 16) ^
/usr/local/Cellar/nim/1.2.6/nim/lib/system/arithmetics.nim(499, 9) *=
/usr/local/Cellar/nim/1.2.6/nim/lib/system/arithmetics.nim(499, 9) Error: unhandled exception: value out of range
I confirmed that this error does not occur on chroma-0.1.0 (there is no distance.nim
). This seems to be specific to macOS since Nim Playground doesn't reproduce this issue.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.