Advertisement
Guest User

Untitled

a guest
Mar 9th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.37 KB | None | 0 0
  1. // ***********************************************************************
  2. // Author : ElektroStudios
  3. // Modified : 15-December-2016
  4. // ***********************************************************************
  5.  
  6. #region Public Members Summary
  7.  
  8. #region Constructors
  9.  
  10. // New(Color)
  11. // New(Pen)
  12. // New(SolidBrush)
  13.  
  14. #endregion
  15.  
  16. #region Properties
  17.  
  18. // Color As Color
  19. // ARGB As String
  20. // RGB As String
  21. // Hex As String
  22. // Web As String
  23. // Hsl As String
  24. // Win32 As String
  25. // Delphi As String
  26. // CSharp(CSharpColorFormat) As String
  27. // VisualBasic(VisualBasicColorFormat) As String
  28. // VisualStudio(VisualStudioColorFormat) As String
  29.  
  30. #endregion
  31.  
  32. #endregion
  33.  
  34. #region Option Statements
  35.  
  36. Option Strict On;
  37. Option Explicit On;
  38. Option Infer Off;
  39.  
  40. #endregion
  41.  
  42. #region Imports
  43.  
  44. Imports System.ComponentModel;
  45. Imports System.Drawing;
  46. Imports System.Globalization;
  47. Imports System.Runtime.Serialization;
  48. Imports System.Security.Permissions;
  49. Imports System.Xml;
  50. Imports System.Xml.Serialization;
  51. #if ! NETCOREAPP
  52. Imports DevCase.ProjectMigration;
  53. #else
  54. Imports System.Runtime.Versioning;
  55. #endif
  56. #endregion
  57.  
  58. #region Color-String
  59.  
  60. // ReSharper disable once CheckNamespace
  61.  
  62. Namespace DevCase.Core.Media.Graphics;
  63.  
  64. /// ----------------------------------------------------------------------------------------------------
  65. /// <summary>
  66. /// Defines a <see cref="System.Drawing.Color"/> with an unique string-format representation in the specified string-syntax.
  67. /// </summary>
  68. /// ----------------------------------------------------------------------------------------------------
  69. /// <example> This is a code example.
  70. /// <code language="VB.NET">
  71. /// Dim colorString As New ColorString(color.FromArgb(255, 91, 146, 198))
  72. ///
  73. /// Console.WriteLine(String.Format("ColorString Structure Size: {0}", Marshal.SizeOf(GetType(ColorString)).ToString()))
  74. /// Console.WriteLine(String.Format("Color.Tostring() : {0}", colorString.Color.ToString()))
  75. /// Console.WriteLine(String.Format("ColorString.Tostring() : {0}", colorString.ToString()))
  76. /// Console.WriteLine()
  77. /// Console.WriteLine(String.Format("ARGB: {0}", colorString.Argb))
  78. /// Console.WriteLine(String.Format("RGB : {0}", colorString.Rgb))
  79. /// Console.WriteLine()
  80. /// Console.WriteLine(String.Format("Hex : {0}", colorString.Hex))
  81. /// Console.WriteLine(String.Format("Web : {0}", colorString.Web))
  82. /// Console.WriteLine(String.Format("HSL : {0}", colorString.Hsl))
  83. /// Console.WriteLine(String.Format("Win32: {0}", colorString.Win32))
  84. /// Console.WriteLine()
  85. /// Console.WriteLine(String.Format("Delphi: {0}", colorString.Delphi))
  86. /// Console.WriteLine()
  87. /// Console.WriteLine(String.Format("C# (Int): {0}", colorString.CSharp(CSharpColorFormat.Int)))
  88. /// Console.WriteLine(String.Format("C# (Hex): {0}", colorString.CSharp(CSharpColorFormat.Hex)))
  89. /// Console.WriteLine(String.Format("C# (Web): {0}", colorString.CSharp(CSharpColorFormat.Html)))
  90. /// Console.WriteLine()
  91. /// Console.WriteLine(String.Format("Vb.Net (Int): {0}", colorString.VisualBasic(VisualBasicColorFormat.Int)))
  92. /// Console.WriteLine(String.Format("Vb.Net (Hex): {0}", colorString.VisualBasic(VisualBasicColorFormat.Hex)))
  93. /// Console.WriteLine(String.Format("Vb.Net (Web): {0}", colorString.VisualBasic(VisualBasicColorFormat.Html)))
  94. /// Console.WriteLine()
  95. /// Console.WriteLine(String.Format("Visual Studio (Int): {0}", colorString.VisualStudio(VisualStudioColorFormat.Int)))
  96. /// Console.WriteLine(String.Format("Visual Studio (Hex): {0}", colorString.VisualStudio(VisualStudioColorFormat.Hex)))
  97. /// </code>
  98. /// </example>
  99. /// ----------------------------------------------------------------------------------------------------
  100. [Serializable]
  101. [XmlRoot(nameof(ColorString))]
  102. [System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0079:Remove unnecessary suppression", Justification="Required to migrate this code to .NET Core")]
  103. Public NotInheritable Class ColorString : Implements ISerializable, IXmlSerializable;
  104.  
  105. #region Properties
  106.  
  107. /// ----------------------------------------------------------------------------------------------------
  108. /// <summary>
  109. /// Gets the <see cref="System.Drawing.Color"/>.
  110. /// </summary>
  111. /// ----------------------------------------------------------------------------------------------------
  112. /// <value>
  113. /// The <see cref="System.Drawing.Color"/>.
  114. /// </value>
  115. /// ----------------------------------------------------------------------------------------------------
  116. Public ReadOnly Property Color As Color;
  117. [DebuggerStepThrough]
  118. get
  119. {
  120. return this.colorB;
  121. }
  122. }
  123. /// ----------------------------------------------------------------------------------------------------
  124. /// <summary>
  125. /// ( Backing field )
  126. /// The <see cref="System.Drawing.Color"/>.
  127. /// </summary>
  128. /// ----------------------------------------------------------------------------------------------------
  129. private Color colorB;
  130.  
  131. /// ----------------------------------------------------------------------------------------------------
  132. /// <summary>
  133. /// Gets the ARGB color representation.
  134. /// </summary>
  135. /// ----------------------------------------------------------------------------------------------------
  136. /// <value>
  137. /// The ARGB color representation.
  138. /// </value>
  139. /// ----------------------------------------------------------------------------------------------------
  140. public string Argb
  141. {
  142. [DebuggerStepThrough]
  143. get
  144. {
  145. return $"{Convert.ToString(this.colorB.A)}, {Convert.ToString(this.colorB.R)}, {Convert.ToString(this.colorB.G)}, {Convert.ToString(this.colorB.B)}";
  146. }
  147. }
  148.  
  149. /// ----------------------------------------------------------------------------------------------------
  150. /// <summary>
  151. /// Gets the RGB color representation.
  152. /// </summary>
  153. /// ----------------------------------------------------------------------------------------------------
  154. /// <value>
  155. /// The RGB color representation.
  156. /// </value>
  157. /// ----------------------------------------------------------------------------------------------------
  158. public string Rgb
  159. {
  160. [DebuggerStepThrough]
  161. get
  162. {
  163. return $"{Convert.ToString(this.colorB.R)}, {Convert.ToString(this.colorB.G)}, {Convert.ToString(this.colorB.B)}";
  164. }
  165. }
  166.  
  167. /// ----------------------------------------------------------------------------------------------------
  168. /// <summary>
  169. /// Gets the HSL color representation.
  170. /// </summary>
  171. /// ----------------------------------------------------------------------------------------------------
  172. /// <value>
  173. /// The HSL color representation.
  174. /// </value>
  175. /// ----------------------------------------------------------------------------------------------------
  176. public string Hsl
  177. {
  178. [DebuggerStepThrough]
  179. get
  180. {
  181. return $@"{this.colorB.GetHue():n0}, {_
  182. (this.colorB.GetSaturation() * 100).ToString("n1", CultureInfo.GetCultureInfo("en-US@").NumberFormat) _
  183. }%, {_
  184. (this.colorB.GetBrightness() * 100).ToString("n1", CultureInfo.GetCultureInfo("en-US@").NumberFormat) _
  185. }%";
  186. }
  187. }
  188.  
  189. /// ----------------------------------------------------------------------------------------------------
  190. /// <summary>
  191. /// Gets the Hexadecimal color representation.
  192. /// </summary>
  193. /// ----------------------------------------------------------------------------------------------------
  194. /// <value>
  195. /// The Hexadecimal color representation.
  196. /// </value>
  197. /// ----------------------------------------------------------------------------------------------------
  198. public string Hex
  199. {
  200. [DebuggerStepThrough]
  201. get
  202. {
  203. string a = Convert.ToString(this.colorB.A, 16).ToUpper();
  204. string r = Convert.ToString(this.colorB.R, 16).ToUpper();
  205. string g = Convert.ToString(this.colorB.G, 16).ToUpper();
  206. string b = Convert.ToString(this.colorB.B, 16).ToUpper();
  207. return $"{a}{r}{g}{b}";
  208. }
  209. }
  210.  
  211. /// ----------------------------------------------------------------------------------------------------
  212. /// <summary>
  213. /// Gets the Web color representation.
  214. /// </summary>
  215. /// ----------------------------------------------------------------------------------------------------
  216. /// <value>
  217. /// The Web color representation.
  218. /// </value>
  219. /// ----------------------------------------------------------------------------------------------------
  220. public string Web
  221. {
  222. [DebuggerStepThrough]
  223. get
  224. {
  225. return ColorTranslator.ToHtml(this.Color);
  226. }
  227. }
  228.  
  229. /// ----------------------------------------------------------------------------------------------------
  230. /// <summary>
  231. /// Gets the Win32 color representation.
  232. /// </summary>
  233. /// ----------------------------------------------------------------------------------------------------
  234. /// <value>
  235. /// The Win32 color representation.
  236. /// </value>
  237. /// ----------------------------------------------------------------------------------------------------
  238. public string Win32
  239. {
  240. [DebuggerStepThrough]
  241. get
  242. {
  243. // Return CStr(ColorExtensions.ToWin32(Me.colorB))
  244. return ColorTranslator.ToWin32(this.colorB).ToString();
  245. }
  246. }
  247.  
  248. /// ----------------------------------------------------------------------------------------------------
  249. /// <summary>
  250. /// Gets the Delphi color representation.
  251. /// </summary>
  252. /// ----------------------------------------------------------------------------------------------------
  253. /// <value>
  254. /// The Delphi color representation.
  255. /// </value>
  256. /// ----------------------------------------------------------------------------------------------------
  257. public string Delphi
  258. {
  259. [DebuggerStepThrough]
  260. get
  261. {
  262. string a = Convert.ToString(this.colorB.A, 16).ToUpper();
  263. string r = Convert.ToString(this.colorB.R, 16).ToUpper();
  264. string g = Convert.ToString(this.colorB.G, 16).ToUpper();
  265. string b = Convert.ToString(this.colorB.B, 16).ToUpper();
  266. return $"$00{a}{r}{g}{b}";
  267. }
  268. }
  269.  
  270. /// ----------------------------------------------------------------------------------------------------
  271. /// <summary>
  272. /// Gets the C# color representation.
  273. /// </summary>
  274. /// ----------------------------------------------------------------------------------------------------
  275. /// <param name="format">
  276. /// The color format.
  277. /// </param>
  278. /// ----------------------------------------------------------------------------------------------------
  279. /// <value>
  280. /// The C# color representation.
  281. /// </value>
  282. /// ----------------------------------------------------------------------------------------------------
  283. // INSTANT C# NOTE: C# does not support parameterized properties - the following property has been rewritten as a function:
  284. // ORIGINAL LINE: Public ReadOnly Property CSharp(format As CSharpColorFormat) As String
  285. public string get_CSharp(CSharpColorFormat format)
  286. {
  287. <DebuggerStepThrough> Get
  288. return this.GetCSharpString(format)
  289. End Get
  290. }
  291.  
  292. /// ----------------------------------------------------------------------------------------------------
  293. /// <summary>
  294. /// Gets the VisualBasic color representation.
  295. /// </summary>
  296. /// ----------------------------------------------------------------------------------------------------
  297. /// <param name="format">
  298. /// The color format.
  299. /// </param>
  300. /// ----------------------------------------------------------------------------------------------------
  301. /// <value>
  302. /// The VisualBasic color representation.
  303. /// </value>
  304. /// ----------------------------------------------------------------------------------------------------
  305. // INSTANT C# NOTE: C# does not support parameterized properties - the following property has been rewritten as a function:
  306. // ORIGINAL LINE: Public ReadOnly Property VisualBasic(format As VisualBasicColorFormat) As String
  307. public string get_VisualBasic(VisualBasicColorFormat format)
  308. {
  309. <DebuggerStepThrough> Get
  310. return this.GetVisualBasicString(format)
  311. End Get
  312. }
  313.  
  314. /// ----------------------------------------------------------------------------------------------------
  315. /// <summary>
  316. /// Gets the VisualStudio color representation.
  317. /// </summary>
  318. /// ----------------------------------------------------------------------------------------------------
  319. /// <param name="format">
  320. /// The color format.
  321. /// </param>
  322. /// ----------------------------------------------------------------------------------------------------
  323. /// <value>
  324. /// The VisualStudio color representation.
  325. /// </value>
  326. /// ----------------------------------------------------------------------------------------------------
  327. // INSTANT C# NOTE: C# does not support parameterized properties - the following property has been rewritten as a function:
  328. // ORIGINAL LINE: Public ReadOnly Property VisualStudio(format As VisualStudioColorFormat) As String
  329. public string get_VisualStudio(VisualStudioColorFormat format)
  330. {
  331. <DebuggerStepThrough> Get
  332. return this.GetVisualStudioString(format)
  333. End Get
  334. }
  335.  
  336. #endregion
  337.  
  338. #region Constructors
  339.  
  340. /// ----------------------------------------------------------------------------------------------------
  341. /// <summary>
  342. /// Prevents a default instance of the <see cref="ColorString"/> class from being created.
  343. /// </summary>
  344. /// ----------------------------------------------------------------------------------------------------
  345. // INSTANT C# TASK: This method is a constructor, but no class name was found:
  346. private <class name>()
  347. {
  348. }
  349.  
  350. /// ----------------------------------------------------------------------------------------------------
  351. /// <summary>
  352. /// Initializes a new instance of the <see cref="ColorString"/> class.
  353. /// </summary>
  354. /// ----------------------------------------------------------------------------------------------------
  355. /// <param name="color">
  356. /// The source <see cref="System.Drawing.Color"/>.
  357. /// </param>
  358. /// ----------------------------------------------------------------------------------------------------
  359. // INSTANT C# TASK: This method is a constructor, but no class name was found:
  360. [DebuggerStepThrough]
  361. public <class name>(Color color)
  362. {
  363.  
  364. this.colorB = color;
  365.  
  366. }
  367.  
  368. /// ----------------------------------------------------------------------------------------------------
  369. /// <summary>
  370. /// Initializes a new instance of the <see cref="ColorString"/> class.
  371. /// </summary>
  372. /// ----------------------------------------------------------------------------------------------------
  373. /// <param name="pen">
  374. /// The source <see cref="Pen"/>.
  375. /// </param>
  376. /// ----------------------------------------------------------------------------------------------------
  377. // INSTANT C# TASK: This method is a constructor, but no class name was found:
  378. [DebuggerStepThrough]
  379. [SupportedOSPlatform("windows")]
  380. public <class name>(Pen pen) : this(pen.Color)
  381. {
  382.  
  383.  
  384. }
  385.  
  386. /// ----------------------------------------------------------------------------------------------------
  387. /// <summary>
  388. /// Initializes a new instance of the <see cref="ColorString"/> class.
  389. /// </summary>
  390. /// ----------------------------------------------------------------------------------------------------
  391. /// <param name="brush">
  392. /// The source <see cref="SolidBrush"/>.
  393. /// </param>
  394. /// ----------------------------------------------------------------------------------------------------
  395. // INSTANT C# TASK: This method is a constructor, but no class name was found:
  396. [DebuggerStepThrough]
  397. [SupportedOSPlatform("windows")]
  398. public <class name>(SolidBrush brush) : this(brush.Color)
  399. {
  400.  
  401.  
  402. }
  403.  
  404. #endregion
  405.  
  406. #region Operator Conversions
  407.  
  408. /// ----------------------------------------------------------------------------------------------------
  409. /// <summary>
  410. /// Performs an implicit conversion from <see cref="ColorString"/> to <see cref="System.Drawing.Color"/>.
  411. /// </summary>
  412. /// ----------------------------------------------------------------------------------------------------
  413. /// <param name="colorString">
  414. /// The <see cref="ColorString"/>.
  415. /// </param>
  416. /// ----------------------------------------------------------------------------------------------------
  417. /// <returns>
  418. /// The resulting <see cref="System.Drawing.Color"/> of the conversion.
  419. /// </returns>
  420. /// ----------------------------------------------------------------------------------------------------
  421. public static implicit operator Color(ColorString colorString)
  422. {
  423.  
  424. return System.Drawing.Color.FromArgb(colorString.Color.R, colorString.Color.G, colorString.Color.B);
  425.  
  426. }
  427.  
  428. /// ----------------------------------------------------------------------------------------------------
  429. /// <summary>
  430. /// Performs an implicit conversion from <see cref="System.Drawing.Color"/> to <see cref="ColorString"/>.
  431. /// </summary>
  432. /// ----------------------------------------------------------------------------------------------------
  433. /// <param name="color">
  434. /// The <see cref="System.Drawing.Color"/>.
  435. /// </param>
  436. /// ----------------------------------------------------------------------------------------------------
  437. /// <returns>
  438. /// The resulting <see cref="ColorString"/> of the conversion.
  439. /// </returns>
  440. /// ----------------------------------------------------------------------------------------------------
  441. public static explicit operator ColorString(Color color)
  442. {
  443.  
  444. return new ColorString(color);
  445.  
  446. }
  447.  
  448. /// ----------------------------------------------------------------------------------------------------
  449. /// <summary>
  450. /// Implements the operator =.
  451. /// </summary>
  452. /// ----------------------------------------------------------------------------------------------------
  453. /// <param name="colorString1">
  454. /// The first <see cref="ColorString"/> to evaluate.
  455. /// </param>
  456. ///
  457. /// <param name="colorString2">
  458. /// The second <see cref="ColorString"/> to evaluate.
  459. /// </param>
  460. /// ----------------------------------------------------------------------------------------------------
  461. /// <returns>
  462. /// The result of the operator.
  463. /// </returns>
  464. /// ----------------------------------------------------------------------------------------------------
  465. public static bool operator == (ColorString colorString1, ColorString colorString2)
  466. {
  467.  
  468. return colorString1.Equals(colorString2);
  469.  
  470. }
  471.  
  472. /// ----------------------------------------------------------------------------------------------------
  473. /// <summary>
  474. /// Implements the operator &lt;&gt;.
  475. /// </summary>
  476. /// ----------------------------------------------------------------------------------------------------
  477. /// <param name="colorString1">
  478. /// The first <see cref="ColorString"/> to evaluate.
  479. /// </param>
  480. ///
  481. /// <param name="colorString2">
  482. /// The second <see cref="ColorString"/> to evaluate.
  483. /// </param>
  484. /// ----------------------------------------------------------------------------------------------------
  485. /// <returns>
  486. /// The result of the operator.
  487. /// </returns>
  488. /// ----------------------------------------------------------------------------------------------------
  489. public static bool operator != (ColorString colorString1, ColorString colorString2)
  490. {
  491.  
  492. return !colorString1.Equals(colorString2);
  493.  
  494. }
  495.  
  496. /// ----------------------------------------------------------------------------------------------------
  497. /// <summary>
  498. /// Determines whether the specified <see cref="Object"/> is equal to this instance.
  499. /// </summary>
  500. /// ----------------------------------------------------------------------------------------------------
  501. /// <param name="obj">
  502. /// Another object to compare to.
  503. /// </param>
  504. /// ----------------------------------------------------------------------------------------------------
  505. /// <returns>
  506. /// <see langword="True"/> if the specified <see cref="Object"/> is equal to this instance; otherwise, <see langword="False"/>.
  507. /// </returns>
  508. /// ----------------------------------------------------------------------------------------------------
  509. public override bool Equals(object obj)
  510. {
  511.  
  512. if (obj is ColorString)
  513. {
  514. return this.Equals((ColorString)obj);
  515.  
  516. }
  517. else if (obj is Color)
  518. {
  519. return this.Equals(new ColorString((Color)obj));
  520.  
  521. }
  522. else
  523. {
  524. return false;
  525.  
  526. }
  527.  
  528. }
  529.  
  530. /// ----------------------------------------------------------------------------------------------------
  531. /// <summary>
  532. /// Determines whether the specified <see cref="ColorString"/> is equal to this instance.
  533. /// </summary>
  534. /// ----------------------------------------------------------------------------------------------------
  535. /// <param name="colorString">
  536. /// Another <see cref="ColorString"/> to compare to.
  537. /// </param>
  538. /// ----------------------------------------------------------------------------------------------------
  539. /// <returns>
  540. /// <see langword="True"/> if the specified <see cref="ColorString"/> is equal to this instance; otherwise, <see langword="False"/>.
  541. /// </returns>
  542. /// ----------------------------------------------------------------------------------------------------
  543. new public bool Equals(ColorString colorString)
  544. {
  545.  
  546. return colorString.Color.ToArgb == this.colorB.ToArgb;
  547.  
  548. }
  549.  
  550. /// ----------------------------------------------------------------------------------------------------
  551. /// <summary>
  552. /// Returns a hash code for this instance.
  553. /// </summary>
  554. /// ----------------------------------------------------------------------------------------------------
  555. /// <returns>
  556. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  557. /// </returns>
  558. /// ----------------------------------------------------------------------------------------------------
  559. public override int GetHashCode()
  560. {
  561.  
  562. return this.colorB.GetHashCode();
  563.  
  564. }
  565.  
  566. /// ----------------------------------------------------------------------------------------------------
  567. /// <summary>
  568. /// Returns a <see cref="String"/> that represents this instance.
  569. /// </summary>
  570. /// ----------------------------------------------------------------------------------------------------
  571. /// <returns>
  572. /// A <see cref="String"/> that represents this instance.
  573. /// </returns>
  574. /// ----------------------------------------------------------------------------------------------------
  575. [EditorBrowsable(EditorBrowsableState.Always)]
  576. [DebuggerNonUserCode]
  577. public override string ToString()
  578. {
  579.  
  580. return string.Format(CultureInfo.CurrentCulture, "{{A={0}, R={1}, G={2}, B={3}}}", this.colorB.A, this.colorB.R, this.colorB.G, this.colorB.B);
  581.  
  582. }
  583.  
  584. #endregion
  585.  
  586. #region ISerializable implementation // For Binary serialization.
  587.  
  588. #pragma warning disable SYSLIB0003, SYSLIB0050 // Type or member is obsolete
  589. /// ----------------------------------------------------------------------------------------------------
  590. /// <summary>
  591. /// Populates a <see cref="SerializationInfo"/> with the data needed to serialize the target object.
  592. /// </summary>
  593. /// ----------------------------------------------------------------------------------------------------
  594. /// <param name="info">
  595. /// The <see cref="SerializationInfo"/> to populate with data.
  596. /// </param>
  597. ///
  598. /// <param name="context">
  599. /// The destination (see <see cref="StreamingContext"/>) for this serialization.
  600. /// </param>
  601. /// ----------------------------------------------------------------------------------------------------
  602. /// <exception cref="ArgumentNullException">
  603. /// info
  604. /// </exception>
  605. /// ----------------------------------------------------------------------------------------------------
  606. [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
  607. [DebuggerStepThrough]
  608. [EditorBrowsable(EditorBrowsableState.Never)]
  609. private void GetObjectData(SerializationInfo info, StreamingContext context)
  610. {
  611. #pragma warning restore SYSLIB0003, SYSLIB0050 // Type or member is obsolete
  612.  
  613. #if ! NETCOREAPP
  614. if (info == null)
  615. {
  616. throw new ArgumentNullException(nameof(info));
  617. }
  618. #else
  619. ArgumentNullException.ThrowIfNull(info, nameof(info));
  620. #endif
  621.  
  622. info.AddValue("ARGB", this.colorB.ToArgb, typeof(int));
  623.  
  624. }
  625.  
  626. /// ----------------------------------------------------------------------------------------------------
  627. /// <summary>
  628. /// Initializes a new instance of the <see cref="ColorString"/> class.
  629. /// </summary>
  630. /// ----------------------------------------------------------------------------------------------------
  631. /// <remarks>
  632. /// This constructor is used to deserialize values.
  633. /// </remarks>
  634. /// ----------------------------------------------------------------------------------------------------
  635. /// <param name="info">
  636. /// The <see cref="SerializationInfo"/> to populate with data.
  637. /// </param>
  638. ///
  639. /// <param name="context">
  640. /// The destination (see <see cref="StreamingContext"/>) for this deserialization.
  641. /// </param>
  642. /// ----------------------------------------------------------------------------------------------------
  643. /// <exception cref="ArgumentNullException">
  644. /// info
  645. /// </exception>
  646. /// ----------------------------------------------------------------------------------------------------
  647. // INSTANT C# TASK: This method is a constructor, but no class name was found:
  648. [DebuggerStepThrough]
  649. [EditorBrowsable(EditorBrowsableState.Never)]
  650. private <class name>(SerializationInfo info, StreamingContext context)
  651. {
  652.  
  653. #if ! NETCOREAPP
  654. if (info == null)
  655. {
  656. throw new ArgumentNullException(nameof(info));
  657. }
  658. #else
  659. ArgumentNullException.ThrowIfNull(info, nameof(info));
  660. #endif
  661.  
  662. this.colorB = System.Drawing.Color.FromArgb(info.GetInt32("ARGB"));
  663.  
  664. }
  665.  
  666. #endregion
  667.  
  668. #region IXMLSerializable implementation // For Xml serialization.
  669.  
  670. /// ----------------------------------------------------------------------------------------------------
  671. /// <summary>
  672. /// This method is reserved and should not be used.
  673. /// When implementing the <see cref="IXmlSerializable"/> interface, you should return <see langword="Nothing"/> from this method,
  674. /// and instead, if specifying a custom schema is required, apply the <see cref="XmlSchemaProviderAttribute"/> to the class.
  675. /// </summary>
  676. /// ----------------------------------------------------------------------------------------------------
  677. /// <returns>
  678. /// An <see cref="Schema.XmlSchema"/> that describes the Xml representation of the object
  679. /// that is produced by the <c>IXmlSerializable.WriteXml(Xml.XmlWriter)</c> method
  680. /// and consumed by the <c>IXmlSerializable.ReadXml(Xml.XmlReader)</c> method.
  681. /// </returns>
  682. /// ----------------------------------------------------------------------------------------------------
  683. [DebuggerNonUserCode]
  684. [EditorBrowsable(EditorBrowsableState.Never)]
  685. public System.Xml.Schema.XmlSchema GetSchema()
  686. {
  687.  
  688. return null;
  689.  
  690. }
  691.  
  692. /// ----------------------------------------------------------------------------------------------------
  693. /// <summary>
  694. /// Converts an object into its Xml representation.
  695. /// </summary>
  696. /// ----------------------------------------------------------------------------------------------------
  697. /// <param name="writer">
  698. /// The <see cref="XmlWriter"/> stream to which the object is serialized.
  699. /// </param>
  700. /// ----------------------------------------------------------------------------------------------------
  701. [DebuggerStepThrough]
  702. [EditorBrowsable(EditorBrowsableState.Never)]
  703. public void WriteXml(XmlWriter writer)
  704. {
  705.  
  706. writer.WriteElementString("ARGB", this.colorB.ToArgb.ToString());
  707.  
  708. }
  709.  
  710. /// ----------------------------------------------------------------------------------------------------
  711. /// <summary>
  712. /// Generates an object from its Xml representation.
  713. /// </summary>
  714. /// ----------------------------------------------------------------------------------------------------
  715. /// <param name="reader">
  716. /// The <see cref="XmlReader"/> stream from which the object is deserialized.
  717. /// </param>
  718. /// ----------------------------------------------------------------------------------------------------
  719. [DebuggerStepThrough]
  720. [EditorBrowsable(EditorBrowsableState.Never)]
  721. public void ReadXml(XmlReader reader)
  722. {
  723.  
  724. reader.ReadStartElement("Color");
  725. this.colorB = System.Drawing.Color.FromArgb(int.Parse(reader.ReadElementContentAsString()));
  726.  
  727. }
  728.  
  729. #endregion
  730.  
  731. #region Private Methods
  732.  
  733. /// ----------------------------------------------------------------------------------------------------
  734. /// <summary>
  735. /// Gets the C# representation of a <see cref="System.Drawing.Color"/>, in the specified format.
  736. /// </summary>
  737. /// ----------------------------------------------------------------------------------------------------
  738. /// <param name="format">
  739. /// The color format.
  740. /// </param>
  741. /// ----------------------------------------------------------------------------------------------------
  742. /// <returns>
  743. /// The resulting string representation.
  744. /// </returns>
  745. /// ----------------------------------------------------------------------------------------------------
  746. /// <exception cref="InvalidEnumArgumentException">
  747. /// format
  748. /// </exception>
  749. /// ----------------------------------------------------------------------------------------------------
  750. [DebuggerStepThrough]
  751. private string GetCSharpString(CSharpColorFormat format)
  752. {
  753.  
  754. switch (format)
  755. {
  756.  
  757. case CSharpColorFormat.Int:
  758. string byteString = $@"{Convert.ToString(this.colorB.A)}, {Convert.ToString(this.colorB.R)}, {_
  759. Convert.ToString(this.colorB.G)}, {Convert.ToString(this.colorB.B)}";
  760. return $"Color.FromArgb({byteString});";
  761.  
  762. case CSharpColorFormat.Hex:
  763. string a = Convert.ToString(this.colorB.A, 16).ToUpper();
  764. string r = Convert.ToString(this.colorB.R, 16).ToUpper();
  765. string g = Convert.ToString(this.colorB.G, 16).ToUpper();
  766. string b = Convert.ToString(this.colorB.B, 16).ToUpper();
  767. return $"Color.FromArgb(0x{a}, 0x{r}, 0x{g}, 0x{b});";
  768.  
  769. case CSharpColorFormat.Html:
  770. string htmlString = ColorTranslator.ToHtml(this.Color);
  771. return string.Format("ColorTranslator.FromHtml(\"{0}\");", htmlString, TangibleStringInterpolationMarker);
  772.  
  773. default:
  774. throw new InvalidEnumArgumentException(nameof(format), format, typeof(CSharpColorFormat));
  775.  
  776. }
  777.  
  778. }
  779.  
  780. /// ----------------------------------------------------------------------------------------------------
  781. /// <summary>
  782. /// Gets the VisualBasic representation of a <see cref="System.Drawing.Color"/>, in the specified format.
  783. /// </summary>
  784. /// ----------------------------------------------------------------------------------------------------
  785. /// <param name="format">
  786. /// The color format.
  787. /// </param>
  788. /// ----------------------------------------------------------------------------------------------------
  789. /// <returns>
  790. /// The resulting string representation.
  791. /// </returns>
  792. /// ----------------------------------------------------------------------------------------------------
  793. /// <exception cref="InvalidEnumArgumentException">
  794. /// format
  795. /// </exception>
  796. /// ----------------------------------------------------------------------------------------------------
  797. [DebuggerStepThrough]
  798. private string GetVisualBasicString(VisualBasicColorFormat format)
  799. {
  800.  
  801. switch (format)
  802. {
  803.  
  804. case VisualBasicColorFormat.Int:
  805. string byteString = $@"{Convert.ToString(this.colorB.A)}, {Convert.ToString(this.colorB.R)}, {_
  806. Convert.ToString(this.colorB.G)}, {Convert.ToString(this.colorB.B)}";
  807. return $"Color.FromArgb({byteString})";
  808.  
  809. case VisualBasicColorFormat.Hex:
  810. string a = Convert.ToString(this.colorB.A, 16).ToUpper();
  811. string r = Convert.ToString(this.colorB.R, 16).ToUpper();
  812. string g = Convert.ToString(this.colorB.G, 16).ToUpper();
  813. string b = Convert.ToString(this.colorB.B, 16).ToUpper();
  814. return $"Color.FromArgb(&H{a}, &H{r}, &H{g}, &H{b})";
  815.  
  816. case VisualBasicColorFormat.Html:
  817. string htmlString = ColorTranslator.ToHtml(this.Color);
  818. return string.Format("ColorTranslator.FromHtml(\"{0}\")", htmlString, TangibleStringInterpolationMarker);
  819.  
  820. default:
  821. throw new InvalidEnumArgumentException(nameof(format), format, typeof(VisualBasicColorFormat));
  822.  
  823. }
  824.  
  825. }
  826.  
  827. /// ----------------------------------------------------------------------------------------------------
  828. /// <summary>
  829. /// Gets the Visual Studio representation of a <see cref="System.Drawing.Color"/>, in the specified format.
  830. /// </summary>
  831. /// ----------------------------------------------------------------------------------------------------
  832. /// <param name="format">
  833. /// The color format.
  834. /// </param>
  835. /// ----------------------------------------------------------------------------------------------------
  836. /// <returns>
  837. /// The resulting string representation.
  838. /// </returns>
  839. /// ----------------------------------------------------------------------------------------------------
  840. /// <exception cref="InvalidEnumArgumentException">
  841. /// format
  842. /// </exception>
  843. /// ----------------------------------------------------------------------------------------------------
  844. [DebuggerStepThrough]
  845. private string GetVisualStudioString(VisualStudioColorFormat format)
  846. {
  847.  
  848. switch (format)
  849. {
  850.  
  851. case VisualStudioColorFormat.Int:
  852. return $@"{Convert.ToString(this.colorB.A)}; {Convert.ToString(this.colorB.R)}; {_
  853. Convert.ToString(this.colorB.G)}; {Convert.ToString(this.colorB.B)}";
  854.  
  855. case VisualStudioColorFormat.Hex:
  856. string a = Convert.ToString(this.colorB.A, 16).ToUpper();
  857. string r = Convert.ToString(this.colorB.R, 16).ToUpper();
  858. string g = Convert.ToString(this.colorB.G, 16).ToUpper();
  859. string b = Convert.ToString(this.colorB.B, 16).ToUpper();
  860. return $"0x{a}{r}{g}{b}";
  861.  
  862. default:
  863. throw new InvalidEnumArgumentException(nameof(format), format, typeof(VisualStudioColorFormat));
  864.  
  865. }
  866.  
  867. }
  868.  
  869. #endregion
  870.  
  871. }
  872.  
  873.  
  874. #endregion
  875.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement