Добавлен Clamp,
опубликован
Ещё один найденный на просторах моего жёсткого диска код. Структура, которая позволяет в одном integer хранить все сведения о цвете.
Сурс в тексте:
раскрыть
#guard yln_color_j
// Use with ints in '0x00' format only.
//
// hex: 0 x 00 00 00 00
// contains: aa rr gg bb
struct Color {
private int value = 0;
public bool operator == (Color other) { return this.value == other.value; }
public int operator a () { return this.value/0x1000000; }
public int operator r () { return this.value/0x10000 - this.value/0x1000000*0x100; }
public int operator g () { return this.value/0x100 - this.value/0x10000*0x100; }
public int operator b () { return this.value - this.value/0x100*0x100; }
public void operator a= (int new) { this.value = this.value - this.a*0x1000000 + new*0x1000000; }
public void operator r= (int new) { this.value = this.value - this.r*0x10000 + new*0x10000; }
public void operator g= (int new) { this.value = this.value - this.g*0x100 + new*0x100; }
public void operator b= (int new) { this.value = this.value - this.b + new; }
public int operator alpha () { return this.a; }
public int operator red () { return this.r; }
public int operator green () { return this.g; }
public int operator blue () { return this.b; }
public void operator alpha= (int new) { this.a = new; }
public void operator red= (int new) { this.r = new; }
public void operator green= (int new) { this.g = new; }
public void operator blue= (int new) { this.b = new; }
// Constructors:
public static Color clone(Color color) {
return Color.create().copy(color);
}
public static Color createFromInt(int num) {
return Color.create().fromInt(num);
}
public static Color createFromColors(int a, int r, int g, int b) {
return Color.create().fromColors(a, r, g, b);
}
public static Color createFromString(string color) {
return Color.create().fromString(color);
}
// Input:
public Color copy(Color other) {
this.value = other.value;
return this;
}
public Color fromInt(int num) {
this.value = num;
return this;
}
public Color fromColors(int a, int r, int g, int b) {
this.value = cutIntToHex(a)*0x1000000 + \
cutIntToHex(r)*0x10000 + \
cutIntToHex(g)*0x100 + \
cutIntToHex(b);
return this;
}
public Color fromString(string colors) {
this.value = cutIntToHex(S2I(SubString(colors, 0, 2)))*0x1000000 + \
cutIntToHex(S2I(SubString(colors, 2, 4)))*0x10000 + \
cutIntToHex(S2I(SubString(colors, 4, 6)))*0x100 + \
cutIntToHex(S2I(SubString(colors, 6, 8)));
return this;
}
// Misc:
public int toInt() {
return this.value;
}
public string toString() {
return I2S(this.alpha) + I2S(this.red) + I2S(this.green) + I2S(this.blue);
}
public string toStringFormat(string text) {
return "|c" + this.toString() + text + "|r";
}
private int cutIntToHex(int num) {
if (0x00 > num) { return 0x00; }
if (0xff < num) { return 0xff; }
return num;
}
}
`
ОЖИДАНИЕ РЕКЛАМЫ...
Комментарии пока отсутcтвуют.
Чтобы оставить комментарий, пожалуйста, войдите на сайт.