You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Colorref.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hwpf.model;
  16. import org.apache.poi.common.Duplicatable;
  17. import org.apache.poi.util.Internal;
  18. import org.apache.poi.util.LittleEndian;
  19. import org.apache.poi.util.Removal;
  20. /**
  21. * 24-bit color structure
  22. */
  23. @Internal
  24. public class Colorref implements Duplicatable {
  25. public static Colorref valueOfIco( int ico )
  26. {
  27. switch ( ico )
  28. {
  29. case 1:
  30. return new Colorref( 0x00000000 );
  31. case 2:
  32. return new Colorref( 0x00FF0000 );
  33. case 3:
  34. return new Colorref( 0x00FFFF00 );
  35. case 4:
  36. return new Colorref( 0x0000FF00 );
  37. case 5:
  38. return new Colorref( 0x00FF00FF );
  39. case 6:
  40. return new Colorref( 0x000000FF );
  41. case 7:
  42. return new Colorref( 0x0000FFFF );
  43. case 8:
  44. return new Colorref( 0x00FFFFFF );
  45. case 9:
  46. return new Colorref( 0x008B0000 );
  47. case 10:
  48. return new Colorref( 0x008B8B00 );
  49. case 11:
  50. return new Colorref( 0x00006400 );
  51. case 12:
  52. return new Colorref( 0x008B008B );
  53. case 13:
  54. return new Colorref( 0x0000008B );
  55. case 14:
  56. return new Colorref( 0x0000CCFF );
  57. case 15:
  58. return new Colorref( 0x00A9A9A9 );
  59. case 16:
  60. return new Colorref( 0x00C0C0C0 );
  61. default:
  62. return new Colorref( 0x00000000 );
  63. }
  64. }
  65. private int value;
  66. public Colorref() {
  67. this.value = -1;
  68. }
  69. public Colorref(Colorref other) {
  70. value = other.value;
  71. }
  72. public Colorref( byte[] data, int offset ) {
  73. this.value = LittleEndian.getInt( data, offset );
  74. }
  75. public Colorref( int value ) {
  76. this.value = value;
  77. }
  78. @Override
  79. @SuppressWarnings("squid:S2975")
  80. @Deprecated
  81. @Removal(version = "5.0.0")
  82. public Colorref clone() {
  83. return copy();
  84. }
  85. @Override
  86. public Colorref copy() {
  87. return new Colorref(this);
  88. }
  89. @Override
  90. public boolean equals( Object obj )
  91. {
  92. if ( this == obj )
  93. return true;
  94. if ( obj == null )
  95. return false;
  96. if ( getClass() != obj.getClass() )
  97. return false;
  98. Colorref other = (Colorref) obj;
  99. if ( value != other.value )
  100. return false;
  101. return true;
  102. }
  103. public int getValue()
  104. {
  105. return value;
  106. }
  107. @Override
  108. public int hashCode()
  109. {
  110. return value;
  111. }
  112. public boolean isEmpty()
  113. {
  114. return value == -1;
  115. }
  116. public void serialize( byte[] data, int offset )
  117. {
  118. LittleEndian.putInt( data, offset, this.value );
  119. }
  120. public void setValue( int value )
  121. {
  122. this.value = value;
  123. }
  124. public byte[] toByteArray()
  125. {
  126. if ( isEmpty() )
  127. throw new IllegalStateException(
  128. "Structure state (EMPTY) is not good for serialization" );
  129. byte[] bs = new byte[4];
  130. serialize( bs, 0 );
  131. return bs;
  132. }
  133. @Override
  134. public String toString()
  135. {
  136. if ( isEmpty() )
  137. return "[COLORREF] EMPTY";
  138. return "[COLORREF] 0x" + Integer.toHexString( value );
  139. }
  140. }