Java Convert RGB (24 bit) to xterm-256 colors. I see many posts on here but none for java. I look at python's code and lots of answers but they all don't do 6x6x6 cubes that tmux does. I don't understand c++ can someone please write tmux
impl in java to convert RGB to xterm-256 color as a byte? And is this problematic in general. Should I be doing this at all? Should I say if the terminal doesn't support RGB to use 4 bit backup colors that people can easily understand.
Asked
Active
Viewed 310 times
-2

jredfox_
- 19
- 6
-
DIfficult to know as you [don't state your goal](https://technojeeves.com/index.php/aliasjava1/15-smart-questions) – g00se Jun 09 '22 at 18:39
-
@g00se "Java Convert RGB (24 bit) to xterm-256 colors." – jredfox_ Jun 09 '22 at 19:25
-
1That's a step, not a goal – g00se Jun 09 '22 at 20:04
-
that is the goal. I have Color and I want it printing on an xterm-256 consoles to. in order to do this I need to convert – jredfox_ Jun 09 '22 at 20:27
-
Let's say you've got the xterm colour - how are you going to do that in Java out of interest? – g00se Jun 09 '22 at 23:23
-
System.out.println works just fine test with TRUE color – jredfox_ Jun 09 '22 at 23:45
-
Not sure what you mean but I'm guessing ANSI escape sequences. Try [this API](https://mabe02.github.io/lanterna/apidocs/3.0/com/googlecode/lanterna/TextColor.Indexed.html) – g00se Jun 10 '22 at 00:03
1 Answers
0
use my Palette class. it can pick the closest color to the palette applicable. it requires a csv file <paletCode, name, R, G, B> and a library to parse a CSV file. from there you can use Palette#pickColor after it's been parsed
package jml.ot.colors;
import java.awt.Color;
import java.io.InputStream;
import java.util.LinkedHashSet;
import jredfox.common.config.csv.CSV;
import jredfox.common.config.csv.CSVE;
public class Palette {
public LinkedHashSet<Entry> entries = new LinkedHashSet<Entry>();
public Palette()
{
}
/**
* return the Palette's clostes color from RGB and then give you the Entry(code, name, rgb)
*/
public Entry pickColor(Color c)
{
int d = 0;
Palette.Entry picked = null;
for(Palette.Entry e : this.entries)
{
int r = e.rgb.getRed();
int g = e.rgb.getGreen();
int b = e.rgb.getBlue();
int distence = Math.abs(r - c.getRed()) + Math.abs(g - c.getGreen()) + Math.abs(b - c.getBlue());
if(picked == null || distence < d)
{
d = distence;
picked = e;
}
}
return picked;
}
public Palette parse(InputStream in) throws Exception
{
CSVE ce = new CSVE();
ce.parse(in);
for(CSV c : ce.list)
this.add(Integer.parseInt(c.list.get(0)), c.list.get(1), new Color(Integer.parseInt(c.list.get(2)), Integer.parseInt(c.list.get(3)), Integer.parseInt(c.list.get(4))));
return this;
}
public void add(int code, String name, Color rgb)
{
this.entries.add(new Entry(code, name, rgb));
}
public void remove(int code)
{
this.entries.remove(new Entry(code, null, null));
}
public class Entry
{
public int code;
public String name;
public Color rgb;
public Entry(int c, String n, Color color)
{
this.code = c;
this.name = n;
this.rgb = color;
}
@Override
public int hashCode()
{
return this.code;
}
@Override
public boolean equals(Object obj)
{
Entry o = (Entry)obj;
return this.code == o.code;
}
@Override
public String toString()
{
return this.code + ", " + this.name + ", " + this.rgb;
}
}
}

jredfox_
- 19
- 6
-
fwiw if the goal is to set xterm colours through Java, I'm guessing that the hardware itself will simply use the nearest xterm-256 colour to any arbitrary 24-bit value given to it if that's all it's capable of – g00se Jun 12 '22 at 15:54