HSVColor.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package com.mxgraph.io.vsdx.theme;
  2. public class HSVColor {
  3. private double h, s, v;
  4. public HSVColor(double h, double s, double v) {
  5. this.h = h;
  6. this.s = s;
  7. this.v = v;
  8. }
  9. public Color toRgb()
  10. {
  11. double h = this.h * 6;
  12. double s = this.s;
  13. double l = this.v;
  14. double i = Math.floor(h);
  15. double f = h - i, p = v * (1 - s);
  16. double q = v * (1 - f * s), t = v * (1 - (1 - f) * s);
  17. int mod = (int)i % 6;
  18. double[] rArr = {v, q, p, p, t, v};
  19. double[] gArr = {t, v, v, q, p, p};
  20. double[] bArr = {p, p, t, v, v, q};
  21. double r = rArr[mod], g = gArr[mod], b = bArr[mod];
  22. return new Color((int) (r * 255), (int) (g * 255), (int) (b * 255));
  23. }
  24. // Force a number between 0 and 1
  25. private double clamp01(double val)
  26. {
  27. return Math.min(1, Math.max(0, val));
  28. }
  29. //lighten or tint
  30. public HSVColor tint (int amount)
  31. {
  32. this.v *= (1 + (amount / 100.0));
  33. this.v = clamp01(this.v);
  34. return this;
  35. }
  36. //darken or shade
  37. public HSVColor shade(int amount)
  38. {
  39. this.v *= amount / 100.0;
  40. this.v = clamp01(this.v);
  41. return this;
  42. }
  43. public HSVColor satMod(int amount)
  44. {
  45. this.s *= amount / 100.0;
  46. this.s = clamp01(this.s);
  47. return this;
  48. }
  49. public HSVColor lumMod(int amount)
  50. {
  51. this.v *= amount / 100.0;
  52. this.v = clamp01(this.v);
  53. return this;
  54. }
  55. }