Setting own RGB palette on Haiku OS

I tried different ways without results. Here my Pascal code:

procedure SendEscapeSeq(const S: String);
begin
  fpWrite(stdoutputhandle, S[1], Length(S));
end;
 
function HexChar(A:Byte):Char;
begin
  A:=A and 15;
  if A<10 then HexChar:=Char(Ord('0')+A) else HexChar:=Char(Ord('A')+A-10);
end;
 
function Hex2(A:Byte):Str2;
begin
  Hex2:=HexChar(A shr 4)+HexChar(A)
end;
 
const
  { palette slot }
  SL     : VGASlot = (0,1,2,3,4,5,20,7,56,57,58,59,60,61,62,63);
 
  VGA_Default : vga_pal = (
  { red   } (0,0,0,0,42,42,42,42,21,21,21,21,63,63,63,63),
  { green } (0,0,42,42,0,0,21,42,21,21,63,63,21,21,63,63),
  { blue  } (0,42,0,42,0,42,0,42,21,63,21,63,21,63,21,63));
 
procedure Set_palette(Slot:Word; SR,SG,SB:Byte);
var
  B:Byte;
begin
  for B:=0 to 15 do
  if SL[B]=Slot then
  begin
    {$IFDEF HAIKU}
  //SendEscapeSeq(#27']P'+HexChar(B)+Hex2(SR)+Hex2(SG)+Hex2(SB));
  //SendEscapeSeq(#27']1337;SetColors='+HexChar(B)+Hex2(SR)+Hex2(SG)+Hex2(SB)+#26'\');
  //SendEscapeSeq(#27']P4;'+Hex2(B)+';#'+Hex2(SR)+Hex2(SG)+Hex2(SB)+#7);
    {$ENDIF}
    Break
  end;

You can set terminal colors directly using these escape sequences:

ESC[38;2;{r};{g};{b}m Set foreground color as RGB.
ESC[48;2;{r};{g};{b}m Set background color as RGB.

If you want to change the 256 color palette you can use ESC]4;idx:name;BEL to set color number idx to name, where name is one of the X11 colors, or rgb:a,b,c or #123456

I think your last commented line in your comment is almost correct, but you have ‘P4’ instead of ‘4’ at the start.

1 Like

Only black color can be changed (B=00).

SendEscapeSeq(#27']4;'+Hex2(B)+';#'+Hex2(SR*4)+Hex2(SG*4)+Hex2(SB*4)+#7);

What is value for remaining colors?

00 Black
01 Blue
02 Green
03 Cyan
04 Red
05 Magenta
06 Brown
07 Light Gray
08 Dark Gray
09 Light Blue
10=0A Light Green
11=0B Light Cyan
12=0C Light Red
13=0D Light Magenta
14=0E Yellow
15=0F White

Colors 00 to 0F should all be settable, in addition to:

10 - default foreground color
11 - default background color
12 - cursor background color

Thanks. I prefer set by foreground/background color as RGB.