Skip to content

Commit

Permalink
Refactored and mad a custom hashcode method for controller and host f…
Browse files Browse the repository at this point in the history
…or a more uniform colors of blocks
  • Loading branch information
cosminpopescu14 committed Apr 27, 2020
1 parent 4ccf359 commit b56fbc7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
15 changes: 10 additions & 5 deletions src/NetCore2Blockly/NetCore2Blockly/ActionInfo.cs
Expand Up @@ -61,17 +61,22 @@ public class ActionInfo
/// The verb.
/// </value>
public string Verb { get; set; }

internal virtual Dictionary<string, (Type type, BindingSource bs)> Params { get; set; }
internal bool HasParams => (Params?.Count ?? 0) > 0;

/// <summary>
/// Initializes a new instance of the <see cref="ActionInfo"/> class.
///
/// </summary>
public ActionInfo()
/// <returns></returns>
public int CustomGetHashCode()
{
int hash = 13;
hash = (hash * 7) + Host?.GetHashCode() ?? 0;
hash = (hash * 7) + ControllerName.GetHashCode();

return hash;
}

internal virtual Dictionary<string, (Type type, BindingSource bs)> Params { get; set; }
internal bool HasParams => (Params?.Count ?? 0) > 0;

Dictionary<string, (Type type, BindingSource bs)> GetParameters(ApiParameterDescription[] parameterDescriptions)
{
Expand Down
20 changes: 6 additions & 14 deletions src/NetCore2Blockly/NetCore2Blockly/BlocklyStringToColor.cs
@@ -1,8 +1,5 @@
using NetCore2Blockly.ExtensionMethods;
using System;
using System.Drawing;
using System.Linq;
using System.Text;

namespace NetCore2Blockly
{
Expand All @@ -13,19 +10,14 @@ public class BlocklyStringToColor
{

/// <summary>
///
/// Convert to hue given the controller name
/// </summary>
/// <param name="controllerName"></param>
/// <param name="value"></param>
/// <returns></returns>
public static int ConvertToHue(string controllerName)
public static int ConvertToHue(int value)
{
//Sum of characters beacuse the hashcode is changing
var asciiArraySum = controllerName.ToCharArray()
.Select(it => (int)it)
.Sum();

var hexColor = ToHexColor(asciiArraySum);
var rgbColor = ConvertFromHexToRgb(Convert.ToInt32("0x" + hexColor, 16));
var hexColor = ToHexColor(value);
var rgbColor = ConvertFromHexToRgb(Convert.ToInt32(hexColor, 16));
var (h, s, v) = ConvertFromRgbToHue(rgbColor);
return (int)h; //cast to int because we want the integer part
}
Expand Down Expand Up @@ -54,7 +46,7 @@ private static (int r, int g, int b) ConvertFromHexToRgb(int hexColor)

//http://www.kourbatov.com/faq/rgb2hsv.htm
/// <summary>
/// Generate hsv color sapce from rgb colors
/// Generate hsv color space from rgb colors
/// </summary>
/// <param name="rgbColor"></param>
/// <returns></returns>
Expand Down
Expand Up @@ -51,7 +51,9 @@ public string GenerateFunctionDefinition(ActionInfo actionInfo)
var strPropsDefinition = GeneratePropertyDefinitions(actionInfo);

var returnType = $@"this.setOutput(true,'{actionInfo.ReturnType.TranslateToBlocklyType()}');";
var blockColor = BlocklyStringToColor.ConvertToHue(actionInfo.ControllerName);
var actionHash = actionInfo.CustomGetHashCode();

var blockColor = BlocklyStringToColor.ConvertToHue(actionHash);
return $@"
Blockly.Blocks['{actionInfo.GenerateCommandName()}'] = {{
init: function() {{
Expand Down
13 changes: 7 additions & 6 deletions src/NetCore2Blockly/NetCore2Blockly/NetCore2Blockly.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b56fbc7

Please sign in to comment.