Wednesday, July 20, 2022

[FIXED] How to edit the values of hexadecimal integer in Java?

Issue

I'm trying to edit values in a hexadecimal in Java.

EDIT: By the way, the hexadecimal contains not just RGB but also alpha.

For example:

int j = 0xAARRGGBB

How would I change the value of AA or RR after the integer has been created?

I couldn't find this question answered anywhere on Google.

Thank you so much for the help!


Solution

Here is all you need I guess to do that

public static int toARGB(int a,int r,int g,int b){
  return ((((((a << 8) | r) << 8) | g) << 8) | b);
}

public static int setB(int origin,int value){
  return set(origin,0,value);
}
public static int setG(int origin,int value){
  return set(origin,1,value);
}
public static int setR(int origin,int value){
  return set(origin,2,value);
}

public static int setA(int origin,int value){
  return set(origin,3,value);
}

public static int set(int origin,int pos,int value){
  return (origin & ~(0xFF << (pos * 8)) | value << (8 * pos));
}


Answered By - Julian Kreuzer
Answer Checked By - Terry (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.