Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. unit test;
  2.  
  3. const
  4. SaturationMult = 0.5;
  5.  
  6. var
  7. plugin: IInterface;
  8. lstPatched: TList;
  9.  
  10. //============================================================================
  11. procedure rgb2hsl(red, green, blue: Byte; var h, s, l: Double);
  12. var
  13. r, g, b, cmax, cmin, d, dd: double;
  14. begin
  15. cmin := Min(red, Min(green, blue)) / 255;
  16. cmax := Max(red, Max(green, blue)) / 255;
  17. r := red / 255;
  18. g := green / 255;
  19. b := blue / 255;
  20. h := (cmax + cmin) / 2;
  21. s := (cmax + cmin) / 2;
  22. l := (cmax + cmin) / 2;
  23. if cmax = cmin then begin
  24. h := 0;
  25. s := 0;
  26. end
  27. else begin
  28. d := cmax - cmin;
  29. if l > 0.5 then s := d / (2 - cmax - cmin) else s := d / (cmax + cmin);
  30. if g < b then dd := 6 else dd := 0;
  31. if cmax = r then
  32. h := (g - b) / d + dd
  33. else if cmax = g then
  34. h := (b - r) / d + 2
  35. else if cmax = b then
  36. h := (r - g) / d + 4;
  37. h := h / 6;
  38. end;
  39. end;
  40.  
  41. //============================================================================
  42. function hue2rgb(p, q, t: Double): Double;
  43. begin
  44. if t < 0 then t := t + 1;
  45. if t > 1 then t := t - 1;
  46. if t < 1 / 6 then
  47. Result := p + (q - p) * 6 * t
  48. else if t < 1 / 2 then
  49. Result := q
  50. else if t < 2 / 3 then
  51. Result := p + (q - p) * (2/3 - t) * 6
  52. else
  53. Result := p;
  54. end;
  55.  
  56. //============================================================================
  57. procedure hsl2rgb(h, s, l: Double; var red, green, blue: Byte);
  58. var
  59. r, g, b, p, q: double;
  60. begin
  61. if s = 0 then begin
  62. r := l;
  63. g := l;
  64. b := l;
  65. end
  66. else begin
  67. if l < 0.5 then q := l * (1 + s) else q := l + s - l * s;
  68. p := 2 * l - q;
  69. r := hue2rgb(p, q, h + 1/3);
  70. g := hue2rgb(p, q, h);
  71. b := hue2rgb(p, q, h - 1/3);
  72. end;
  73. red := Round(r * 255);
  74. green := Round(g * 255);
  75. blue := Round(b * 255);
  76. end;
  77.  
  78. //============================================================================
  79. function CheckForErrors(aElement: IInterface): Boolean;
  80. var
  81. Error : string;
  82. i : Integer;
  83. begin
  84. Error := Check(aElement);
  85. Result := Error <> '';
  86. if Result then Exit;
  87.  
  88. for i := ElementCount(aElement) - 1 downto 0 do begin
  89. Result := CheckForErrors(ElementByIndex(aElement, i));
  90. if Result then Exit;
  91. end;
  92. end;
  93.  
  94. //============================================================================
  95. function CopyRecord(e: IInterface): IInterface;
  96. begin
  97. if not Assigned(e) then
  98. Exit;
  99.  
  100. if lstPatched.IndexOf(TObject(GetLoadOrderFormID(e))) <> -1 then
  101. Exit;
  102.  
  103. if CheckForErrors(e) then begin
  104. AddMessage('Skipping ' + Name(e) + ' due to errors');
  105. Exit;
  106. end;
  107.  
  108. if not Assigned(plugin) then
  109. plugin := AddNewFile;
  110.  
  111. if not Assigned(plugin) then
  112. raise Exception.Create('Patch file creation cancelled by user');
  113.  
  114. AddRequiredElementMasters(e, plugin, False);
  115. // copy record as override
  116. Result := wbCopyElementToFile(e, plugin, False, True);
  117.  
  118. lstPatched.Add(TObject(GetLoadOrderFormID(Result)));
  119. end;
  120.  
  121. //============================================================================
  122. procedure PatchColor(e: IInterface);
  123. var
  124. h, s, l: double;
  125. r, g, b: byte;
  126. begin
  127. rgb2hsl(
  128. GetElementNativeValues(e, 'Red'),
  129. GetElementNativeValues(e, 'Green'),
  130. GetElementNativeValues(e, 'Blue'),
  131. h, s, l
  132. );
  133. s := s * SaturationMult;
  134. hsl2rgb(h, s, l, r, g, b);
  135. //AddMessage(IntToStr(r)); AddMessage(IntToStr(g)); AddMessage(IntToStr(b)); Exit;
  136. SetElementNativeValues(e, 'Red', r);
  137. SetElementNativeValues(e, 'Green', g);
  138. SetElementNativeValues(e, 'Blue', b);
  139. end;
  140.  
  141. //============================================================================
  142. procedure PatchIMAD(e: IInterface);
  143. begin
  144. e := CopyRecord(e);
  145. if not Assigned(e) then
  146. Exit;
  147.  
  148. PatchColor(ElementByPath(e, 'TNAM\Data #0'));
  149. end;
  150.  
  151. //============================================================================
  152. procedure PatchIMGS(e: IInterface);
  153. begin
  154. e := CopyRecord(e);
  155. if not Assigned(e) then
  156. Exit;
  157.  
  158. if GetElementNativeValues(e, 'DNAM\Cinematic\Tint\Value') <> 0 then
  159. PatchColor(ElementByPath(e, 'DNAM\Cinematic\Tint\Color'));
  160. end;
  161.  
  162. //============================================================================
  163. procedure PatchWTHR(e: IInterface);
  164. begin
  165. if not Assigned(e) then
  166. Exit;
  167.  
  168. PatchIMAD(LinksTo(ElementBySignature(e, #0'IAD')));
  169. PatchIMAD(LinksTo(ElementBySignature(e, #1'IAD')));
  170. PatchIMAD(LinksTo(ElementBySignature(e, #2'IAD')));
  171. PatchIMAD(LinksTo(ElementBySignature(e, #3'IAD')));
  172. PatchIMAD(LinksTo(ElementBySignature(e, #4'IAD')));
  173. PatchIMAD(LinksTo(ElementBySignature(e, #5'IAD')));
  174. end;
  175.  
  176. //============================================================================
  177. procedure PatchWRLD(e: IInterface);
  178. begin
  179. if not Assigned(e) then
  180. Exit;
  181.  
  182. PatchIMGS(LinksTo(ElementBySignature(e, 'INAM')));
  183. end;
  184.  
  185. //============================================================================
  186. function Initialize: Integer;
  187. begin
  188. lstPatched := TList.Create;
  189. end;
  190.  
  191. //============================================================================
  192. function Process(e: IInterface): integer;
  193. var
  194. sig: string;
  195. begin
  196. sig := Signature(e);
  197. if sig = 'WTHR' then
  198. PatchWTHR(e)
  199. else if sig = 'WRLD' then
  200. PatchWRLD(e);
  201. //else if sig = 'IMAD' then
  202. // PatchIMAD(e);
  203. end;
  204.  
  205. //============================================================================
  206. function Finalize: Integer;
  207. begin
  208. if Assigned(plugin) then
  209. SortMasters(plugin);
  210. lstPatched.Free;
  211. end;
  212.  
  213. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement