mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-06 03:50:19 +02:00
better implementation for ZLColor class
git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@153 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
parent
882289c560
commit
62dba68578
2 changed files with 60 additions and 130 deletions
|
@ -11,39 +11,37 @@ import org.zlibrary.options.util.*;
|
|||
*
|
||||
*/
|
||||
public final class ZLColorOption extends ZLOption {
|
||||
|
||||
private long myIntValue;
|
||||
private long myDefaultValue;
|
||||
|
||||
public ZLColorOption (String category, String group, String optionName, ZLColor defaultValue){
|
||||
super(category, group, optionName);
|
||||
myDefaultValue = defaultValue.getIntValue();
|
||||
myIntValue = myDefaultValue;
|
||||
}
|
||||
|
||||
public long getValue(){
|
||||
if (!myIsSynchronized){
|
||||
String strDefaultValue = ZLToStringConverter.convert(myDefaultValue);
|
||||
String value = myConfig.getValue(myCategory, myGroup,
|
||||
myOptionName, strDefaultValue);
|
||||
myIntValue = ZLFromStringConverter.getIntegerValue(value);
|
||||
myIsSynchronized = true;
|
||||
}
|
||||
return myIntValue;
|
||||
}
|
||||
|
||||
public void setValue(ZLColor colorValue){
|
||||
long value = colorValue.getIntValue();
|
||||
if (myIsSynchronized && (myIntValue == value)) {
|
||||
return;
|
||||
}
|
||||
myIntValue = value;
|
||||
myIsSynchronized = true;
|
||||
if (myIntValue == myDefaultValue) {
|
||||
myConfig.unsetValue(myCategory, myGroup, myOptionName);
|
||||
} else {
|
||||
String stringValue = ZLToStringConverter.convert(myIntValue);
|
||||
myConfig.setValue(myCategory, myGroup, myOptionName, stringValue);
|
||||
}
|
||||
}
|
||||
public ZLColorOption (String category, String group, String optionName, ZLColor defaultValue){
|
||||
super(category, group, optionName);
|
||||
myDefaultValue = defaultValue.getIntValue();
|
||||
myIntValue = myDefaultValue;
|
||||
}
|
||||
|
||||
public long getValue(){
|
||||
if (!myIsSynchronized){
|
||||
String strDefaultValue = ZLToStringConverter.convert(myDefaultValue);
|
||||
String value = myConfig.getValue(myCategory, myGroup, myOptionName, strDefaultValue);
|
||||
myIntValue = ZLFromStringConverter.getIntegerValue(value);
|
||||
myIsSynchronized = true;
|
||||
}
|
||||
return myIntValue;
|
||||
}
|
||||
|
||||
public void setValue(ZLColor colorValue){
|
||||
long value = colorValue.getIntValue();
|
||||
if (myIsSynchronized && (myIntValue == value)) {
|
||||
return;
|
||||
}
|
||||
myIntValue = value;
|
||||
myIsSynchronized = true;
|
||||
if (myIntValue == myDefaultValue) {
|
||||
myConfig.unsetValue(myCategory, myGroup, myOptionName);
|
||||
} else {
|
||||
String stringValue = ZLToStringConverter.convert(myIntValue);
|
||||
myConfig.setValue(myCategory, myGroup, myOptionName, stringValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,114 +1,46 @@
|
|||
package org.zlibrary.options.util;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
/**
|
||||
* ñóùíîñòü "öâåò". ïðåäñòàâëåíèå çäåñü - ñòàíäàðòíûå RGB êîìïîíåíòû
|
||||
* @author Àäìèíèñòðàòîð
|
||||
*
|
||||
* class Color. Color is presented as the triple of short's (Red, Green, Blue components)
|
||||
* Each component should be in the range 0..255
|
||||
*/
|
||||
public class ZLColor {
|
||||
public final short Red;
|
||||
public final short Green;
|
||||
public final short Blue;
|
||||
|
||||
private int myRed = 0;
|
||||
private int myGreen = 0;
|
||||
private int myBlue = 0;
|
||||
|
||||
/**
|
||||
* è êîíñòðóêòîð ñîîòâåòñòâåííî äëÿ óäîáñòâà
|
||||
* @param color
|
||||
*/
|
||||
public ZLColor(Color color){
|
||||
setColor((int)color.getRed(), (int)color.getGreen(), (int)color.getBlue());
|
||||
}
|
||||
public ZLColor() {
|
||||
Red = 0;
|
||||
Green = 0;
|
||||
Blue = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* êîíñòðóêòîð ïî óìîë÷àíèþ äåëàåò öâåò ÷åðíûì
|
||||
*/
|
||||
public ZLColor(){
|
||||
}
|
||||
|
||||
/**
|
||||
* êîíñòðóêòîð ñ ïàðàìåòðàìè
|
||||
*/
|
||||
public ZLColor(int red, int green, int blue){
|
||||
setColor(red, green, blue);
|
||||
}
|
||||
|
||||
public ZLColor(String color){
|
||||
String[] components = color.split(",");
|
||||
setColor(Integer.parseInt(components[0]), Integer.parseInt(components[1]),
|
||||
Integer.parseInt(components[2]));
|
||||
}
|
||||
|
||||
/**
|
||||
* ïåðåêðûâàåì ìåòîä toString,
|
||||
* ïî ñóòè - êîäèðîâêà â äåñÿòè÷íóþ çàïèñü
|
||||
* ðåçóëüòàò äîëæåí íàãëÿäíî ñîâïàäàòü ñ ðåçóëüòàòîì getIntValue
|
||||
*/
|
||||
public String toString(){
|
||||
return "" + myRed + "," + myGreen + "," + myBlue;
|
||||
public ZLColor(short r, short g, short b) {
|
||||
Red = (short)(r & 0xFF);
|
||||
Green = (short)(g & 0xFF);
|
||||
Blue = (short)(b & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* ÿâëÿåòñÿ ëè ÷èñëî êîððåêòíûì çíà÷åíèåì êîìïîíåíòû ðãá öâåòà.
|
||||
*/
|
||||
private boolean isCorrectComponent(int value){
|
||||
return (value >=0 && value <= 255);
|
||||
public ZLColor(int intValue) {
|
||||
Red = (short)((intValue >> 16) & 0xFF);
|
||||
Green = (short)((intValue >> 8) & 0xFF);
|
||||
Blue = (short)(intValue & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* óñòàíàâëèâàåì òîëüêî òå êîìïîíåíòû, êîòîðûå çàäàíû êîððåêòíî,
|
||||
* òî åñòü ÿâëÿþòñÿ íåîòðèöàòåëüíûì ÷èñëîì < 256
|
||||
*/
|
||||
public void setColor (int red, int green, int blue){
|
||||
if (isCorrectComponent(red)) {
|
||||
myRed = red;
|
||||
}
|
||||
if (isCorrectComponent(green)) {
|
||||
myGreen = green;
|
||||
}
|
||||
if (isCorrectComponent(blue)) {
|
||||
myBlue = blue;
|
||||
}
|
||||
public int getIntValue() {
|
||||
return (Red << 16) + (Green << 8) + Blue;
|
||||
}
|
||||
|
||||
/**
|
||||
* êîíâåðòèðîâàíèå â ÀÂÒøíûé öâåò.
|
||||
* @param color
|
||||
*/
|
||||
//TODO ðåøèòü íóæíî ëè ýòî âîîáùå
|
||||
public void convertFromColor(Color color){
|
||||
myRed = (int)color.getRed();
|
||||
myGreen = (int)color.getGreen();
|
||||
myBlue = (int)color.getBlue();
|
||||
}
|
||||
|
||||
public Color convertToColor(){
|
||||
return new Color(myRed, myGreen, myBlue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return öâåò îäíèì ÷èñëîì, ÷òîáû õðàíèòü â ïàìÿòè ìåíüøå =)
|
||||
*/
|
||||
public long getIntValue(){
|
||||
return myRed*1000000 + myGreen*1000 + myBlue;
|
||||
}
|
||||
|
||||
/**
|
||||
* ïåðåêðûâàåì ìåòîä equals. öâåòà ñ÷èòàåì ðàâíûìè ïðè ðàâåíñòâå âñåõ êîìïîíåíò
|
||||
*/
|
||||
public boolean equals (Object o){
|
||||
if (o == this)
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (o == this) {
|
||||
return true;
|
||||
|
||||
if (! (o.getClass() == this.getClass()))
|
||||
}
|
||||
|
||||
if (!(o instanceof ZLColor)) {
|
||||
return false;
|
||||
|
||||
ZLColor zlc = (ZLColor) o;
|
||||
|
||||
return ((zlc.myRed == this.myRed) &&
|
||||
(zlc.myGreen == this.myGreen) &&
|
||||
(zlc.myBlue == this.myBlue));
|
||||
}
|
||||
|
||||
ZLColor color = (ZLColor)o;
|
||||
return (color.Red == Red) && (color.Green == Green) && (color.Blue == Blue);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue