Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color approximation is off due to integer truncation. And other fixes. #5

Open
mathewreny opened this issue Aug 10, 2017 · 4 comments

Comments

@mathewreny
Copy link

I noticed that the color function is not correct. Values close to 255 are truncated, so an entire plane of the 6x6x6 ansi color cube is never hit. Here is part of my update to solve this issue.

    r6 := int(float32(r)/51.0 + 0.5)
    g6 := int(float32(g)/51.0 + 0.5)
    b6 := int(float32(b)/51.0 + 0.5)

Another problem was the 4kilobytes of memory this program consumes for no apparent reason. I solved this by removing the codes.go file and writing color function output like so

if foreground {
    return append([]byte("38;5;"),strconv.Itoa(ansi)...)
} else {
    return append([]byte("48;5;"),strconv.Itoa(ansi)...)
}

Finally, the grayscale function has been reduced to a bounds check and one line of math. This is much faster and allows grayscale values to round to the closest ansi value.

// There are 24 grayscale values between 232 and 255.
return 232 + int(float32(scale-8)/10.0 + 0.5)

Here is a screenshot of the improved color approximation in action.

screen shot 2017-08-10 at 2 49 16 am

May I please have write access so I can submit a pull request?

@aybabtme
Copy link
Owner

aybabtme commented Aug 10, 2017

@mathewreny thanks a lot for this!

You should be able to send a PR without necessitating write access to the repo. If you fork the project and add your fork as a remote, you'll be able to push your changes to a branch on your fork and then open a PR against this repo.

The commands would look like (after having fork'd using the github UI):

# clone this repo
$ git clone https://github.com/aybabtme/rgbterm.git $GOPATH/src/github.com/aybabtme/rgbterm
$ cd $GOPATH/src/github.com/aybabtme/rgbterm

# add your fork as a Git remote
$ git remote add my_fork [email protected]/rgbterm.git

# push your changes to your remote, in a branch
$ git checkout -b branch_name
$ git add . -A
$ git commit -m "Fix to RGB approximation"
$ git push my_for branch_name

Then when you visit https://github.com/aybabtme/rgbterm you'll be offered to open a PR with your fork/branch.

FYI this is usually how it's done for Go projects, so if you're interested in contributing to other projects, it's a good thing to learn the method. 😄

If this is too much trouble or you don't feel like it 😉, feel free to just push your changes to a fork and let me know what commit it is, and I'll pull the change myself.

@mathewreny
Copy link
Author

Thank you for the fork instructions! You caught me, I'm new to this open source thing. I'll open a request once I write some new tests.

@eljobe
Copy link

eljobe commented Sep 17, 2017

@mathewreny, are you still planning to make a pull request for this one? If not, I might take a stab at it.

@TylerBrock
Copy link

TylerBrock commented Jul 15, 2018

@mathewreny would love to have these improvements in the code, are you still planning on making the changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants